9.2. For the Programmer

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

Adding NLS support to a program

  1. Insert this code into the startup 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 no NLS is configured.)

    This may tend to add a lot of clutter. One common shortcut is to

    #define _(x) gettext((x))

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

  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:

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

To ease the translation of messages, here are some guidelines: