E-MailRelay

ggettext.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file ggettext.h
19///
20
21#ifndef G_GETTEXT_H
22#define G_GETTEXT_H
23
24#include "gdef.h"
25#include "gstringview.h"
26#include <string>
27
28// String literals should be marked for translation using gettext() or
29// gettext_noop(), but not using the "G::" namespace scoping so that
30// 'xgettext(1)' will still work. For brevity G::txt() or G::tx()
31// can be used instead. See also G::format.
32//
33// Eg:
34/// \code
35/// #include "ggettext.h"
36/// using G::tx ;
37/// using G::txt ;
38/// Message msg( tx("world") ) ; // Message ctor calls gettext()
39/// std::cout << txt("hello") << msg.translated() << "\n" ;
40/// \endcode
41
42 namespace G
43{
44 void gettext_init( const std::string & localedir , const std::string & name ) ;
45 ///< Initialises the gettext() library. This uses environment variables
46 ///< to set the CTYPE and MESSAGES facets of the global C locale as a
47 ///< side-effect.
48
49 const char * gettext( const char * ) noexcept ;
50 ///< Returns the message translation in the current locale's codeset,
51 ///< eg. ISO-8859-1 or UTF-8, transcoding from the catalogue as
52 ///< necessary.
53
54 constexpr const char * gettext_noop( const char * p ) noexcept ;
55 ///< Returns the parameter. Used to mark a string-literal for
56 ///< translation, with the conversion at run-time done with
57 ///< a call to gettext() elsewhere in the code.
58 ///<
59 ///< \code
60 ///< using G::gettext_noop ;
61 ///< std::cout << call_gettext( gettext_noop("hello, world") ) ;
62 ///< \endcode
63
64 const char * txt( const char * p ) noexcept ;
65 ///< A briefer alternative to G::gettext().
66
67 constexpr const char * tx( const char * p ) noexcept ;
68 ///< A briefer alternative to G::gettext_noop().
69
70 constexpr std::string_view tx( std::string_view sv ) noexcept ;
71 ///< String view overload.
72}
73
74 inline const char * G::txt( const char * p ) noexcept
75{
76 return G::gettext( p ) ;
77}
78
79 constexpr const char * G::gettext_noop( const char * p ) noexcept
80{
81 return p ;
82}
83
84 constexpr const char * G::tx( const char * p ) noexcept
85{
86 return p ;
87}
88
89 constexpr std::string_view G::tx( std::string_view sv ) noexcept
90{
91 return sv ;
92}
93
94#endif
gstringview.h
G
Low-level classes.
Definition: garg.h:36
G::gettext
const char * gettext(const char *) noexcept
Returns the message translation in the current locale's codeset, eg.
Definition: ggettext_none.cpp:28
G::gettext_noop
constexpr const char * gettext_noop(const char *p) noexcept
Returns the parameter.
Definition: ggettext.h:79
G::txt
const char * txt(const char *p) noexcept
A briefer alternative to G::gettext().
Definition: ggettext.h:74
G::tx
constexpr const char * tx(const char *p) noexcept
A briefer alternative to G::gettext_noop().
Definition: ggettext.h:84
G::gettext_init
void gettext_init(const std::string &localedir, const std::string &name)
Initialises the gettext() library.
Definition: ggettext_none.cpp:24