Jump to navigation Jump to search



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

See also: Software localization.


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"));


There exist several PO editors, e.g.

Toolkits for localization and translation


  • Dealing with arguments
  • Handling plural
  • ...



Content of this article has been taken from EduTechWiki (en) or EduTechWiki (fr) at the date indicated in the history. DKS was the main founder and main contributor of EduTechWiki. If you cite this page you also must cite and credit EduTechWiki, according to the CC BY-NC-SA license. View the pageinfo-toolboxlink for this article.