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