1 <sect1 id="manual.localization.facet.messages" xreflabel="messages">
2 <?dbhtml filename="messages.html"?>
15 <title>messages</title>
18 The std::messages facet implements message retrieval functionality
19 equivalent to Java's java.text.MessageFormat .using either GNU gettext
20 or IEEE 1003.1-200 functions.
23 <sect2 id="facet.messages.req" xreflabel="facet.messages.req">
24 <title>Requirements</title>
27 The std::messages facet is probably the most vaguely defined facet in
28 the standard library. It's assumed that this facility was built into
29 the standard library in order to convert string literals from one
30 locale to the other. For instance, converting the "C" locale's
31 <code>const char* c = "please"</code> to a German-localized <code>"bitte"</code>
32 during program execution.
37 22.2.7.1 - Template class messages [lib.locale.messages]
42 This class has three public member functions, which directly
43 correspond to three protected virtual member functions.
47 The public member functions are:
51 <code>catalog open(const string&, const locale&) const</code>
55 <code>string_type get(catalog, int, int, const string_type&) const</code>
59 <code>void close(catalog) const</code>
63 While the virtual functions are:
67 <code>catalog do_open(const string&, const locale&) const</code>
72 -1- Returns: A value that may be passed to get() to retrieve a
73 message, from the message catalog identified by the string name
74 according to an implementation-defined mapping. The result can be used
75 until it is passed to close(). Returns a value less than 0 if no such
76 catalog can be opened.
82 <code>string_type do_get(catalog, int, int, const string_type&) const</code>
87 -3- Requires: A catalog cat obtained from open() and not yet closed.
88 -4- Returns: A message identified by arguments set, msgid, and dfault,
89 according to an implementation-defined mapping. If no such message can
90 be found, returns dfault.
96 <code>void do_close(catalog) const</code>
101 -5- Requires: A catalog cat obtained from open() and not yet closed.
102 -6- Effects: Releases unspecified resources associated with cat.
103 -7- Notes: The limit on such resources, if any, is implementation-defined.
111 <sect2 id="facet.messages.design" xreflabel="facet.messages.design">
112 <title>Design</title>
115 A couple of notes on the standard.
119 First, why is <code>messages_base::catalog</code> specified as a typedef
120 to int? This makes sense for implementations that use
121 <code>catopen</code>, but not for others. Fortunately, it's not heavily
122 used and so only a minor irritant.
126 Second, by making the member functions <code>const</code>, it is
127 impossible to save state in them. Thus, storing away information used
128 in the 'open' member function for use in 'get' is impossible. This is
133 The 'open' member function in particular seems to be oddly
134 designed. The signature seems quite peculiar. Why specify a <code>const
135 string& </code> argument, for instance, instead of just <code>const
136 char*</code>? Or, why specify a <code>const locale&</code> argument that is
137 to be used in the 'get' member function? How, exactly, is this locale
138 argument useful? What was the intent? It might make sense if a locale
139 argument was associated with a given default message string in the
140 'open' member function, for instance. Quite murky and unclear, on
145 Lastly, it seems odd that messages, which explicitly require code
146 conversion, don't use the codecvt facet. Because the messages facet
147 has only one template parameter, it is assumed that ctype, and not
148 codecvt, is to be used to convert between character sets.
152 It is implicitly assumed that the locale for the default message
153 string in 'get' is in the "C" locale. Thus, all source code is assumed
154 to be written in English, so translations are always from "en_US" to
155 other, explicitly named locales.
160 <sect2 id="facet.messages.impl" xreflabel="facet.messages.impl">
161 <title>Implementation</title>
163 <sect3 id="messages.impl.models" xreflabel="messages.impl.models">
164 <title>Models</title>
166 This is a relatively simple class, on the face of it. The standard
167 specifies very little in concrete terms, so generic
168 implementations that are conforming yet do very little are the
169 norm. Adding functionality that would be useful to programmers and
170 comparable to Java's java.text.MessageFormat takes a bit of work,
171 and is highly dependent on the capabilities of the underlying
176 Three different mechanisms have been provided, selectable via
186 This model does very little, and is what is used by default.
195 The gnu model is complete and fully tested. It's based on the
196 GNU gettext package, which is part of glibc. It uses the
197 functions <code>textdomain, bindtextdomain, gettext</code> to
198 implement full functionality. Creating message catalogs is a
199 relatively straight-forward process and is lightly documented
200 below, and fully documented in gettext's distributed
210 This is a complete, though untested, implementation based on
211 the IEEE standard. The functions <code>catopen, catgets,
212 catclose</code> are used to retrieve locale-specific messages
213 given the appropriate message catalogs that have been
214 constructed for their use. Note, the script <code>
215 po2msg.sed</code> that is part of the gettext distribution can
216 convert gettext catalogs into catalogs that
217 <code>catopen</code> can use.
223 A new, standards-conformant non-virtual member function signature was
224 added for 'open' so that a directory could be specified with a given
225 message catalog. This simplifies calling conventions for the gnu
231 <sect3 id="messages.impl.gnu" xreflabel="messages.impl.gnu">
232 <title>The GNU Model</title>
235 The messages facet, because it is retrieving and converting
236 between characters sets, depends on the ctype and perhaps the
237 codecvt facet in a given locale. In addition, underlying "C"
238 library locale support is necessary for more than just the
239 <code>LC_MESSAGES</code> mask: <code>LC_CTYPE</code> is also
240 necessary. To avoid any unpleasantness, all bits of the "C" mask
241 (i.e. <code>LC_ALL</code>) are set before retrieving messages.
245 Making the message catalogs can be initially tricky, but become
246 quite simple with practice. For complete info, see the gettext
247 documentation. Here's an idea of what is required:
253 Make a source file with the required string literals that need
254 to be translated. See <code>intl/string_literals.cc</code> for
261 Make initial catalog (see "4 Making the PO Template File" from
262 the gettext docs).</para>
264 <code> xgettext --c++ --debug string_literals.cc -o libstdc++.pot </code>
269 <para>Make language and country-specific locale catalogs.</para>
271 <code>cp libstdc++.pot fr_FR.po</code>
274 <code>cp libstdc++.pot de_DE.po</code>
280 Edit localized catalogs in emacs so that strings are
284 <code>emacs fr_FR.po</code>
289 <para>Make the binary mo files.</para>
291 <code>msgfmt fr_FR.po -o fr_FR.mo</code>
294 <code>msgfmt de_DE.po -o de_DE.mo</code>
299 <para>Copy the binary files into the correct directory structure.</para>
301 <code>cp fr_FR.mo (dir)/fr_FR/LC_MESSAGES/libstdc++.mo</code>
304 <code>cp de_DE.mo (dir)/de_DE/LC_MESSAGES/libstdc++.mo</code>
309 <para>Use the new message catalogs.</para>
311 <code>locale loc_de("de_DE");</code>
315 use_facet<messages<char> >(loc_de).open("libstdc++", locale(), dir);
324 <sect2 id="facet.messages.use" xreflabel="facet.messages.use">
327 A simple example using the GNU model of message conversion.
331 #include <iostream>
332 #include <locale>
337 typedef messages<char>::catalog catalog;
339 "/mnt/egcs/build/i686-pc-linux-gnu/libstdc++/po/share/locale";
340 const locale loc_de("de_DE");
341 const messages<char>& mssg_de = use_facet<messages<char> >(loc_de);
343 catalog cat_de = mssg_de.open("libstdc++", loc_de, dir);
344 string s01 = mssg_de.get(cat_de, 0, 0, "please");
345 string s02 = mssg_de.get(cat_de, 0, 0, "thank you");
346 cout << "please in german:" << s01 << '\n';
347 cout << "thank you in german:" << s02 << '\n';
348 mssg_de.close(cat_de);
354 <sect2 id="facet.messages.future" xreflabel="facet.messages.future">
355 <title>Future</title>
360 Things that are sketchy, or remain unimplemented:
365 _M_convert_from_char, _M_convert_to_char are in flux,
366 depending on how the library ends up doing character set
367 conversions. It might not be possible to do a real character
368 set based conversion, due to the fact that the template
369 parameter for messages is not enough to instantiate the
370 codecvt facet (1 supplied, need at least 2 but would prefer
377 There are issues with gettext needing the global locale set
378 to extract a message. This dependence on the global locale
379 makes the current "gnu" model non MT-safe. Future versions
380 of glibc, i.e. glibc 2.3.x will fix this, and the C++ library
381 bits are already in place.
389 Development versions of the GNU "C" library, glibc 2.3 will allow
390 a more efficient, MT implementation of std::messages, and will
391 allow the removal of the _M_name_messages data member. If this is
392 done, it will change the library ABI. The C++ parts to support
393 glibc 2.3 have already been coded, but are not in use: once this
394 version of the "C" library is released, the marked parts of the
395 messages implementation can be switched over to the new "C"
396 library functionality.
401 At some point in the near future, std::numpunct will probably use
402 std::messages facilities to implement truename/falsename
403 correctly. This is currently not done, but entries in
404 libstdc++.pot have already been made for "true" and "false" string
405 literals, so all that remains is the std::numpunct coding and the
406 configure/make hassles to make the installed library search its
407 own catalog. Currently the libstdc++.mo catalog is only searched
408 for the testsuite cases involving messages members.
413 <para> The following member functions:</para>
418 open(const basic_string<char>& __s, const locale& __loc) const
425 open(const basic_string<char>&, const locale&, const char*) const;
430 Don't actually return a "value less than 0 if no such catalog
431 can be opened" as required by the standard in the "gnu"
432 model. As of this writing, it is unknown how to query to see
433 if a specified message catalog exists using the gettext
441 <bibliography id="facet.messages.biblio" xreflabel="facet.messages.biblio">
442 <title>Bibliography</title>
450 <surname>McGrath</surname>
451 <firstname>Roland</firstname>
454 <surname>Drepper</surname>
455 <firstname>Ulrich</firstname>
462 <pagenums>Chapters 6 Character Set Handling, and 7 Locales and Internationalization
473 <surname>Drepper</surname>
474 <firstname>Ulrich</firstname>
485 ISO/IEC 14882:1998 Programming languages - C++
496 ISO/IEC 9899:1999 Programming languages - C
507 System Interface Definitions, Issue 6 (IEEE Std. 1003.1-200x)
513 The Open Group/The Institute of Electrical and Electronics Engineers, Inc.</holder>
517 <ulink url="http://www.opennc.org/austin/docreg.html">
525 The C++ Programming Language, Special Edition
529 <surname>Stroustrup</surname>
530 <firstname>Bjarne</firstname>
535 <holder>Addison Wesley, Inc.</holder>
537 <pagenums>Appendix D</pagenums>
550 Standard C++ IOStreams and Locales
553 Advanced Programmer's Guide and Reference
557 <surname>Langer</surname>
558 <firstname>Angelika</firstname>
562 <surname>Kreft</surname>
563 <firstname>Klaus</firstname>
568 <holder>Addison Wesley Longman, Inc.</holder>
573 Addison Wesley Longman
581 Java 2 Platform, Standard Edition, v 1.3.1 API Specification
583 <pagenums>java.util.Properties, java.text.MessageFormat,
584 java.util.Locale, java.util.ResourceBundle</pagenums>
586 <ulink url="http://java.sun.com/j2se/1.3/docs/api">
593 GNU gettext tools, version 0.10.38, Native Language Support
597 <ulink url="http://sources.redhat.com/gettext">