From EduTech Wiki
Jump to: navigation, search


1 Introduction

Gettext is the name of the popular GNU internationalization and localization (i18n) library used in free software.

See also: Software localization.

2 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

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

$locale = "de_DE";
if (isSet($_GET["locale"])) $locale = $_GET["locale"];
setlocale(LC_ALL, $locale);
bindtextdomain("messages", "./locale");

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("This text will be translated"));

3 Tools

There exist several PO editors, e.g.

Toolkits for localization and translation

4 Complications

  • Dealing with arguments
  • Handling plural
  • ...

5 Links