Getting Started with Internationalization (i18n)
gettext is the GNU internationalization (i18n) library and the standard for open source. Translators understand it and there are a variety of tools available that work with gettext data. This page provides a brief overview of the steps needed to begin internationalizing your project.
Note: If you are starting a new project, you may wish to use Linux Project Generator to help correctly set up the autoconf build files with internationalization support.
Project Code Changes
Step 1: Update header files for main
Add the following to the headers of the C module in which you have your main() function:
#ifdef USE_GETTEXT
#include "libintl.h"
#include "locale.h"
#define _(String) gettext (String)
#else
#define _(String) String
#endifStep 2: main function
Add the following to the beginning of your main() function:
#ifdef USE_GETTEXT
setlocale (LC_MESSAGES, "");
setlocale (LC_CTYPE, "");
setlocale (LC_COLLATE, "");
textdomain ("my-program");
bindtextdomain ("my-program", NULL);
#endifStep 3: Update header files
Add the following to each header where internationalized strings are used.
#ifdef USE_GETTEXT
#include "libintl.h"
#define _(String) gettext (String)
#else
#define _(String) String
#endifStep 4: String prefixes
When writing a string anywhere in code, use the following format: _( <string> ). For example, instead of:
(Not internationalized)
printf ("some string");instead use:
(Internationalized)
printf (_("some string") )Step 5: Build flags
Either compile your program with -DUSE_GETTEXT or add the following near the top of the main header file:
#define USE_GETTEXTStep 6: Generating pot and po files
The .pot file contains the list of localizable (translatable) strings. There is often a single .pot file for a project, although multiple .pot files can be generated to avoid overly large files.
Create a single .pot file:
xgettext -k_ -o my-program.pot *.c *.h --from-code=iso-8859-1The .pot file that is generated from the above command is the template that contains the English strings and a corresponding empty string field for the translator to add the translation. Once the translations are complete, the file is saved with a .po extension to indicate it is complete. There is one .po file for each language.
Finally, a .mo file must be created for each .po file. The .mo file is the binary version that gets integrated into the parent package.
msgfmt my-program.es.po -o my-program.es.moStep 7: Installing the files
Install the .mo file in the proper directory:
cp my-program.es.mo /usr/share/locale/es/LC_MESSAGES/my-program.moFor full details on internationalizing your projects check out the Internationalization Guidelines (PDF, 450KB).
| Attachment | Size |
|---|---|
| internationalization_guidelines_2.pdf | 431.54 KB |