Gettext
Introduction
Gettext is the name of the popular GNU internationalization and localization (i18n) library used in free software.
See also: Software localization.
Principle
Gettext uses the following principle:
- All source code must use Gettext calls when using strings. (e.g. print("salut") would become print (gettext ("salut") or the shorcut print (_("salut"))
- The language used in the code will be the key for all translation languages
- xgetttext then can extract all these from the source and produce a *.pot template file.
- This template file then is use to produce editable *.po files that look like this
#: src/name.c:36 msgid "My name is %s.\n" msgstr "Je m'appelle %s.\n"
Msgid (the key and original language) and msgstr the translated string can be displayd side-by side by so-called PO editors.
Gettext is used in slightly different ways in different languages.
- PHP source code example
(1) Sample code with a string that needs translation
<?php
echo _("Hello World!");
?>
(2) Translations then should go a directory, e.g. locale Each locale will sit in its own subdirectory. (more details needed here ..)
(3) Sample code to initialize the run-time localization
<?php
$locale = "de_DE";
if (isSet($_GET["locale"])) $locale = $_GET["locale"];
putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
bindtextdomain("messages", "./locale");
textdomain("messages");
?>
- Java
Gettext can be used with the gettext-commons library. Java source code modifications rather would look like this:
I18n i18n = I18nFactory.getI18n(getClass()); System.out.println(i18n.tr("This text will be translated"));
Tools
There exist several PO editors, e.g.
Toolkits for localization and translation
- Translate Toolkit (Wikipedia) can handle PO files.
Complications
- Dealing with arguments
- Handling plural
- ...
Links
- GNU Gettext (Wikipedia)
- Localizing PHP web sites using gettext by Pablo Hoch, 2005.