Rewrite update-po target, so that it works less like a shell script and more
[PostgreSQL.git] / doc / src / sgml / nls.sgml
blob52da8ead956b0b165517ce98f89ff90c0f6d9095
1 <!-- $PostgreSQL$ -->
3 <chapter id="nls">
4 <chapterinfo>
5 <author>
6 <firstname>Peter</firstname>
7 <surname>Eisentraut</surname>
8 </author>
9 </chapterinfo>
11 <title>Native Language Support</title>
13 <sect1 id="nls-translator">
14 <title>For the Translator</title>
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>
27 <sect2>
28 <title>Requirements</title>
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>
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>. (From CVS, you will still need
50 it.) <application>GNU Gettext 0.10.36</application> or later is currently recommended.
51 </para>
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>
60 <sect2>
61 <title>Concepts</title>
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>
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>
86 <para>
87 The file format of the PO files is illustrated here:
88 <programlisting>
89 # comment
91 msgid "original string"
92 msgstr "translated string"
94 msgid "more original"
95 msgstr "another translated"
96 "string can be broken up like this"
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>
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>
140 <sect2>
141 <title>Creating and maintaining message catalogs</title>
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>
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://lcweb.loc.gov/standards/iso639-2/englangn.html">
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.din.de/gremien/nas/nabd/iso3166ma/codlstp1/en_listp1.html">
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>
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>
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>
208 <sect2>
209 <title>Editing the PO files</title>
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>
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 what might be the right
228 translation.
229 </para>
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>
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>
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>
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>
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>
295 </itemizedlist>
296 </para>
297 </sect2>
299 </sect1>
302 <sect1 id="nls-programmer">
303 <title>For the Programmer</title>
305 <sect2 id="nls-mechanics">
306 <title>Mechanics</title>
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>
315 <procedure>
316 <title>Adding NLS support to a program</title>
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
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>
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 no NLS is
351 configured.)
352 </para>
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>
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:
373 <variablelist>
374 <varlistentry>
375 <term><varname>CATALOG_NAME</varname></term>
377 <listitem>
378 <para>
379 The program name, as provided in the
380 <function>textdomain()</function> call.
381 </para>
382 </listitem>
383 </varlistentry>
385 <varlistentry>
386 <term><varname>AVAIL_LANGUAGES</varname></term>
388 <listitem>
389 <para>
390 List of provided translations &mdash; initially empty.
391 </para>
392 </listitem>
393 </varlistentry>
395 <varlistentry>
396 <term><varname>GETTEXT_FILES</varname></term>
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>
411 <varlistentry>
412 <term><varname>GETTEXT_TRIGGERS</varname></term>
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 </para>
425 </listitem>
426 </varlistentry>
427 </variablelist>
428 </para>
429 </step>
431 </procedure>
433 <para>
434 The build system will automatically take care of building and
435 installing the message catalogs.
436 </para>
437 </sect2>
439 <sect2 id="nls-guidelines">
440 <title>Message-writing guidelines</title>
442 <para>
443 Here are some guidelines for writing messages that are easily
444 translatable.
446 <itemizedlist>
447 <listitem>
448 <para>
449 Do not construct sentences at run-time, like:
450 <programlisting>
451 printf("Files were %s.\n", flag ? "copied" : "removed");
452 </programlisting>
453 The word order within the sentence might be different in other
454 languages. Also, even if you remember to call gettext() on each
455 fragment, the fragments might not translate well separately. It's
456 better to duplicate a little code so that each message to be
457 translated is a coherent whole. Only numbers, file names, and
458 such-like run-time variables should be inserted at run time into
459 a message text.
460 </para>
461 </listitem>
463 <listitem>
464 <para>
465 For similar reasons, this won't work:
466 <programlisting>
467 printf("copied %d file%s", n, n!=1 ? "s" : "");
468 </programlisting>
469 because it assumes how the plural is formed. If you figured you
470 could solve it like this:
471 <programlisting>
472 if (n==1)
473 printf("copied 1 file");
474 else
475 printf("copied %d files", n):
476 </programlisting>
477 then be disappointed. Some languages have more than two forms,
478 with some peculiar rules. We might have a solution for this in
479 the future, but for now the matter is best avoided altogether.
480 You could write:
481 <programlisting>
482 printf("number of copied files: %d", n);
483 </programlisting>
484 </para>
485 </listitem>
487 <listitem>
488 <para>
489 If you want to communicate something to the translator, such as
490 about how a message is intended to line up with other output,
491 precede the occurrence of the string with a comment that starts
492 with <literal>translator</literal>, e.g.:
493 <programlisting>
494 /* translator: This message is not what it seems to be. */
495 </programlisting>
496 These comments are copied to the message catalog files so that
497 the translators can see them.
498 </para>
499 </listitem>
500 </itemizedlist>
501 </para>
502 </sect2>
503 </sect1>
505 </chapter>