6 <firstname>Peter
</firstname>
7 <surname>Eisentraut
</surname>
11 <title>Native Language Support
</title>
13 <sect1 id=
"nls-translator">
14 <title>For the Translator
</title>
17 <productname>PostgreSQL<
/>
18 programs (server and client) can issue their messages in
19 your favorite language
— 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
24 to do this. This section explains how to help.
28 <title>Requirements
</title>
31 We won't judge your language skills
— 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.
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 CVS, you will still need
50 it.)
<application>GNU Gettext
0.10.36</application> or later is currently recommended.
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.
61 <title>Concepts
</title>
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.
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).
87 The file format of the PO files is illustrated here:
91 msgid
"original string"
92 msgstr
"translated string"
95 msgstr
"another translated"
96 "string can be broken up like this"
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.)
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.
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.
141 <title>Creating and maintaining message catalogs
</title>
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
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
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>,
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
172 If you need to start a new translation effort, then first run the
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
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:
188 AVAIL_LANGUAGES := de fr
190 (Other languages can appear, of course.)
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:
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.
209 <title>Editing the PO files
</title>
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
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
232 Here are some things to keep in mind while editing the
237 Make sure that if the original ends with a newline, the
238 translation does, too. Similarly for tabs, etc.
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:
250 msgstr
"Die Datei %2$s hat %1$u Zeichen."
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.)
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.
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">.
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.
302 <sect1 id=
"nls-programmer">
303 <title>For the Programmer
</title>
305 <sect2 id=
"nls-mechanics">
306 <title>Mechanics
</title>
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.
316 <title>Adding NLS support to a program
</title>
320 Insert this code into the start-up sequence of the program:
323 #include
<locale.h
>
329 setlocale(LC_ALL,
"");
330 bindtextdomain(
"<replaceable>progname</replaceable>", LOCALEDIR);
331 textdomain(
"<replaceable>progname</replaceable>");
334 (The
<replaceable>progname
</replaceable> can actually be chosen
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.:
344 fprintf(stderr,
"panic level %d\n", lvl);
348 fprintf(stderr, gettext(
"panic level %d\n"), lvl);
350 (
<symbol>gettext
</symbol> is defined as a no-op if NLS support is
355 This tends to add a lot of clutter. One common shortcut is to use:
357 #define _(x) gettext(x)
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
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:
375 <term><varname>CATALOG_NAME
</varname></term>
379 The program name, as provided in the
380 <function>textdomain()
</function> call.
386 <term><varname>AVAIL_LANGUAGES
</varname></term>
390 List of provided translations
— initially empty.
396 <term><varname>GETTEXT_FILES
</varname></term>
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
412 <term><varname>GETTEXT_TRIGGERS
</varname></term>
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).
437 The build system will automatically take care of building and
438 installing the message catalogs.
442 <sect2 id=
"nls-guidelines">
443 <title>Message-writing guidelines
</title>
446 Here are some guidelines for writing messages that are easily
452 Do not construct sentences at run-time, like:
454 printf(
"Files were %s.\n", flag ?
"copied" :
"removed");
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
468 For similar reasons, this won't work:
470 printf(
"copied %d file%s", n, n!=
1 ?
"s" :
"");
472 because it assumes how the plural is formed. If you figured you
473 could solve it like this:
476 printf(
"copied 1 file");
478 printf(
"copied %d files", n):
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:
484 printf(
"number of copied files: %d", n);
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:
494 errmsg_plural(
"copied %d file",
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<
/>.
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
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.:
528 /* translator: This message is not what it seems to be. */
530 These comments are copied to the message catalog files so that
531 the translators can see them.