OSDN Git Service

Add composite-type attributes to information_schema.element_types view
[pg-rex/syncrep.git] / doc / src / sgml / nls.sgml
1 <!-- doc/src/sgml/nls.sgml -->
2
3 <chapter id="nls">
4  <chapterinfo>
5   <author>
6    <firstname>Peter</firstname>
7    <surname>Eisentraut</surname>
8   </author>
9  </chapterinfo>
10
11  <title>Native Language Support</title>
12
13  <sect1 id="nls-translator">
14   <title>For the Translator</title>
15
16   <para>
17    <productname>PostgreSQL</>
18    programs (server and client) can issue their messages in
19    your favorite language &mdash; if the messages have been translated.
20    Creating and maintaining translated message sets needs the help of
21    people who speak their own language well and want to contribute to
22    the <productname>PostgreSQL</> effort.  You do not have to be a
23    programmer at all
24    to do this.  This section explains how to help.
25   </para>
26
27   <sect2>
28    <title>Requirements</title>
29
30    <para>
31     We won't judge your language skills &mdash; this section is about
32     software tools.  Theoretically, you only need a text editor.  But
33     this is only in the unlikely event that you do not want to try out
34     your translated messages.  When you configure your source tree, be
35     sure to use the <option>--enable-nls</option> option.  This will
36     also check for the <application>libintl</application> library and the
37     <filename>msgfmt</filename> program, which all end users will need
38     anyway.  To try out your work, follow the applicable portions of
39     the installation instructions.
40    </para>
41
42    <para>
43     If you want to start a new translation effort or want to do a
44     message catalog merge (described later), you will need the
45     programs <filename>xgettext</filename> and
46     <filename>msgmerge</filename>, respectively, in a GNU-compatible
47     implementation.  Later, we will try to arrange it so that if you
48     use a packaged source distribution, you won't need
49     <filename>xgettext</filename>.  (If working from Git, you will still need
50     it.)  <application>GNU Gettext 0.10.36</application> or later is currently recommended.
51    </para>
52
53    <para>
54     Your local gettext implementation should come with its own
55     documentation.  Some of that is probably duplicated in what
56     follows, but for additional details you should look there.
57    </para>
58   </sect2>
59
60   <sect2>
61    <title>Concepts</title>
62
63    <para>
64     The pairs of original (English) messages and their (possibly)
65     translated equivalents are kept in <firstterm>message
66     catalogs</firstterm>, one for each program (although related
67     programs can share a message catalog) and for each target
68     language.  There are two file formats for message catalogs:  The
69     first is the <quote>PO</quote> file (for Portable Object), which
70     is a plain text file with special syntax that translators edit.
71     The second is the <quote>MO</quote> file (for Machine Object),
72     which is a binary file generated from the respective PO file and
73     is used while the internationalized program is run.  Translators
74     do not deal with MO files; in fact hardly anyone does.
75    </para>
76
77    <para>
78     The extension of the message catalog file is to no surprise either
79     <filename>.po</filename> or <filename>.mo</filename>.  The base
80     name is either the name of the program it accompanies, or the
81     language the file is for, depending on the situation.  This is a
82     bit confusing.  Examples are <filename>psql.po</filename> (PO file
83     for psql) or <filename>fr.mo</filename> (MO file in French).
84    </para>
85
86    <para>
87     The file format of the PO files is illustrated here:
88 <programlisting>
89 # comment
90
91 msgid "original string"
92 msgstr "translated string"
93
94 msgid "more original"
95 msgstr "another translated"
96 "string can be broken up like this"
97
98 ...
99 </programlisting>
100     The msgid's are extracted from the program source.  (They need not
101     be, but this is the most common way.)  The msgstr lines are
102     initially empty and are filled in with useful strings by the
103     translator.  The strings can contain C-style escape characters and
104     can be continued across lines as illustrated.  (The next line must
105     start at the beginning of the line.)
106    </para>
107
108    <para>
109     The # character introduces a comment.  If whitespace immediately
110     follows the # character, then this is a comment maintained by the
111     translator.  There can also be automatic comments, which have a
112     non-whitespace character immediately following the #.  These are
113     maintained by the various tools that operate on the PO files and
114     are intended to aid the translator.
115 <programlisting>
116 #. automatic comment
117 #: filename.c:1023
118 #, flags, flags
119 </programlisting>
120     The #. style comments are extracted from the source file where the
121     message is used.  Possibly the programmer has inserted information
122     for the translator, such as about expected alignment.  The #:
123     comment indicates the exact location(s) where the message is used
124     in the source.  The translator need not look at the program
125     source, but he can if there is doubt about the correct
126     translation.  The #, comments contain flags that describe the
127     message in some way.  There are currently two flags:
128     <literal>fuzzy</literal> is set if the message has possibly been
129     outdated because of changes in the program source.  The translator
130     can then verify this and possibly remove the fuzzy flag.  Note
131     that fuzzy messages are not made available to the end user.  The
132     other flag is <literal>c-format</literal>, which indicates that
133     the message is a <function>printf</function>-style format
134     template.  This means that the translation should also be a format
135     string with the same number and type of placeholders.  There are
136     tools that can verify this, which key off the c-format flag.
137    </para>
138   </sect2>
139
140   <sect2>
141    <title>Creating and Maintaining Message Catalogs</title>
142
143    <para>
144     OK, so how does one create a <quote>blank</quote> message
145     catalog?  First, go into the directory that contains the program
146     whose messages you want to translate.  If there is a file
147     <filename>nls.mk</filename>, then this program has been prepared
148     for translation.
149    </para>
150
151    <para>
152     If there are already some <filename>.po</filename> files, then
153     someone has already done some translation work.  The files are
154     named <filename><replaceable>language</replaceable>.po</filename>,
155     where <replaceable>language</replaceable> is the
156     <ulink url="http://www.loc.gov/standards/iso639-2/php/English_list.php">
157     ISO 639-1 two-letter language code (in lower case)</ulink>, e.g.,
158     <filename>fr.po</filename> for French.  If there is really a need
159     for more than one translation effort per language then the files
160     can also be named
161     <filename><replaceable>language</replaceable>_<replaceable>region</replaceable>.po</filename>
162     where <replaceable>region</replaceable> is the
163     <ulink url="http://www.iso.org/iso/english_country_names_and_code_elements">
164     ISO 3166-1 two-letter country code (in upper case)</ulink>,
165     e.g.,
166     <filename>pt_BR.po</filename> for Portuguese in Brazil.  If you
167     find the language you wanted you can just start working on that
168     file.
169    </para>
170
171    <para>
172     If you need to start a new translation effort, then first run the
173     command:
174 <programlisting>
175 gmake init-po
176 </programlisting>
177     This will create a file
178     <filename><replaceable>progname</replaceable>.pot</filename>.
179     (<filename>.pot</filename> to distinguish it from PO files that
180     are <quote>in production</quote>. The <literal>T</> stands for
181     <quote>template</>.)
182     Copy this file to
183     <filename><replaceable>language</replaceable>.po</filename> and
184     edit it.  To make it known that the new language is available,
185     also edit the file <filename>nls.mk</filename> and add the
186     language (or language and country) code to the line that looks like:
187 <programlisting>
188 AVAIL_LANGUAGES := de fr
189 </programlisting>
190     (Other languages can appear, of course.)
191    </para>
192
193    <para>
194     As the underlying program or library changes, messages might be
195     changed or added by the programmers.  In this case you do not need
196     to start from scratch.  Instead, run the command:
197 <programlisting>
198 gmake update-po
199 </programlisting>
200     which will create a new blank message catalog file (the pot file
201     you started with) and will merge it with the existing PO files.
202     If the merge algorithm is not sure about a particular message it
203     marks it <quote>fuzzy</quote> as explained above.  The new PO file
204     is saved with a <filename>.po.new</filename> extension.
205    </para>
206   </sect2>
207
208   <sect2>
209    <title>Editing the PO Files</title>
210
211    <para>
212     The PO files can be edited with a regular text editor.  The
213     translator should only change the area between the quotes after
214     the msgstr directive, add comments, and alter the fuzzy flag.
215     There is (unsurprisingly) a PO mode for Emacs, which I find quite
216     useful.
217    </para>
218
219    <para>
220     The PO files need not be completely filled in.  The software will
221     automatically fall back to the original string if no translation
222     (or an empty translation) is available.  It is no problem to
223     submit incomplete translations for inclusions in the source tree;
224     that gives room for other people to pick up your work.  However,
225     you are encouraged to give priority to removing fuzzy entries
226     after doing a merge.  Remember that fuzzy entries will not be
227     installed; they only serve as reference for what might be the right
228     translation.
229    </para>
230
231    <para>
232     Here are some things to keep in mind while editing the
233     translations:
234     <itemizedlist>
235      <listitem>
236       <para>
237        Make sure that if the original ends with a newline, the
238        translation does, too.  Similarly for tabs, etc.
239       </para>
240      </listitem>
241
242      <listitem>
243       <para>
244        If the original is a <function>printf</> format string, the translation
245        also needs to be.  The translation also needs to have the same
246        format specifiers in the same order.  Sometimes the natural
247        rules of the language make this impossible or at least awkward.
248        In that case you can modify the format specifiers like this:
249 <programlisting>
250 msgstr "Die Datei %2$s hat %1$u Zeichen."
251 </programlisting>
252        Then the first placeholder will actually use the second
253        argument from the list.  The
254        <literal><replaceable>digits</replaceable>$</literal> needs to
255        follow the % immediately, before any other format manipulators.
256        (This feature really exists in the <function>printf</function>
257        family of functions.  You might not have heard of it before because
258        there is little use for it outside of message
259        internationalization.)
260       </para>
261      </listitem>
262
263      <listitem>
264       <para>
265        If the original string contains a linguistic mistake, report
266        that (or fix it yourself in the program source) and translate
267        normally.  The corrected string can be merged in when the
268        program sources have been updated.  If the original string
269        contains a factual mistake, report that (or fix it yourself)
270        and do not translate it.  Instead, you can mark the string with
271        a comment in the PO file.
272       </para>
273      </listitem>
274
275      <listitem>
276       <para>
277        Maintain the style and tone of the original string.
278        Specifically, messages that are not sentences (<literal>cannot
279        open file %s</literal>) should probably not start with a
280        capital letter (if your language distinguishes letter case) or
281        end with a period (if your language uses punctuation marks).
282        It might help to read <xref linkend="error-style-guide">.
283       </para>
284      </listitem>
285
286      <listitem>
287       <para>
288        If you don't know what a message means, or if it is ambiguous,
289        ask on the developers' mailing list.  Chances are that English
290        speaking end users might also not understand it or find it
291        ambiguous, so it's best to improve the message.
292       </para>
293      </listitem>
294
295     </itemizedlist>
296    </para>
297   </sect2>
298
299  </sect1>
300
301
302  <sect1 id="nls-programmer">
303   <title>For the Programmer</title>
304
305   <sect2 id="nls-mechanics">
306    <title>Mechanics</title>
307
308   <para>
309    This section describes how to implement native language support in a
310    program or library that is part of the
311    <productname>PostgreSQL</> distribution.
312    Currently, it only applies to C programs.
313   </para>
314
315   <procedure>
316    <title>Adding NLS Support to a Program</title>
317
318    <step>
319     <para>
320      Insert this code into the start-up sequence of the program:
321 <programlisting>
322 #ifdef ENABLE_NLS
323 #include &lt;locale.h&gt;
324 #endif
325
326 ...
327
328 #ifdef ENABLE_NLS
329 setlocale(LC_ALL, "");
330 bindtextdomain("<replaceable>progname</replaceable>", LOCALEDIR);
331 textdomain("<replaceable>progname</replaceable>");
332 #endif
333 </programlisting>
334      (The <replaceable>progname</replaceable> can actually be chosen
335      freely.)
336     </para>
337    </step>
338
339    <step>
340     <para>
341      Wherever a message that is a candidate for translation is found,
342      a call to <function>gettext()</function> needs to be inserted.  E.g.:
343 <programlisting>
344 fprintf(stderr, "panic level %d\n", lvl);
345 </programlisting>
346      would be changed to:
347 <programlisting>
348 fprintf(stderr, gettext("panic level %d\n"), lvl);
349 </programlisting>
350      (<symbol>gettext</symbol> is defined as a no-op if NLS support is
351      not configured.)
352     </para>
353
354     <para>
355      This tends to add a lot of clutter.  One common shortcut is to use:
356 <programlisting>
357 #define _(x) gettext(x)
358 </programlisting>
359      Another solution is feasible if the program does much of its
360      communication through one or a few functions, such as
361      <function>ereport()</function> in the backend.  Then you make this
362      function call <function>gettext</function> internally on all
363      input strings.
364     </para>
365    </step>
366
367    <step>
368     <para>
369      Add a file <filename>nls.mk</filename> in the directory with the
370      program sources.  This file will be read as a makefile.  The
371      following variable assignments need to be made here:
372
373      <variablelist>
374       <varlistentry>
375        <term><varname>CATALOG_NAME</varname></term>
376
377        <listitem>
378         <para>
379          The program name, as provided in the
380          <function>textdomain()</function> call.
381         </para>
382        </listitem>
383       </varlistentry>
384
385       <varlistentry>
386        <term><varname>AVAIL_LANGUAGES</varname></term>
387
388        <listitem>
389         <para>
390          List of provided translations &mdash; initially empty.
391         </para>
392        </listitem>
393       </varlistentry>
394
395       <varlistentry>
396        <term><varname>GETTEXT_FILES</varname></term>
397
398        <listitem>
399         <para>
400          List of files that contain translatable strings, i.e., those
401          marked with <function>gettext</function> or an alternative
402          solution.  Eventually, this will include nearly all source
403          files of the program.  If this list gets too long you can
404          make the first <quote>file</quote> be a <literal>+</literal>
405          and the second word be a file that contains one file name per
406          line.
407         </para>
408        </listitem>
409       </varlistentry>
410
411       <varlistentry>
412        <term><varname>GETTEXT_TRIGGERS</varname></term>
413
414        <listitem>
415         <para>
416          The tools that generate message catalogs for the translators
417          to work on need to know what function calls contain
418          translatable strings.  By default, only
419          <function>gettext()</function> calls are known.  If you used
420          <function>_</function> or other identifiers you need to list
421          them here.  If the translatable string is not the first
422          argument, the item needs to be of the form
423          <literal>func:2</literal> (for the second argument).
424          If you have a function that supports pluralized messages,
425          the item should look like <literal>func:1,2</literal>
426          (identifying the singular and plural message arguments).
427         </para>
428        </listitem>
429       </varlistentry>
430      </variablelist>
431     </para>
432    </step>
433
434   </procedure>
435
436   <para>
437    The build system will automatically take care of building and
438    installing the message catalogs.
439   </para>
440   </sect2>
441
442   <sect2 id="nls-guidelines">
443    <title>Message-writing Guidelines</title>
444
445   <para>
446    Here are some guidelines for writing messages that are easily
447    translatable.
448
449    <itemizedlist>
450     <listitem>
451      <para>
452       Do not construct sentences at run-time, like:
453 <programlisting>
454 printf("Files were %s.\n", flag ? "copied" : "removed");
455 </programlisting>
456       The word order within the sentence might be different in other
457       languages.  Also, even if you remember to call <function>gettext()</> on
458       each fragment, the fragments might not translate well separately.  It's
459       better to duplicate a little code so that each message to be
460       translated is a coherent whole.  Only numbers, file names, and
461       such-like run-time variables should be inserted at run time into
462       a message text.
463      </para>
464     </listitem>
465
466     <listitem>
467      <para>
468       For similar reasons, this won't work:
469 <programlisting>
470 printf("copied %d file%s", n, n!=1 ? "s" : "");
471 </programlisting>
472       because it assumes how the plural is formed.  If you figured you
473       could solve it like this:
474 <programlisting>
475 if (n==1)
476     printf("copied 1 file");
477 else
478     printf("copied %d files", n):
479 </programlisting>
480       then be disappointed.  Some languages have more than two forms,
481       with some peculiar rules.  It's often best to design the message
482       to avoid the issue altogether, for instance like this:
483 <programlisting>
484 printf("number of copied files: %d", n);
485 </programlisting>
486      </para>
487
488      <para>
489       If you really want to construct a properly pluralized message,
490       there is support for this, but it's a bit awkward.  When generating
491       a primary or detail error message in <function>ereport()</>, you can
492       write something like this:
493 <programlisting>
494 errmsg_plural("copied %d file",
495               "copied %d files",
496               n,
497               n)
498 </programlisting>
499       The first argument is the format string appropriate for English
500       singular form, the second is the format string appropriate for
501       English plural form, and the third is the integer control value
502       that determines which plural form to use.  Subsequent arguments
503       are formatted per the format string as usual.  (Normally, the
504       pluralization control value will also be one of the values to be
505       formatted, so it has to be written twice.)  In English it only
506       matters whether <replaceable>n</> is 1 or not 1, but in other
507       languages there can be many different plural forms.  The translator
508       sees the two English forms as a group and has the opportunity to
509       supply multiple substitute strings, with the appropriate one being
510       selected based on the run-time value of <replaceable>n</>.
511      </para>
512
513      <para>
514       If you need to pluralize a message that isn't going directly to an
515       <function>errmsg</> or <function>errdetail</> report, you have to use
516       the underlying function <function>ngettext</>.  See the gettext
517       documentation.
518      </para>
519     </listitem>
520
521     <listitem>
522      <para>
523       If you want to communicate something to the translator, such as
524       about how a message is intended to line up with other output,
525       precede the occurrence of the string with a comment that starts
526       with <literal>translator</literal>, e.g.:
527 <programlisting>
528 /* translator: This message is not what it seems to be. */
529 </programlisting>
530       These comments are copied to the message catalog files so that
531       the translators can see them.
532      </para>
533     </listitem>
534    </itemizedlist>
535   </para>
536   </sect2>
537  </sect1>
538
539 </chapter>