Chris@16
|
1 //
|
Chris@16
|
2 // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
|
Chris@16
|
3 //
|
Chris@16
|
4 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
5 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
6 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7 //
|
Chris@16
|
8 #ifndef BOOST_LOCLAE_GNU_GETTEXT_HPP
|
Chris@16
|
9 #define BOOST_LOCLAE_GNU_GETTEXT_HPP
|
Chris@16
|
10
|
Chris@16
|
11 #include <boost/locale/message.hpp>
|
Chris@16
|
12 #include <boost/function.hpp>
|
Chris@16
|
13 #include <stdexcept>
|
Chris@16
|
14
|
Chris@16
|
15 namespace boost {
|
Chris@16
|
16 namespace locale {
|
Chris@16
|
17 /// \addtogroup message
|
Chris@16
|
18 /// @{
|
Chris@16
|
19
|
Chris@16
|
20
|
Chris@16
|
21 ///
|
Chris@16
|
22 /// \brief This namespace holds classes that provide GNU Gettext message catalogs support.
|
Chris@16
|
23 ///
|
Chris@16
|
24 namespace gnu_gettext {
|
Chris@16
|
25
|
Chris@16
|
26 ///
|
Chris@16
|
27 /// \brief This structure holds all information required for creating gnu-gettext message catalogs,
|
Chris@16
|
28 ///
|
Chris@16
|
29 /// The user is expected to set its parameters to load these catalogs correctly. This structure
|
Chris@16
|
30 /// also allows providing functions for charset conversion. Note, you need to provide them,
|
Chris@16
|
31 /// so this structure is not useful for wide characters without subclassing and it will also
|
Chris@16
|
32 /// ignore gettext catalogs that use a charset different from \a encoding.
|
Chris@16
|
33 ///
|
Chris@16
|
34 struct messages_info {
|
Chris@16
|
35 messages_info() :
|
Chris@16
|
36 language("C"),
|
Chris@16
|
37 locale_category("LC_MESSAGES")
|
Chris@16
|
38 {
|
Chris@16
|
39 }
|
Chris@16
|
40
|
Chris@16
|
41 std::string language; ///< The language we load the catalog for, like "ru", "en", "de"
|
Chris@16
|
42 std::string country; ///< The country we load the catalog for, like "US", "IL"
|
Chris@16
|
43 std::string variant; ///< Language variant, like "euro" so it would look for catalog like de_DE\@euro
|
Chris@16
|
44 std::string encoding; ///< Required target charset encoding. Ignored for wide characters.
|
Chris@16
|
45 ///< For narrow, should specify the correct encoding required for this catalog
|
Chris@16
|
46 std::string locale_category; ///< Locale category, is set by default to LC_MESSAGES, but may be changed
|
Chris@16
|
47 ///
|
Chris@16
|
48 /// \brief This type represents GNU Gettext domain name for the messages.
|
Chris@16
|
49 ///
|
Chris@16
|
50 /// It consists of two parameters:
|
Chris@16
|
51 ///
|
Chris@16
|
52 /// - name - the name of the domain - used for opening the file name
|
Chris@16
|
53 /// - encoding - the encoding of the keys in the sources, default - UTF-8
|
Chris@16
|
54 ///
|
Chris@16
|
55 struct domain {
|
Chris@16
|
56
|
Chris@16
|
57 std::string name; ///< The name of the domain
|
Chris@16
|
58 std::string encoding; ///< The character encoding for the domain
|
Chris@16
|
59 domain() {}
|
Chris@16
|
60 ///
|
Chris@16
|
61 /// Create a domain object from the name that can hold an encoding after symbol "/"
|
Chris@16
|
62 /// such that if n is "hello/cp1255" then the name would be "hello" and "encoding" would
|
Chris@16
|
63 /// be "cp1255" and if n is "hello" then the name would be the same but encoding would be
|
Chris@16
|
64 /// "UTF-8"
|
Chris@16
|
65 ///
|
Chris@16
|
66 domain(std::string const &n)
|
Chris@16
|
67 {
|
Chris@16
|
68 size_t pos = n.find("/");
|
Chris@16
|
69 if(pos==std::string::npos) {
|
Chris@16
|
70 name = n;
|
Chris@16
|
71 encoding = "UTF-8";
|
Chris@16
|
72 }
|
Chris@16
|
73 else {
|
Chris@16
|
74 name = n.substr(0,pos);
|
Chris@16
|
75 encoding = n.substr(pos+1);
|
Chris@16
|
76 }
|
Chris@16
|
77
|
Chris@16
|
78 }
|
Chris@16
|
79
|
Chris@16
|
80 ///
|
Chris@16
|
81 /// Check whether two objects are equivalent, only names are compared, encoding is ignored
|
Chris@16
|
82 ///
|
Chris@16
|
83 bool operator==(domain const &other) const
|
Chris@16
|
84 {
|
Chris@16
|
85 return name==other.name;
|
Chris@16
|
86 }
|
Chris@16
|
87 ///
|
Chris@16
|
88 /// Check whether two objects are distinct, only names are compared, encoding is ignored
|
Chris@16
|
89 ///
|
Chris@16
|
90 bool operator!=(domain const &other) const
|
Chris@16
|
91 {
|
Chris@16
|
92 return !(*this==other);
|
Chris@16
|
93 }
|
Chris@16
|
94
|
Chris@16
|
95 };
|
Chris@16
|
96
|
Chris@16
|
97 typedef std::vector<domain> domains_type; ///< Type that defines a list of domains that are loaded
|
Chris@16
|
98 ///< The first one is the default one
|
Chris@16
|
99 domains_type domains; ///< Message domains - application name, like my_app. So files named my_app.mo
|
Chris@16
|
100 ///< would be loaded
|
Chris@16
|
101 std::vector<std::string> paths; ///< Paths to search files in. Under MS Windows it uses encoding
|
Chris@16
|
102 ///< parameter to convert them to wide OS specific paths.
|
Chris@16
|
103
|
Chris@16
|
104 ///
|
Chris@16
|
105 /// The callback for custom file system support. This callback should read the file named \a file_name
|
Chris@16
|
106 /// encoded in \a encoding character set into std::vector<char> and return it.
|
Chris@16
|
107 ///
|
Chris@16
|
108 /// - If the file does not exist, it should return an empty vector.
|
Chris@16
|
109 /// - If a error occurs during file read it should throw a error.
|
Chris@16
|
110 ///
|
Chris@16
|
111 /// \note The user should support only the encodings the locales are created for. So if the user
|
Chris@16
|
112 /// uses only one encoding or the file system is encoding agnostic, he may ignore the \a encoding parameter.
|
Chris@16
|
113 ///
|
Chris@16
|
114 typedef function<
|
Chris@16
|
115 std::vector<char>(
|
Chris@16
|
116 std::string const &file_name,
|
Chris@16
|
117 std::string const &encoding
|
Chris@16
|
118 )
|
Chris@16
|
119 > callback_type;
|
Chris@16
|
120
|
Chris@16
|
121 ///
|
Chris@16
|
122 /// The callback for handling custom file systems, if it is empty, the real OS file-system
|
Chris@16
|
123 /// is being used.
|
Chris@16
|
124 ///
|
Chris@16
|
125 callback_type callback;
|
Chris@16
|
126
|
Chris@16
|
127 };
|
Chris@16
|
128
|
Chris@16
|
129 ///
|
Chris@16
|
130 /// Create a message_format facet using GNU Gettext catalogs. It uses \a info structure to get
|
Chris@16
|
131 /// information about where to read them from and uses it for character set conversion (if needed)
|
Chris@16
|
132 ///
|
Chris@16
|
133
|
Chris@16
|
134 template<typename CharType>
|
Chris@16
|
135 message_format<CharType> *create_messages_facet(messages_info const &info);
|
Chris@16
|
136
|
Chris@16
|
137 /// \cond INTERNAL
|
Chris@16
|
138
|
Chris@16
|
139 template<>
|
Chris@16
|
140 BOOST_LOCALE_DECL message_format<char> *create_messages_facet(messages_info const &info);
|
Chris@16
|
141
|
Chris@16
|
142 template<>
|
Chris@16
|
143 BOOST_LOCALE_DECL message_format<wchar_t> *create_messages_facet(messages_info const &info);
|
Chris@16
|
144
|
Chris@16
|
145 #ifdef BOOST_HAS_CHAR16_T
|
Chris@16
|
146 template<>
|
Chris@16
|
147 BOOST_LOCALE_DECL message_format<char16_t> *create_messages_facet(messages_info const &info);
|
Chris@16
|
148 #endif
|
Chris@16
|
149
|
Chris@16
|
150 #ifdef BOOST_HAS_CHAR32_T
|
Chris@16
|
151 template<>
|
Chris@16
|
152 BOOST_LOCALE_DECL message_format<char32_t> *create_messages_facet(messages_info const &info);
|
Chris@16
|
153 #endif
|
Chris@16
|
154
|
Chris@16
|
155 /// \endcond
|
Chris@16
|
156
|
Chris@16
|
157 } // gnu_gettext
|
Chris@16
|
158
|
Chris@16
|
159 /// @}
|
Chris@16
|
160
|
Chris@16
|
161 } // locale
|
Chris@16
|
162 } // boost
|
Chris@16
|
163
|
Chris@16
|
164 #endif
|
Chris@16
|
165
|
Chris@16
|
166 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
|
Chris@16
|
167
|