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> and define <code>nl_catd</code> as int, but not for
123 others. Fortunately, it's not heavily used and so only a minor irritant.
124 This has been reported as a possible defect in the standard (LWG 2028).
128 Second, by making the member functions <code>const</code>, it is
129 impossible to save state in them. Thus, storing away information used
130 in the 'open' member function for use in 'get' is impossible. This is
135 The 'open' member function in particular seems to be oddly
136 designed. The signature seems quite peculiar. Why specify a <code>const
137 string& </code> argument, for instance, instead of just <code>const
138 char*</code>? Or, why specify a <code>const locale&</code> argument that is
139 to be used in the 'get' member function? How, exactly, is this locale
140 argument useful? What was the intent? It might make sense if a locale
141 argument was associated with a given default message string in the
142 'open' member function, for instance. Quite murky and unclear, on
147 Lastly, it seems odd that messages, which explicitly require code
148 conversion, don't use the codecvt facet. Because the messages facet
149 has only one template parameter, it is assumed that ctype, and not
150 codecvt, is to be used to convert between character sets.
154 It is implicitly assumed that the locale for the default message
155 string in 'get' is in the "C" locale. Thus, all source code is assumed
156 to be written in English, so translations are always from "en_US" to
157 other, explicitly named locales.
162 <section xml:id="facet.messages.impl"><info><title>Implementation</title></info>
165 <section xml:id="messages.impl.models"><info><title>Models</title></info>
168 This is a relatively simple class, on the face of it. The standard
169 specifies very little in concrete terms, so generic
170 implementations that are conforming yet do very little are the
171 norm. Adding functionality that would be useful to programmers and
172 comparable to Java's java.text.MessageFormat takes a bit of work,
173 and is highly dependent on the capabilities of the underlying
178 Three different mechanisms have been provided, selectable via
188 This model does very little, and is what is used by default.
197 The gnu model is complete and fully tested. It's based on the
198 GNU gettext package, which is part of glibc. It uses the
199 functions <code>textdomain, bindtextdomain, gettext</code> to
200 implement full functionality. Creating message catalogs is a
201 relatively straight-forward process and is lightly documented
202 below, and fully documented in gettext's distributed
212 This is a complete, though untested, implementation based on
213 the IEEE standard. The functions <code>catopen, catgets,
214 catclose</code> are used to retrieve locale-specific messages
215 given the appropriate message catalogs that have been
216 constructed for their use. Note, the script <code>
217 po2msg.sed</code> that is part of the gettext distribution can
218 convert gettext catalogs into catalogs that
219 <code>catopen</code> can use.
225 A new, standards-conformant non-virtual member function signature was
226 added for 'open' so that a directory could be specified with a given
227 message catalog. This simplifies calling conventions for the gnu
233 <section xml:id="messages.impl.gnu"><info><title>The GNU Model</title></info>
237 The messages facet, because it is retrieving and converting
238 between characters sets, depends on the ctype and perhaps the
239 codecvt facet in a given locale. In addition, underlying "C"
240 library locale support is necessary for more than just the
241 <code>LC_MESSAGES</code> mask: <code>LC_CTYPE</code> is also
242 necessary. To avoid any unpleasantness, all bits of the "C" mask
243 (i.e. <code>LC_ALL</code>) are set before retrieving messages.
247 Making the message catalogs can be initially tricky, but become
248 quite simple with practice. For complete info, see the gettext
249 documentation. Here's an idea of what is required:
255 Make a source file with the required string literals that need
256 to be translated. See <code>intl/string_literals.cc</code> for
263 Make initial catalog (see "4 Making the PO Template File" from
264 the gettext docs).</para>
266 <code> xgettext --c++ --debug string_literals.cc -o libstdc++.pot </code>
271 <para>Make language and country-specific locale catalogs.</para>
273 <code>cp libstdc++.pot fr_FR.po</code>
276 <code>cp libstdc++.pot de_DE.po</code>
282 Edit localized catalogs in emacs so that strings are
286 <code>emacs fr_FR.po</code>
291 <para>Make the binary mo files.</para>
293 <code>msgfmt fr_FR.po -o fr_FR.mo</code>
296 <code>msgfmt de_DE.po -o de_DE.mo</code>
301 <para>Copy the binary files into the correct directory structure.</para>
303 <code>cp fr_FR.mo (dir)/fr_FR/LC_MESSAGES/libstdc++.mo</code>
306 <code>cp de_DE.mo (dir)/de_DE/LC_MESSAGES/libstdc++.mo</code>
311 <para>Use the new message catalogs.</para>
313 <code>locale loc_de("de_DE");</code>
317 use_facet<messages<char> >(loc_de).open("libstdc++", locale(), dir);
326 <section xml:id="facet.messages.use"><info><title>Use</title></info>
329 A simple example using the GNU model of message conversion.
333 #include <iostream>
334 #include <locale>
339 typedef messages<char>::catalog catalog;
341 "/mnt/egcs/build/i686-pc-linux-gnu/libstdc++/po/share/locale";
342 const locale loc_de("de_DE");
343 const messages<char>& mssg_de = use_facet<messages<char> >(loc_de);
345 catalog cat_de = mssg_de.open("libstdc++", loc_de, dir);
346 string s01 = mssg_de.get(cat_de, 0, 0, "please");
347 string s02 = mssg_de.get(cat_de, 0, 0, "thank you");
348 cout << "please in german:" << s01 << '\n';
349 cout << "thank you in german:" << s02 << '\n';
350 mssg_de.close(cat_de);
356 <section xml:id="facet.messages.future"><info><title>Future</title></info>
362 Things that are sketchy, or remain unimplemented:
367 _M_convert_from_char, _M_convert_to_char are in flux,
368 depending on how the library ends up doing character set
369 conversions. It might not be possible to do a real character
370 set based conversion, due to the fact that the template
371 parameter for messages is not enough to instantiate the
372 codecvt facet (1 supplied, need at least 2 but would prefer
379 There are issues with gettext needing the global locale set
380 to extract a message. This dependence on the global locale
381 makes the current "gnu" model non MT-safe. Future versions
382 of glibc, i.e. glibc 2.3.x will fix this, and the C++ library
383 bits are already in place.
391 Development versions of the GNU "C" library, glibc 2.3 will allow
392 a more efficient, MT implementation of std::messages, and will
393 allow the removal of the _M_name_messages data member. If this is
394 done, it will change the library ABI. The C++ parts to support
395 glibc 2.3 have already been coded, but are not in use: once this
396 version of the "C" library is released, the marked parts of the
397 messages implementation can be switched over to the new "C"
398 library functionality.
403 At some point in the near future, std::numpunct will probably use
404 std::messages facilities to implement truename/falsename
405 correctly. This is currently not done, but entries in
406 libstdc++.pot have already been made for "true" and "false" string
407 literals, so all that remains is the std::numpunct coding and the
408 configure/make hassles to make the installed library search its
409 own catalog. Currently the libstdc++.mo catalog is only searched
410 for the testsuite cases involving messages members.
415 <para> The following member functions:</para>
420 open(const basic_string<char>& __s, const locale& __loc) const
427 open(const basic_string<char>&, const locale&, const char*) const;
432 Don't actually return a "value less than 0 if no such catalog
433 can be opened" as required by the standard in the "gnu"
434 model. As of this writing, it is unknown how to query to see
435 if a specified message catalog exists using the gettext
443 <bibliography xml:id="facet.messages.biblio"><info><title>Bibliography</title></info>
450 <author><personname><surname>McGrath</surname><firstname>Roland</firstname></personname></author>
451 <author><personname><surname>Drepper</surname><firstname>Ulrich</firstname></personname></author>
456 <pagenums>Chapters 6 Character Set Handling, and 7 Locales and Internationalization
464 <author><personname><surname>Drepper</surname><firstname>Ulrich</firstname></personname></author>
473 ISO/IEC 14882:1998 Programming languages - C++
483 ISO/IEC 9899:1999 Programming languages - C
494 <link xmlns:xlink="http://www.w3.org/1999/xlink"
495 xlink:href="http://www.opengroup.org/austin">
496 System Interface Definitions, Issue 7 (IEEE Std. 1003.1-2008)
502 The Open Group/The Institute of Electrical and Electronics
510 The C++ Programming Language, Special Edition
512 <author><personname><surname>Stroustrup</surname><firstname>Bjarne</firstname></personname></author>
515 <holder>Addison Wesley, Inc.</holder>
517 <pagenums>Appendix D</pagenums>
527 Standard C++ IOStreams and Locales
530 Advanced Programmer's Guide and Reference
532 <author><personname><surname>Langer</surname><firstname>Angelika</firstname></personname></author>
533 <author><personname><surname>Kreft</surname><firstname>Klaus</firstname></personname></author>
536 <holder>Addison Wesley Longman, Inc.</holder>
540 Addison Wesley Longman
547 <link xmlns:xlink="http://www.w3.org/1999/xlink"
548 xlink:href="http://java.sun.com/reference/api/index.html">
549 API Specifications, Java Platform
553 <pagenums>java.util.Properties, java.text.MessageFormat,
554 java.util.Locale, java.util.ResourceBundle
561 <link xmlns:xlink="http://www.w3.org/1999/xlink"
562 xlink:href="http://www.gnu.org/software/gettext">
563 GNU gettext tools, version 0.10.38, Native Language Support