Progmar Marcin Załęczny

Language:

C++ gettext usage

Assume following program hello.cpp:
#include <libintl.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>

#define _(STRING) gettext(STRING)

int main(void)
{
setlocale(LC_ALL, "");
bindtextdomain("hello", "./locale");
textdomain("hello");

printf("%s\n", _("Hello, world!"));

return 0;
}

In the code above inserted includes must be for gettext to work. Line with define keyword is for shortage usage of gettext: we can write _("Text to translate") instead of gettext("Text to translate"). At the very beginning of the program we call following functions to initialise gettext:
    * setlocale for setting proper language (in the example it is system default)
    * bindtextdomain it points to the files with translated strings
    * textdomain it reads translation into program's memory
After requirements above we write all strings to be translated with _("string") notation.

Now, how to make translation files

xgettext -d hello -s -o hello.pot hello.cpp -k_ Gets from hello.cpp file all strings inside gettext("")/_("") statements and produce hello.pot file with gettext syntax that contain header tags such as package version, revision date, translator's name, language and charset.
Parameter -s makes all texts to be sorted and after parameter -d we pass domain name the same as in bindtextdomain call. Parameter -k means that function gettext is available with _ macrodefinition. At the moment we must replace CHARSET tag by proper encoding (ex. UTF-8 defaulted in Ubuntu and other Linux systems) and PACKAGE VERSION tag replace by version number of the translation in generated hello.pot file.

msginit -l pl_PL -o hello.po -i hello.pot It firstly asks for the email of the translator and next generates file for proper destination language (here pl_PL). We operate on the file to translate all strings after msgid keywords. It is done by filling up lines following (after msgstr keyword).

msgfmt -c -v -o hello.mo hello.po Compiles translation file (hello.po) to binary format that can be understand by the program. Generated hello.mo file we must copy to the ./locale/pl/LC_MESSAGES.

And now running program should output translated string.