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