48.2. For the Programmer

48.2.1. Mechanics

Note: The following description applies both to Postgres-XC and PostgreSQL if not described explicitly.

This section describes how to implement native language support in a program or library that is part of the Postgres-XC distribution. Currently, it only applies to C programs.

Adding NLS Support to a Program

  1. Insert this code into the start-up sequence of the program:

    #ifdef ENABLE_NLS
    #include <locale.h>
    #endif
    
    ...
    
    #ifdef ENABLE_NLS
    setlocale(LC_ALL, "");
    bindtextdomain("progname", LOCALEDIR);
    textdomain("progname");
    #endif

    (The progname can actually be chosen freely.)

  2. Wherever a message that is a candidate for translation is found, a call to gettext() needs to be inserted. E.g.:

    fprintf(stderr, "panic level %d\n", lvl);

    would be changed to:

    fprintf(stderr, gettext("panic level %d\n"), lvl);

    (gettext is defined as a no-op if NLS support is not configured.)

    This tends to add a lot of clutter. One common shortcut is to use:

    #define _(x) gettext(x)

    Another solution is feasible if the program does much of its communication through one or a few functions, such as ereport() in the backend. Then you make this function call gettext internally on all input strings.

  3. Add a file nls.mk in the directory with the program sources. This file will be read as a makefile. The following variable assignments need to be made here:

    CATALOG_NAME

    The program name, as provided in the textdomain() call.

    AVAIL_LANGUAGES

    List of provided translations — initially empty.

    GETTEXT_FILES

    List of files that contain translatable strings, i.e., those marked with gettext 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 "file" be a + and the second word be a file that contains one file name per line.

    GETTEXT_TRIGGERS

    The tools that generate message catalogs for the translators to work on need to know what function calls contain translatable strings. By default, only gettext() calls are known. If you used _ 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 func:2 (for the second argument). If you have a function that supports pluralized messages, the item should look like func:1,2 (identifying the singular and plural message arguments).

The build system will automatically take care of building and installing the message catalogs.

48.2.2. Message-writing Guidelines

Note: The following description applies both to Postgres-XC and PostgreSQL if not described explicitly.

Here are some guidelines for writing messages that are easily translatable.