Next: , Previous: Notes for Debugging, Up: Programming

6.13 How to use I18N in own programs

This chapter discusses shortly how to use the Internationalization (I18N) features of GNU Pascal.


You need to have gettext installed. Try to compile demos/gettextdemo.pas. Furthermore, you should download a tool named pas2po from

The source

We would like to translate the messages provided with this simple example different languages (here: German) without touching the source for each language:

     program Hello1;
       WriteLn ('Hello, World!');
       WriteLn ('The answer of the questions is: ', 42)

Preparing the source

To do so, we must prepare the source to use gettext:

     program Hello2;
     uses GPC, Intl;
     var s: TString;
       Discard (BindTextDomain ('hello2', '/usr/share/locale/'));
       Discard (TextDomain ('hello2'));
       WriteLn (GetText ('Hello, World!'));
       s := FormatString (GetText ('The answer of the questions is %s'), 42);
       WriteLn (s)

BindTextDomain sets the path to find our message catalogs in the system. This path is system dependent. TextDomain tells the program to use this catalog. GetText looks up the given string in the catalog and returns a translated string within the current locale settings. FormatString replaces some format specifiers with the following argument. %s is the first following argument. After this step is done, we do not need to touch the sourcefile any longer. The output of this program is as follows:

     Hello, World!
     The answer of the questions is 42

Getting the translatable strings

There are lots of strings in the above example, but only those surrounded with GetText should be translated. We use pas2po hello2.pas -o hello2.po to extract the messages. The output is:

     # This file was created by pas2po with 'hello2.pas'.
     # Please change this file manually.
     # Copyright (C) YEAR Free Software Foundation, Inc.
     #, fuzzy
     msgid ""
     msgstr ""
     "Project-Id-Version: PACKAGE VERSION\n"
     "POT-Creation-Date: 2003-04-27 20:48+0200\n"
     "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
     "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
     "Language-Team: LANGUAGE <>\n"
     "MIME-Version: 1.0\n"
     "Content-Type: text/plain; charset=CHARSET\n"
     "Content-Transfer-Encoding: 8bit\n"
     msgid "Hello, World!"
     msgstr ""
     msgid "The answer of the questions is %s"
     msgstr ""

Now we translate the message ids into German language, and set some needful informations at their appropriate places. The following steps must be repeated for each language we would like to support:

     # This file was created by pas2po with 'hello2.pas'.
     # Copyright (C) 2003 Free Software Foundation, Inc.
     # Eike Lange  <>, 2003.
     msgid ""
     msgstr ""
     "Project-Id-Version: Hello2 1.0\n"
     "POT-Creation-Date: 2003-04-27 12:00+0200\n"
     "PO-Revision-Date: 2003-04-27 12:06+0200\n"
     "Last-Translator: Eike Lange <>\n"
     "Language-Team: de <>\n"
     "MIME-Version: 1.0\n"
     "Content-Type: text/plain; charset=ISO-8859-1\n"
     "Content-Transfer-Encoding: 8bit\n"
     msgid "Hello, World!"
     msgstr "Hallo, Welt!"
     msgid "The answer of the questions is %s"
     msgstr "'%s' lautet die Antwort auf die Frage."

Please note that we swapped text and numeric arguments and added some single quotes arround the first argument. We compile the message catalog with msgfmt -vv hello2.po -o and install the file at /usr/share/locale/de/LC_MESSAGES/ With a german locale setting, the output should be as follows:

     Hallo, Welt!
     '42' lautet die Antwort auf die Frage.

System dependent notes:

The topmost path where message catalogs reside is system dependent:

for DJGPP:
GetEnv ('$DJDIR') + '/share/locale'
for Mac OS X:
/usr/share/locale or /sw/share/locale
for Linux, *BSD:
/usr/share/locale or /usr/local/share/locale

See also

Gettext (gettext), FormatString, Intl.