1 <section xmlns="http://docbook.org/ns/docbook" version="5.0"
2 xml:id="manual.localization.facet.messages" xreflabel="Messages">
3 <?dbhtml filename="messages.html"?>
5 <info><title>messages</title>
19 The std::messages facet implements message retrieval functionality
20 equivalent to Java's java.text.MessageFormat .using either GNU gettext
21 or IEEE 1003.1-200 functions.
24 <section xml:id="facet.messages.req"><info><title>Requirements</title></info>
28 The std::messages facet is probably the most vaguely defined facet in
29 the standard library. It's assumed that this facility was built into
30 the standard library in order to convert string literals from one
31 locale to the other. For instance, converting the "C" locale's
32 <code>const char* c = "please"</code> to a German-localized <code>"bitte"</code>
33 during program execution.
38 22.2.7.1 - Template class messages [lib.locale.messages]
43 This class has three public member functions, which directly
44 correspond to three protected virtual member functions.
48 The public member functions are:
52 <code>catalog open(const string&, const locale&) const</code>
56 <code>string_type get(catalog, int, int, const string_type&) const</code>
60 <code>void close(catalog) const</code>
64 While the virtual functions are:
68 <code>catalog do_open(const string&, const locale&) const</code>
73 -1- Returns: A value that may be passed to get() to retrieve a
74 message, from the message catalog identified by the string name
75 according to an implementation-defined mapping. The result can be used
76 until it is passed to close(). Returns a value less than 0 if no such
77 catalog can be opened.
83 <code>string_type do_get(catalog, int, int, const string_type&) const</code>
88 -3- Requires: A catalog cat obtained from open() and not yet closed.
89 -4- Returns: A message identified by arguments set, msgid, and dfault,
90 according to an implementation-defined mapping. If no such message can
91 be found, returns dfault.
97 <code>void do_close(catalog) const</code>
102 -5- Requires: A catalog cat obtained from open() and not yet closed.
103 -6- Effects: Releases unspecified resources associated with cat.
104 -7- Notes: The limit on such resources, if any, is implementation-defined.
112 <section xml:id="facet.messages.design"><info><title>Design</title></info>
116 A couple of notes on the standard.
120 First, why is <code>messages_base::catalog</code> specified as a typedef
121 to int? This makes sense for implementations that use
122 <code>catopen</code>, but not for others. Fortunately, it's not heavily
123 used and so only a minor irritant.
127 Second, by making the member functions <code>const</code>, it is
128 impossible to save state in them. Thus, storing away information used
129 in the 'open' member function for use in 'get' is impossible. This is
134 The 'open' member function in particular seems to be oddly
135 designed. The signature seems quite peculiar. Why specify a <code>const
136 string& </code> argument, for instance, instead of just <code>const
137 char*</code>? Or, why specify a <code>const locale&</code> argument that is
138 to be used in the 'get' member function? How, exactly, is this locale
139 argument useful? What was the intent? It might make sense if a locale
140 argument was associated with a given default message string in the
141 'open' member function, for instance. Quite murky and unclear, on
146 Lastly, it seems odd that messages, which explicitly require code
147 conversion, don't use the codecvt facet. Because the messages facet
148 has only one template parameter, it is assumed that ctype, and not
149 codecvt, is to be used to convert between character sets.
153 It is implicitly assumed that the locale for the default message
154 string in 'get' is in the "C" locale. Thus, all source code is assumed
155 to be written in English, so translations are always from "en_US" to
156 other, explicitly named locales.
161 <section xml:id="facet.messages.impl"><info><title>Implementation</title></info>
164 <section xml:id="messages.impl.models"><info><title>Models</title></info>
167 This is a relatively simple class, on the face of it. The standard
168 specifies very little in concrete terms, so generic
169 implementations that are conforming yet do very little are the
170 norm. Adding functionality that would be useful to programmers and
171 comparable to Java's java.text.MessageFormat takes a bit of work,
172 and is highly dependent on the capabilities of the underlying
177 Three different mechanisms have been provided, selectable via
187 This model does very little, and is what is used by default.
196 The gnu model is complete and fully tested. It's based on the
197 GNU gettext package, which is part of glibc. It uses the
198 functions <code>textdomain, bindtextdomain, gettext</code> to
199 implement full functionality. Creating message catalogs is a
200 relatively straight-forward process and is lightly documented
201 below, and fully documented in gettext's distributed
211 This is a complete, though untested, implementation based on
212 the IEEE standard. The functions <code>catopen, catgets,
213 catclose</code> are used to retrieve locale-specific messages
214 given the appropriate message catalogs that have been
215 constructed for their use. Note, the script <code>
216 po2msg.sed</code> that is part of the gettext distribution can
217 convert gettext catalogs into catalogs that
218 <code>catopen</code> can use.
224 A new, standards-conformant non-virtual member function signature was
225 added for 'open' so that a directory could be specified with a given
226 message catalog. This simplifies calling conventions for the gnu
232 <section xml:id="messages.impl.gnu"><info><title>The GNU Model</title></info>
236 The messages facet, because it is retrieving and converting
237 between characters sets, depends on the ctype and perhaps the
238 codecvt facet in a given locale. In addition, underlying "C"
239 library locale support is necessary for more than just the
240 <code>LC_MESSAGES</code> mask: <code>LC_CTYPE</code> is also
241 necessary. To avoid any unpleasantness, all bits of the "C" mask
242 (i.e. <code>LC_ALL</code>) are set before retrieving messages.
246 Making the message catalogs can be initially tricky, but become
247 quite simple with practice. For complete info, see the gettext
248 documentation. Here's an idea of what is required:
254 Make a source file with the required string literals that need
255 to be translated. See <code>intl/string_literals.cc</code> for
262 Make initial catalog (see "4 Making the PO Template File" from
263 the gettext docs).</para>
265 <code> xgettext --c++ --debug string_literals.cc -o libstdc++.pot </code>
270 <para>Make language and country-specific locale catalogs.</para>
272 <code>cp libstdc++.pot fr_FR.po</code>
275 <code>cp libstdc++.pot de_DE.po</code>
281 Edit localized catalogs in emacs so that strings are
285 <code>emacs fr_FR.po</code>
290 <para>Make the binary mo files.</para>
292 <code>msgfmt fr_FR.po -o fr_FR.mo</code>
295 <code>msgfmt de_DE.po -o de_DE.mo</code>
300 <para>Copy the binary files into the correct directory structure.</para>
302 <code>cp fr_FR.mo (dir)/fr_FR/LC_MESSAGES/libstdc++.mo</code>
305 <code>cp de_DE.mo (dir)/de_DE/LC_MESSAGES/libstdc++.mo</code>
310 <para>Use the new message catalogs.</para>
312 <code>locale loc_de("de_DE");</code>
316 use_facet<messages<char> >(loc_de).open("libstdc++", locale(), dir);
325 <section xml:id="facet.messages.use"><info><title>Use</title></info>
328 A simple example using the GNU model of message conversion.
332 #include <iostream>
333 #include <locale>
338 typedef messages<char>::catalog catalog;
340 "/mnt/egcs/build/i686-pc-linux-gnu/libstdc++/po/share/locale";
341 const locale loc_de("de_DE");
342 const messages<char>& mssg_de = use_facet<messages<char> >(loc_de);
344 catalog cat_de = mssg_de.open("libstdc++", loc_de, dir);
345 string s01 = mssg_de.get(cat_de, 0, 0, "please");
346 string s02 = mssg_de.get(cat_de, 0, 0, "thank you");
347 cout << "please in german:" << s01 << '\n';
348 cout << "thank you in german:" << s02 << '\n';
349 mssg_de.close(cat_de);
355 <section xml:id="facet.messages.future"><info><title>Future</title></info>
361 Things that are sketchy, or remain unimplemented:
366 _M_convert_from_char, _M_convert_to_char are in flux,
367 depending on how the library ends up doing character set
368 conversions. It might not be possible to do a real character
369 set based conversion, due to the fact that the template
370 parameter for messages is not enough to instantiate the
371 codecvt facet (1 supplied, need at least 2 but would prefer
378 There are issues with gettext needing the global locale set
379 to extract a message. This dependence on the global locale
380 makes the current "gnu" model non MT-safe. Future versions
381 of glibc, i.e. glibc 2.3.x will fix this, and the C++ library
382 bits are already in place.
390 Development versions of the GNU "C" library, glibc 2.3 will allow
391 a more efficient, MT implementation of std::messages, and will
392 allow the removal of the _M_name_messages data member. If this is
393 done, it will change the library ABI. The C++ parts to support
394 glibc 2.3 have already been coded, but are not in use: once this
395 version of the "C" library is released, the marked parts of the
396 messages implementation can be switched over to the new "C"
397 library functionality.
402 At some point in the near future, std::numpunct will probably use
403 std::messages facilities to implement truename/falsename
404 correctly. This is currently not done, but entries in
405 libstdc++.pot have already been made for "true" and "false" string
406 literals, so all that remains is the std::numpunct coding and the
407 configure/make hassles to make the installed library search its
408 own catalog. Currently the libstdc++.mo catalog is only searched
409 for the testsuite cases involving messages members.
414 <para> The following member functions:</para>
419 open(const basic_string<char>& __s, const locale& __loc) const
426 open(const basic_string<char>&, const locale&, const char*) const;
431 Don't actually return a "value less than 0 if no such catalog
432 can be opened" as required by the standard in the "gnu"
433 model. As of this writing, it is unknown how to query to see
434 if a specified message catalog exists using the gettext
442 <bibliography xml:id="facet.messages.biblio"><info><title>Bibliography</title></info>
449 <author><personname><surname>McGrath</surname><firstname>Roland</firstname></personname></author>
450 <author><personname><surname>Drepper</surname><firstname>Ulrich</firstname></personname></author>
455 <pagenums>Chapters 6 Character Set Handling, and 7 Locales and Internationalization
463 <author><personname><surname>Drepper</surname><firstname>Ulrich</firstname></personname></author>
472 ISO/IEC 14882:1998 Programming languages - C++
482 ISO/IEC 9899:1999 Programming languages - C
493 <link xmlns:xlink="http://www.w3.org/1999/xlink"
494 xlink:href="http://www.opengroup.org/austin">
495 System Interface Definitions, Issue 7 (IEEE Std. 1003.1-2008)
501 The Open Group/The Institute of Electrical and Electronics
509 The C++ Programming Language, Special Edition
511 <author><personname><surname>Stroustrup</surname><firstname>Bjarne</firstname></personname></author>
514 <holder>Addison Wesley, Inc.</holder>
516 <pagenums>Appendix D</pagenums>
526 Standard C++ IOStreams and Locales
529 Advanced Programmer's Guide and Reference
531 <author><personname><surname>Langer</surname><firstname>Angelika</firstname></personname></author>
532 <author><personname><surname>Kreft</surname><firstname>Klaus</firstname></personname></author>
535 <holder>Addison Wesley Longman, Inc.</holder>
539 Addison Wesley Longman
546 <link xmlns:xlink="http://www.w3.org/1999/xlink"
547 xlink:href="http://java.sun.com/reference/api/index.html">
548 API Specifications, Java Platform
552 <pagenums>java.util.Properties, java.text.MessageFormat,
553 java.util.Locale, java.util.ResourceBundle
560 <link xmlns:xlink="http://www.w3.org/1999/xlink"
561 xlink:href="http://www.gnu.org/software/gettext">
562 GNU gettext tools, version 0.10.38, Native Language Support