nls.sgml 17.8 KB
Newer Older
1
<!--
2
$PostgreSQL: pgsql/doc/src/sgml/nls.sgml,v 1.10 2004/11/15 06:32:14 neilc Exp $
3
-->
4 5

<chapter id="nls">
6
 <chapterinfo>
7 8 9 10
  <author>
   <firstname>Peter</firstname>
   <surname>Eisentraut</surname>
  </author>
11
 </chapterinfo>
12 13 14 15 16 17 18

 <title>Native Language Support</title>

 <sect1 id="nls-translator">
  <title>For the Translator</title>

  <para>
19 20
   <productname>PostgreSQL</>
   programs (server and client) can issue their messages in
21
   your favorite language &mdash; if the messages have been translated.
22 23
   Creating and maintaining translated message sets needs the help of
   people who speak their own language well and want to contribute to
24 25
   the <productname>PostgreSQL</> effort.  You do not have to be a
   programmer at all
26 27 28 29 30 31 32
   to do this.  This section explains how to help.
  </para>

  <sect2>
   <title>Requirements</title>

   <para>
33
    We won't judge your language skills &mdash; this section is about
34 35 36 37
    software tools.  Theoretically, you only need a text editor.  But
    this is only in the unlikely event that you do not want to try out
    your translated messages.  When you configure your source tree, be
    sure to use the <option>--enable-nls</option> option.  This will
38
    also check for the <application>libintl</application> library and the
39 40 41 42 43 44 45 46 47 48 49 50 51
    <filename>msgfmt</filename> program, which all end users will need
    anyway.  To try out your work, follow the applicable portions of
    the installation instructions.
   </para>

   <para>
    If you want to start a new translation effort or want to do a
    message catalog merge (described later), you will need the
    programs <filename>xgettext</filename> and
    <filename>msgmerge</filename>, respectively, in a GNU-compatible
    implementation.  Later, we will try to arrange it so that if you
    use a packaged source distribution, you won't need
    <filename>xgettext</filename>.  (From CVS, you will still need
52
    it.)  <application>GNU Gettext 0.10.36</application> or later is currently recommended.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
   </para>

   <para>
    Your local gettext implementation should come with its own
    documentation.  Some of that is probably duplicated in what
    follows, but for additional details you should look there.
   </para>
  </sect2>

  <sect2>
   <title>Concepts</title>

   <para>
    The pairs of original (English) messages and their (possibly)
    translated equivalents are kept in <firstterm>message
    catalogs</firstterm>, one for each program (although related
    programs can share a message catalog) and for each target
    language.  There are two file formats for message catalogs:  The
    first is the <quote>PO</quote> file (for Portable Object), which
    is a plain text file with special syntax that translators edit.
    The second is the <quote>MO</quote> file (for Machine Object),
    which is a binary file generated from the respective PO file and
    is used while the internationalized program is run.  Translators
    do not deal with MO files; in fact hardly anyone does.
   </para>

   <para>
    The extension of the message catalog file is to no surprise either
    <filename>.po</filename> or <filename>.mo</filename>.  The base
    name is either the name of the program it accompanies, or the
    language the file is for, depending on the situation.  This is a
    bit confusing.  Examples are <filename>psql.po</filename> (PO file
    for psql) or <filename>fr.mo</filename> (MO file in French).
   </para>

   <para>
    The file format of the PO files is illustrated here:
<programlisting>
# comment

msgid "original string"
msgstr "translated string"

msgid "more original"
msgstr "another translated"
"string can be broken up like this"

...
</programlisting>
    The msgid's are extracted from the program source.  (They need not
    be, but this is the most common way.)  The msgstr lines are
    initially empty and are filled in with useful strings by the
    translator.  The strings can contain C-style escape characters and
    can be continued across lines as illustrated.  (The next line must
    start at the beginning of the line.)
   </para>

   <para>
    The # character introduces a comment.  If whitespace immediately
    follows the # character, then this is a comment maintained by the
    translator.  There may also be automatic comments, which have a
    non-whitespace character immediately following the #.  These are
    maintained by the various tools that operate on the PO files and
    are intended to aid the translator.
<programlisting>
#. automatic comment
#: filename.c:1023
#, flags, flags
</programlisting>
    The #. style comments are extracted from the source file where the
    message is used.  Possibly the programmer has inserted information
    for the translator, such as about expected alignment.  The #:
    comment indicates the exact location(s) where the message is used
    in the source.  The translator need not look at the program
    source, but he can if there is doubt about the correct
    translation.  The #, comments contain flags that describe the
    message in some way.  There are currently two flags:
    <literal>fuzzy</literal> is set if the message has possibly been
    outdated because of changes in the program source.  The translator
    can then verify this and possibly remove the fuzzy flag.  Note
    that fuzzy messages are not made available to the end user.  The
    other flag is <literal>c-format</literal>, which indicates that
    the message is a <function>printf</function>-style format
    template.  This means that the translation should also be a format
    string with the same number and type of placeholders.  There are
    tools that can verify this, which key off the c-format flag.
   </para>
  </sect2>

  <sect2>
   <title>Creating and maintaining message catalogs</title>

   <para>
146
    OK, so how does one create a <quote>blank</quote> message
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    catalog?  First, go into the directory that contains the program
    whose messages you want to translate.  If there is a file
    <filename>nls.mk</filename>, then this program has been prepared
    for translation.
   </para>

   <para>
    If there are already some <filename>.po</filename> files, then
    someone has already done some translation work.  The files are
    named <filename><replaceable>language</replaceable>.po</filename>,
    where <replaceable>language</replaceable> is the <ulink
    url="http://lcweb.loc.gov/standards/iso639-2/englangn.html">ISO
    639-1</ulink> two-letter language code (in lower case), e.g.,
    <filename>fr.po</filename> for French.  If there is really a need
    for more than one translation effort per language then the files
    may also be named
    <filename><replaceable>language</replaceable>_<replaceable>region</replaceable>.po</filename>
    where <replaceable>region</replaceable> is the <ulink
    url="http://www.din.de/gremien/nas/nabd/iso3166ma/codlstp1/en_listp1.html">ISO
    3166-1</ulink> two-letter country code (in upper case), e.g.,
    <filename>pt_BR.po</filename> for Portuguese in Brazil.  If you
    find the language you wanted you can just start working on that
    file.
   </para>

   <para>
    If you need to start a new translation effort, then first run the
    command
<programlisting>
gmake init-po
</programlisting>
    This will create a file
    <filename><replaceable>progname</replaceable>.pot</filename>.
    (<filename>.pot</filename> to distinguish it from PO files that
181 182 183
    are <quote>in production</quote>. The <literal>T</> stands for
    <quote>template</>.)
    Copy this file to
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    <filename><replaceable>language</replaceable>.po</filename> and
    edit it.  To make it known that the new language is available,
    also edit the file <filename>nls.mk</filename> and add the
    language (or language and country) code to the line that looks like:
<programlisting>
AVAIL_LANGUAGES := de fr
</programlisting>
    (Other languages may appear, of course.)
   </para>

   <para>
    As the underlying program or library changes, messages may be
    changed or added by the programmers.  In this case you do not need
    to start from scratch.  Instead, run the command
<programlisting>
gmake update-po
</programlisting>
    which will create a new blank message catalog file (the pot file
    you started with) and will merge it with the existing PO files.
    If the merge algorithm is not sure about a particular message it
    marks it <quote>fuzzy</quote> as explained above.  For the case
    where something went really wrong, the old PO file is saved with a
    <filename>.po.old</filename> extension.
   </para>
  </sect2>

  <sect2>
   <title>Editing the PO files</title>

   <para>
    The PO files can be edited with a regular text editor.  The
    translator should only change the area between the quotes after
    the msgstr directive, may add comments and alter the fuzzy flag.
    There is (unsurprisingly) a PO mode for Emacs, which I find quite
    useful.
   </para>

   <para>
    The PO files need not be completely filled in.  The software will
    automatically fall back to the original string if no translation
    (or an empty translation) is available.  It is no problem to
    submit incomplete translations for inclusions in the source tree;
    that gives room for other people to pick up your work.  However,
    you are encouraged to give priority to removing fuzzy entries
    after doing a merge.  Remember that fuzzy entries will not be
    installed; they only serve as reference what might be the right
    translation.
   </para>

   <para>
    Here are some things to keep in mind while editing the
    translations:
    <itemizedlist>
     <listitem>
      <para>
       Make sure that if the original ends with a newline, the
       translation does, too.  Similarly for tabs, etc.
      </para>
     </listitem>

     <listitem>
      <para>
246 247
       If the original is a <function>printf</> format string, the translation
       also needs to be.  The translation also needs to have the same
248 249
       format specifiers in the same order.  Sometimes the natural
       rules of the language make this impossible or at least awkward.
250
       In that case you can modify the format specifiers like this:
251 252 253 254 255 256
<programlisting>
msgstr "Die Datei %2$s hat %1$u Zeichen."
</programlisting>
       Then the first placeholder will actually use the second
       argument from the list.  The
       <literal><replaceable>digits</replaceable>$</literal> needs to
257
       follow the % immediately, before any other format manipulators.
258
       (This feature really exists in the <function>printf</function>
259
       family of functions.  You may not have heard of it before because
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
       there is little use for it outside of message
       internationalization.)
      </para>
     </listitem>

     <listitem>
      <para>
       If the original string contains a linguistic mistake, report
       that (or fix it yourself in the program source) and translate
       normally.  The corrected string can be merged in when the
       program sources have been updated.  If the original string
       contains a factual mistake, report that (or fix it yourself)
       and do not translate it.  Instead, you may mark the string with
       a comment in the PO file.
      </para>
     </listitem>

     <listitem>
      <para>
       Maintain the style and tone of the original string.
       Specifically, messages that are not sentences (<literal>cannot
       open file %s</literal>) should probably not start with a
       capital letter (if your language distinguishes letter case) or
       end with a period (if your language uses punctuation marks).
284
       It may help to read <xref linkend="error-style-guide">.
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
      </para>
     </listitem>

     <listitem>
      <para>
       If you don't know what a message means, or if it is ambiguous,
       ask on the developers' mailing list.  Chances are that English
       speaking end users might also not understand it or find it
       ambiguous, so it's best to improve the message.
      </para>
     </listitem>

    </itemizedlist>
   </para>
  </sect2>

 </sect1>


 <sect1 id="nls-programmer">
  <title>For the Programmer</title>

307 308 309
  <sect2 id="nls-mechanics">
   <title>Mechanics</title>

310
  <para>
311
   This section describes how to implement native language support in a
312 313
   program or library that is part of the
   <productname>PostgreSQL</> distribution.
314 315 316 317 318 319 320 321
   Currently, it only applies to C programs.
  </para>

  <procedure>
   <title>Adding NLS support to a program</title>

   <step>
    <para>
322
     Insert this code into the start-up sequence of the program:
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
<programlisting>
#ifdef ENABLE_NLS
#include &lt;locale.h&gt;
#endif

...

#ifdef ENABLE_NLS
setlocale(LC_ALL, "");
bindtextdomain("<replaceable>progname</replaceable>", LOCALEDIR);
textdomain("<replaceable>progname</replaceable>");
#endif
</programlisting>
     (The <replaceable>progname</replaceable> can actually be chosen
     freely.)
    </para>
   </step>

   <step>
    <para>
343
     Wherever a message that is a candidate for translation is found,
344 345 346 347 348 349 350 351 352 353 354 355 356
     a call to <function>gettext()</function> needs to be inserted.  E.g.,
<programlisting>
fprintf(stderr, "panic level %d\n", lvl);
</programlisting>
     would be changed to
<programlisting>
fprintf(stderr, gettext("panic level %d\n"), lvl);
</programlisting>
     (<symbol>gettext</symbol> is defined as a no-op if no NLS is
     configured.)
    </para>

    <para>
357
     This may tend to add a lot of clutter.  One common shortcut is to use
358
<programlisting>
359
#define _(x) gettext(x)
360 361 362
</programlisting>
     Another solution is feasible if the program does much of its
     communication through one or a few functions, such as
363
     <function>ereport()</function> in the backend.  Then you make this
364
     function call <function>gettext</function> internally on all
365
     input strings.
366 367 368 369 370 371 372 373 374 375 376
    </para>
   </step>

   <step>
    <para>
     Add a file <filename>nls.mk</filename> in the directory with the
     program sources.  This file will be read as a makefile.  The
     following variable assignments need to be made here:

     <variablelist>
      <varlistentry>
377
       <term><varname>CATALOG_NAME</varname></term>
378 379 380 381 382 383 384 385 386 387

       <listitem>
        <para>
         The program name, as provided in the
         <function>textdomain()</function> call.
        </para>
       </listitem>
      </varlistentry>

      <varlistentry>
388
       <term><varname>AVAIL_LANGUAGES</varname></term>
389 390 391

       <listitem>
        <para>
392
         List of provided translations &mdash; initially empty.
393 394 395 396 397
        </para>
       </listitem>
      </varlistentry>

      <varlistentry>
398
       <term><varname>GETTEXT_FILES</varname></term>
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413

       <listitem>
        <para>
         List of files that contain translatable strings, i.e., those
         marked with <function>gettext</function> or an alternative
         solution.  Eventually, this will include nearly all source
         files of the program.  If this list gets too long you can
         make the first <quote>file</quote> be a <literal>+</literal>
         and the second word be a file that contains one file name per
         line.
        </para>
       </listitem>
      </varlistentry>

      <varlistentry>
414
       <term><varname>GETTEXT_TRIGGERS</varname></term>
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438

       <listitem>
        <para>
         The tools that generate message catalogs for the translators
         to work on need to know what function calls contain
         translatable strings.  By default, only
         <function>gettext()</function> calls are known.  If you used
         <function>_</function> or other identifiers you need to list
         them here.  If the translatable string is not the first
         argument, the item needs to be of the form
         <literal>func:2</literal> (for the second argument).
        </para>
       </listitem>
      </varlistentry>
     </variablelist>
    </para>
   </step>

  </procedure>

  <para>
   The build system will automatically take care of building and
   installing the message catalogs.
  </para>
439 440 441 442
  </sect2>

  <sect2 id="nls-guidelines">
   <title>Message-writing guidelines</title>
443 444

  <para>
445 446
   Here are some guidelines for writing messages that are easily
   translatable.
447 448 449 450

   <itemizedlist>
    <listitem>
     <para>
451
      Do not construct sentences at run-time, like
452
<programlisting>
453
printf("Files were %s.\n", flag ? "copied" : "removed");
454 455
</programlisting>
      The word order within the sentence may be different in other
456 457 458 459 460 461
      languages.  Also, even if you remember to call gettext() on each
      fragment, the fragments may not translate well separately.  It's
      better to duplicate a little code so that each message to be
      translated is a coherent whole.  Only numbers, file names, and
      such-like run-time variables should be inserted at runtime into
      a message text.
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
     </para>
    </listitem>

    <listitem>
     <para>
      For similar reasons, this won't work:
<programlisting>
printf("copied %d file%s", n, n!=1 ? "s" : "");
</programlisting>
      because it assumes how the plural is formed.  If you figured you
      could solve it like this
<programlisting>
if (n==1)
    printf("copied 1 file");
else
    printf("copied %d files", n):
</programlisting>
      then be disappointed.  Some languages have more than two forms,
      with some peculiar rules.  We may have a solution for this in
481 482
      the future, but for now the matter is best avoided altogether.
      You could write:
483 484 485 486 487 488 489 490 491 492
<programlisting>
printf("number of copied files: %d", n);
</programlisting>
     </para>
    </listitem>

    <listitem>
     <para>
      If you want to communicate something to the translator, such as
      about how a message is intended to line up with other output,
493
      precede the occurrence of the string with a comment that starts
494 495 496 497 498 499 500 501 502 503
      with <literal>translator</literal>, e.g.,
<programlisting>
/* translator: This message is not what it seems to be. */
</programlisting>
      These comments are copied to the message catalog files so that
      the translators can see them.
     </para>
    </listitem>
   </itemizedlist>
  </para>
504
  </sect2>
505 506 507
 </sect1>

</chapter>