1 <section xmlns="http://docbook.org/ns/docbook" version="5.0"
2 xml:id="std.localization.facet.messages" xreflabel="Messages">
3 <?dbhtml filename="messages.html"?>
5 <info><title>messages</title>
7 <keyword>ISO C++</keyword>
8 <keyword>messages</keyword>
15 The <classname>std::messages</classname> facet implements message retrieval functionality
16 equivalent to Java's <classname>java.text.MessageFormat</classname> using either GNU <function>gettext</function>
17 or IEEE 1003.1-200 functions.
20 <section xml:id="facet.messages.req"><info><title>Requirements</title></info>
24 The <classname>std::messages</classname> facet is probably the most vaguely defined facet in
25 the standard library. It's assumed that this facility was built into
26 the standard library in order to convert string literals from one
27 locale to the other. For instance, converting the "C" locale's
28 <code>const char* c = "please"</code> to a German-localized <code>"bitte"</code>
29 during program execution.
34 22.2.7.1 - Template class messages [lib.locale.messages]
39 This class has three public member functions, which directly
40 correspond to three protected virtual member functions.
44 The public member functions are:
48 <code>catalog open(const string&, const locale&) const</code>
52 <code>string_type get(catalog, int, int, const string_type&) const</code>
56 <code>void close(catalog) const</code>
60 While the virtual functions are:
64 <code>catalog do_open(const string& name, const locale& loc) const</code>
69 -1- Returns: A value that may be passed to <code>get()</code> to retrieve a
70 message, from the message catalog identified by the string <code>name</code>
71 according to an implementation-defined mapping. The result can be used
72 until it is passed to <code>close()</code>. Returns a value less than 0 if no such
73 catalog can be opened.
79 <code>string_type do_get(catalog cat, int set , int msgid, const string_type& dfault) const</code>
84 -3- Requires: A catalog <code>cat</code> obtained from <code>open()</code> and not yet closed.
85 -4- Returns: A message identified by arguments <code>set</code>, <code>msgid</code>, and <code>dfault</code>,
86 according to an implementation-defined mapping. If no such message can
87 be found, returns <code>dfault</code>.
93 <code>void do_close(catalog cat) const</code>
98 -5- Requires: A catalog cat obtained from <code>open()</code> and not yet closed.
99 -6- Effects: Releases unspecified resources associated with <code>cat</code>.
100 -7- Notes: The limit on such resources, if any, is implementation-defined.
108 <section xml:id="facet.messages.design"><info><title>Design</title></info>
112 A couple of notes on the standard.
116 First, why is <code>messages_base::catalog</code> specified as a typedef
117 to int? This makes sense for implementations that use
118 <code>catopen</code> and define <code>nl_catd</code> as int, but not for
119 others. Fortunately, it's not heavily used and so only a minor irritant.
120 This has been reported as a possible defect in the standard (LWG 2028).
124 Second, by making the member functions <code>const</code>, it is
125 impossible to save state in them. Thus, storing away information used
126 in the 'open' member function for use in 'get' is impossible. This is
131 The 'open' member function in particular seems to be oddly
132 designed. The signature seems quite peculiar. Why specify a <code>const
133 string& </code> argument, for instance, instead of just <code>const
134 char*</code>? Or, why specify a <code>const locale&</code> argument that is
135 to be used in the 'get' member function? How, exactly, is this locale
136 argument useful? What was the intent? It might make sense if a locale
137 argument was associated with a given default message string in the
138 'open' member function, for instance. Quite murky and unclear, on
143 Lastly, it seems odd that messages, which explicitly require code
144 conversion, don't use the codecvt facet. Because the messages facet
145 has only one template parameter, it is assumed that ctype, and not
146 codecvt, is to be used to convert between character sets.
150 It is implicitly assumed that the locale for the default message
151 string in 'get' is in the "C" locale. Thus, all source code is assumed
152 to be written in English, so translations are always from "en_US" to
153 other, explicitly named locales.
158 <section xml:id="facet.messages.impl"><info><title>Implementation</title></info>
161 <section xml:id="messages.impl.models"><info><title>Models</title></info>
164 This is a relatively simple class, on the face of it. The standard
165 specifies very little in concrete terms, so generic
166 implementations that are conforming yet do very little are the
167 norm. Adding functionality that would be useful to programmers and
168 comparable to Java's java.text.MessageFormat takes a bit of work,
169 and is highly dependent on the capabilities of the underlying
174 Three different mechanisms have been provided, selectable via
184 This model does very little, and is what is used by default.
193 The gnu model is complete and fully tested. It's based on the
194 GNU gettext package, which is part of glibc. It uses the
195 functions <code>textdomain, bindtextdomain, gettext</code> to
196 implement full functionality. Creating message catalogs is a
197 relatively straight-forward process and is lightly documented
198 below, and fully documented in gettext's distributed
208 This is a complete, though untested, implementation based on
209 the IEEE standard. The functions <code>catopen, catgets,
210 catclose</code> are used to retrieve locale-specific messages
211 given the appropriate message catalogs that have been
212 constructed for their use. Note, the script <code>
213 po2msg.sed</code> that is part of the gettext distribution can
214 convert gettext catalogs into catalogs that
215 <code>catopen</code> can use.
221 A new, standards-conformant non-virtual member function signature was
222 added for 'open' so that a directory could be specified with a given
223 message catalog. This simplifies calling conventions for the gnu
229 <section xml:id="messages.impl.gnu"><info><title>The GNU Model</title></info>
233 The messages facet, because it is retrieving and converting
234 between characters sets, depends on the ctype and perhaps the
235 codecvt facet in a given locale. In addition, underlying "C"
236 library locale support is necessary for more than just the
237 <code>LC_MESSAGES</code> mask: <code>LC_CTYPE</code> is also
238 necessary. To avoid any unpleasantness, all bits of the "C" mask
239 (i.e. <code>LC_ALL</code>) are set before retrieving messages.
243 Making the message catalogs can be initially tricky, but become
244 quite simple with practice. For complete info, see the gettext
245 documentation. Here's an idea of what is required:
251 Make a source file with the required string literals that need
252 to be translated. See <code>intl/string_literals.cc</code> for
259 Make initial catalog (see "4 Making the PO Template File" from
260 the gettext docs).</para>
262 <code> xgettext --c++ --debug string_literals.cc -o libstdc++.pot </code>
267 <para>Make language and country-specific locale catalogs.</para>
269 <code>cp libstdc++.pot fr_FR.po</code>
272 <code>cp libstdc++.pot de_DE.po</code>
278 Edit localized catalogs in emacs so that strings are
282 <code>emacs fr_FR.po</code>
287 <para>Make the binary mo files.</para>
289 <code>msgfmt fr_FR.po -o fr_FR.mo</code>
292 <code>msgfmt de_DE.po -o de_DE.mo</code>
297 <para>Copy the binary files into the correct directory structure.</para>
299 <code>cp fr_FR.mo (dir)/fr_FR/LC_MESSAGES/libstdc++.mo</code>
302 <code>cp de_DE.mo (dir)/de_DE/LC_MESSAGES/libstdc++.mo</code>
307 <para>Use the new message catalogs.</para>
309 <code>locale loc_de("de_DE");</code>
313 use_facet<messages<char> >(loc_de).open("libstdc++", locale(), dir);
322 <section xml:id="facet.messages.use"><info><title>Use</title></info>
325 A simple example using the GNU model of message conversion.
329 #include <iostream>
330 #include <locale>
335 typedef messages<char>::catalog catalog;
337 "/mnt/egcs/build/i686-pc-linux-gnu/libstdc++/po/share/locale";
338 const locale loc_de("de_DE");
339 const messages<char>& mssg_de = use_facet<messages<char> >(loc_de);
341 catalog cat_de = mssg_de.open("libstdc++", loc_de, dir);
342 string s01 = mssg_de.get(cat_de, 0, 0, "please");
343 string s02 = mssg_de.get(cat_de, 0, 0, "thank you");
344 cout << "please in german:" << s01 << '\n';
345 cout << "thank you in german:" << s02 << '\n';
346 mssg_de.close(cat_de);
352 <section xml:id="facet.messages.future"><info><title>Future</title></info>
358 Things that are sketchy, or remain unimplemented:
363 _M_convert_from_char, _M_convert_to_char are in flux,
364 depending on how the library ends up doing character set
365 conversions. It might not be possible to do a real character
366 set based conversion, due to the fact that the template
367 parameter for messages is not enough to instantiate the
368 codecvt facet (1 supplied, need at least 2 but would prefer
375 There are issues with gettext needing the global locale set
376 to extract a message. This dependence on the global locale
377 makes the current "gnu" model non MT-safe. Future versions
378 of glibc, i.e. glibc 2.3.x will fix this, and the C++ library
379 bits are already in place.
387 Development versions of the GNU "C" library, glibc 2.3 will allow
388 a more efficient, MT implementation of std::messages, and will
389 allow the removal of the _M_name_messages data member. If this is
390 done, it will change the library ABI. The C++ parts to support
391 glibc 2.3 have already been coded, but are not in use: once this
392 version of the "C" library is released, the marked parts of the
393 messages implementation can be switched over to the new "C"
394 library functionality.
399 At some point in the near future, std::numpunct will probably use
400 std::messages facilities to implement truename/falsename
401 correctly. This is currently not done, but entries in
402 libstdc++.pot have already been made for "true" and "false" string
403 literals, so all that remains is the std::numpunct coding and the
404 configure/make hassles to make the installed library search its
405 own catalog. Currently the libstdc++.mo catalog is only searched
406 for the testsuite cases involving messages members.
411 <para> The following member functions:</para>
416 open(const basic_string<char>& __s, const locale& __loc) const
423 open(const basic_string<char>&, const locale&, const char*) const;
428 Don't actually return a "value less than 0 if no such catalog
429 can be opened" as required by the standard in the "gnu"
430 model. As of this writing, it is unknown how to query to see
431 if a specified message catalog exists using the gettext
439 <bibliography xml:id="facet.messages.biblio"><info><title>Bibliography</title></info>
446 <author><personname><surname>McGrath</surname><firstname>Roland</firstname></personname></author>
447 <author><personname><surname>Drepper</surname><firstname>Ulrich</firstname></personname></author>
452 <pagenums>Chapters 6 Character Set Handling, and 7 Locales and Internationalization
460 <author><personname><surname>Drepper</surname><firstname>Ulrich</firstname></personname></author>
469 ISO/IEC 14882:1998 Programming languages - C++
479 ISO/IEC 9899:1999 Programming languages - C
490 <link xmlns:xlink="http://www.w3.org/1999/xlink"
491 xlink:href="https://pubs.opengroup.org/onlinepubs/9699919799/">
492 System Interface Definitions, Issue 7 (IEEE Std. 1003.1-2008)
498 The Open Group/The Institute of Electrical and Electronics
506 The C++ Programming Language, Special Edition
508 <author><personname><surname>Stroustrup</surname><firstname>Bjarne</firstname></personname></author>
511 <holder>Addison Wesley, Inc.</holder>
513 <pagenums>Appendix D</pagenums>
523 Standard C++ IOStreams and Locales
526 Advanced Programmer's Guide and Reference
528 <author><personname><surname>Langer</surname><firstname>Angelika</firstname></personname></author>
529 <author><personname><surname>Kreft</surname><firstname>Klaus</firstname></personname></author>
532 <holder>Addison Wesley Longman, Inc.</holder>
536 Addison Wesley Longman
543 <link xmlns:xlink="http://www.w3.org/1999/xlink"
544 xlink:href="https://docs.oracle.com/en/java/">
545 API Specifications, Java Platform
549 <pagenums>java.util.Properties, java.text.MessageFormat,
550 java.util.Locale, java.util.ResourceBundle
557 <link xmlns:xlink="http://www.w3.org/1999/xlink"
558 xlink:href="https://www.gnu.org/software/gettext/">
559 GNU gettext tools, version 0.10.38, Native Language Support