OSDN Git Service

PR libstdc++/50834
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / doc / xml / manual / messages.xml
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"?>
4
5 <info><title>messages</title>
6   <keywordset>
7     <keyword>
8       ISO C++
9     </keyword>
10     <keyword>
11       messages
12     </keyword>
13   </keywordset>
14 </info>
15
16
17
18 <para>
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.
22 </para>
23
24 <section xml:id="facet.messages.req"><info><title>Requirements</title></info>
25
26
27 <para>
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.
34 </para>
35
36 <blockquote>
37 <para>
38 22.2.7.1 - Template class messages [lib.locale.messages]
39 </para>
40 </blockquote>
41
42 <para>
43 This class has three public member functions, which directly
44 correspond to three protected virtual member functions.
45 </para>
46
47 <para>
48 The public member functions are:
49 </para>
50
51 <para>
52 <code>catalog open(const string&amp;, const locale&amp;) const</code>
53 </para>
54
55 <para>
56 <code>string_type get(catalog, int, int, const string_type&amp;) const</code>
57 </para>
58
59 <para>
60 <code>void close(catalog) const</code>
61 </para>
62
63 <para>
64 While the virtual functions are:
65 </para>
66
67 <para>
68 <code>catalog do_open(const string&amp;, const locale&amp;) const</code>
69 </para>
70 <blockquote>
71 <para>
72 <emphasis>
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.
78 </emphasis>
79 </para>
80 </blockquote>
81
82 <para>
83 <code>string_type do_get(catalog, int, int, const string_type&amp;) const</code>
84 </para>
85 <blockquote>
86 <para>
87 <emphasis>
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.
92 </emphasis>
93 </para>
94 </blockquote>
95
96 <para>
97 <code>void do_close(catalog) const</code>
98 </para>
99 <blockquote>
100 <para>
101 <emphasis>
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.
105 </emphasis>
106 </para>
107 </blockquote>
108
109
110 </section>
111
112 <section xml:id="facet.messages.design"><info><title>Design</title></info>
113
114
115 <para>
116 A couple of notes on the standard.
117 </para>
118
119 <para>
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.
124 </para>
125
126 <para>
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
130 unfortunate.
131 </para>
132
133 <para>
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&amp; </code> argument, for instance, instead of just <code>const
137 char*</code>? Or, why specify a <code>const locale&amp;</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
142 reflection.
143 </para>
144
145 <para>
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.
150 </para>
151
152 <para>
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.
157 </para>
158
159 </section>
160
161 <section xml:id="facet.messages.impl"><info><title>Implementation</title></info>
162
163
164   <section xml:id="messages.impl.models"><info><title>Models</title></info>
165   
166   <para>
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
173     operating system.
174   </para>
175
176   <para>
177     Three different mechanisms have been provided, selectable via
178     configure flags:
179   </para>
180
181 <itemizedlist>
182    <listitem>
183      <para>
184        generic
185      </para>
186      <para>
187        This model does very little, and is what is used by default.
188      </para>
189    </listitem>
190
191    <listitem>
192      <para>
193        gnu
194      </para>
195      <para>
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
202        documentation.
203      </para>
204    </listitem>
205
206    <listitem>
207      <para>
208        ieee_1003.1-200x
209      </para>
210      <para>
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.
219    </para>
220    </listitem>
221 </itemizedlist>
222
223 <para>
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
227 model.
228 </para>
229
230   </section>
231
232   <section xml:id="messages.impl.gnu"><info><title>The GNU Model</title></info>
233   
234
235   <para>
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.
243   </para>
244
245   <para>
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:
249   </para>
250
251 <itemizedlist>
252    <listitem>
253      <para>
254        Make a source file with the required string literals that need
255        to be translated. See <code>intl/string_literals.cc</code> for
256        an example.
257      </para>
258    </listitem>
259
260    <listitem>
261      <para>
262        Make initial catalog (see "4 Making the PO Template File" from
263        the gettext docs).</para>
264    <para>
265    <code> xgettext --c++ --debug string_literals.cc -o libstdc++.pot </code>
266    </para>
267    </listitem>
268
269    <listitem>
270      <para>Make language and country-specific locale catalogs.</para>
271    <para>
272    <code>cp libstdc++.pot fr_FR.po</code>
273    </para>
274    <para>
275    <code>cp libstdc++.pot de_DE.po</code>
276    </para>
277    </listitem>
278
279    <listitem>
280      <para>
281        Edit localized catalogs in emacs so that strings are
282        translated.
283      </para>
284    <para>
285    <code>emacs fr_FR.po</code>
286    </para>
287    </listitem>
288
289    <listitem>
290      <para>Make the binary mo files.</para>
291    <para>
292    <code>msgfmt fr_FR.po -o fr_FR.mo</code>
293    </para>
294    <para>
295    <code>msgfmt de_DE.po -o de_DE.mo</code>
296    </para>
297    </listitem>
298
299    <listitem>
300      <para>Copy the binary files into the correct directory structure.</para>
301    <para>
302    <code>cp fr_FR.mo (dir)/fr_FR/LC_MESSAGES/libstdc++.mo</code>
303    </para>
304    <para>
305    <code>cp de_DE.mo (dir)/de_DE/LC_MESSAGES/libstdc++.mo</code>
306    </para>
307    </listitem>
308
309    <listitem>
310      <para>Use the new message catalogs.</para>
311    <para>
312    <code>locale loc_de("de_DE");</code>
313    </para>
314    <para>
315    <code>
316    use_facet&lt;messages&lt;char&gt; &gt;(loc_de).open("libstdc++", locale(), dir);
317    </code>
318    </para>
319    </listitem>
320 </itemizedlist>
321
322   </section>
323 </section>
324
325 <section xml:id="facet.messages.use"><info><title>Use</title></info>
326
327  <para>
328    A simple example using the GNU model of message conversion.
329  </para>
330
331 <programlisting>
332 #include &lt;iostream&gt;
333 #include &lt;locale&gt;
334 using namespace std;
335
336 void test01()
337 {
338   typedef messages&lt;char&gt;::catalog catalog;
339   const char* dir =
340   "/mnt/egcs/build/i686-pc-linux-gnu/libstdc++/po/share/locale";
341   const locale loc_de("de_DE");
342   const messages&lt;char&gt;&amp; mssg_de = use_facet&lt;messages&lt;char&gt; &gt;(loc_de);
343
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 &lt;&lt; "please in german:" &lt;&lt; s01 &lt;&lt; '\n';
348   cout &lt;&lt; "thank you in german:" &lt;&lt; s02 &lt;&lt; '\n';
349   mssg_de.close(cat_de);
350 }
351 </programlisting>
352
353 </section>
354
355 <section xml:id="facet.messages.future"><info><title>Future</title></info>
356
357
358 <itemizedlist>
359 <listitem>
360   <para>
361     Things that are sketchy, or remain unimplemented:
362   </para>
363    <itemizedlist>
364       <listitem>
365         <para>
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
372           3).
373         </para>
374       </listitem>
375
376       <listitem>
377         <para>
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.
383         </para>
384       </listitem>
385    </itemizedlist>
386 </listitem>
387
388 <listitem>
389   <para>
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.
398   </para>
399 </listitem>
400 <listitem>
401   <para>
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.
410   </para>
411 </listitem>
412
413 <listitem>
414   <para> The following member functions:</para>
415
416    <para>
417    <code>
418         catalog
419         open(const basic_string&lt;char&gt;&amp; __s, const locale&amp; __loc) const
420    </code>
421    </para>
422
423    <para>
424    <code>
425    catalog
426    open(const basic_string&lt;char&gt;&amp;, const locale&amp;, const char*) const;
427    </code>
428    </para>
429
430    <para>
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
435    package.
436    </para>
437 </listitem>
438 </itemizedlist>
439
440 </section>
441
442 <bibliography xml:id="facet.messages.biblio"><info><title>Bibliography</title></info>
443
444
445   <biblioentry>
446     <citetitle>
447       The GNU C Library
448     </citetitle>
449     <author><personname><surname>McGrath</surname><firstname>Roland</firstname></personname></author>
450     <author><personname><surname>Drepper</surname><firstname>Ulrich</firstname></personname></author>
451     <copyright>
452       <year>2007</year>
453       <holder>FSF</holder>
454     </copyright>
455     <pagenums>Chapters 6 Character Set Handling, and 7 Locales and Internationalization
456     </pagenums>
457   </biblioentry>
458
459   <biblioentry>
460     <citetitle>
461       Correspondence
462     </citetitle>
463     <author><personname><surname>Drepper</surname><firstname>Ulrich</firstname></personname></author>
464     <copyright>
465       <year>2002</year>
466       <holder/>
467     </copyright>
468   </biblioentry>
469
470   <biblioentry>
471     <citetitle>
472       ISO/IEC 14882:1998 Programming languages - C++
473     </citetitle>
474     <copyright>
475       <year>1998</year>
476       <holder>ISO</holder>
477     </copyright>
478   </biblioentry>
479
480   <biblioentry>
481     <citetitle>
482       ISO/IEC 9899:1999 Programming languages - C
483     </citetitle>
484
485     <copyright>
486       <year>1999</year>
487       <holder>ISO</holder>
488     </copyright>
489   </biblioentry>
490
491   <biblioentry>
492       <title>
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)
496         </link>
497       </title>
498         <copyright>
499       <year>2008</year>
500       <holder>
501         The Open Group/The Institute of Electrical and Electronics
502         Engineers, Inc.
503       </holder>
504     </copyright>
505   </biblioentry>
506
507   <biblioentry>
508     <citetitle>
509       The C++ Programming Language, Special Edition
510     </citetitle>
511     <author><personname><surname>Stroustrup</surname><firstname>Bjarne</firstname></personname></author>
512     <copyright>
513       <year>2000</year>
514       <holder>Addison Wesley, Inc.</holder>
515     </copyright>
516     <pagenums>Appendix D</pagenums>
517     <publisher>
518       <publishername>
519         Addison Wesley
520       </publishername>
521     </publisher>
522   </biblioentry>
523
524   <biblioentry>
525     <citetitle>
526       Standard C++ IOStreams and Locales
527     </citetitle>
528     <subtitle>
529       Advanced Programmer's Guide and Reference
530     </subtitle>
531     <author><personname><surname>Langer</surname><firstname>Angelika</firstname></personname></author>
532     <author><personname><surname>Kreft</surname><firstname>Klaus</firstname></personname></author>
533     <copyright>
534       <year>2000</year>
535       <holder>Addison Wesley Longman, Inc.</holder>
536     </copyright>
537     <publisher>
538       <publishername>
539         Addison Wesley Longman
540       </publishername>
541     </publisher>
542   </biblioentry>
543
544   <biblioentry>
545       <title>
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
549         </link>
550       </title>
551
552     <pagenums>java.util.Properties, java.text.MessageFormat,
553 java.util.Locale, java.util.ResourceBundle
554     </pagenums>
555   </biblioentry>
556
557
558   <biblioentry>
559       <title>
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
563       Library and Tools.
564         </link>
565       </title>
566
567   </biblioentry>
568
569 </bibliography>
570
571 </section>