Jump to: navigation, search

gettext is the GNU internationalization (i18n) library. It is commonly used for writing multilingual programs. The latest version is 0.17.



Source code is first modified to use the GNU gettext calls. This is, for most programming languages, done by wrapping strings that the user will see in the gettext function. To save on typing time, and to reduce code clutter, this function is commonly aliased to _, so that the Python code

print 'Hello World!'

would become

print _('Hello World!')

in Python

To load the gettext function and alias it to _, include this code:

from gettext import gettext as _

Now you're set for using gettext in your project. Simply wrap outputs from 'Output' to _('Output'). Keep in mind, that not only strings can require localization, but also

  • numbers,
  • time formats,
  • currencies,
  • time zones,
  • names and titles,
  • ...

Example Application

Let's make up an example ( for translating names and titles:

from gettext import gettext as _

title = _('Mr.')
lastname = 'Hager'
firstname = 'Chris'

name = _('%(title)s %(lastname)s %(firstname)s') % {'title': title, 'lastname': lastname, 'firstname': firstname};
print name;


It's possible to leave a comment directed to the translator like this:

# TRANSLATORS: Please just rearrange the 3 '%(...)s' parts as required.
name = _('%(title)s %(lastname)s %(firstname)s') % {'title': title, 'lastname': lastname, 'firstname': firstname};

Note: When './ genpot' is used in a sugar environment to generate the PO template file, it specifies 'TRANS:' rather than 'TRANSLATORS:' as the marker for comments to translators. So, if the software being internationalized is a python sugar activity, comments directed to the translators should be marked with 'TRANS:' rather than 'TRANSLATORS:'.

Building the template file

No we use xgettext to build a .po template file from the source code. This will be used by translators to derive local .po files.

xgettext --add-comments=TRANSLATORS:

Our newly created template file with translations (eg. messages.po) looks like this:

msgid "Mr."
msgstr ""

# TRANSLATORS: Please just rearrange the 3 '%(...)s' parts as required.
#, python-format
msgid "%(title)s %(lastname)s %(firstname)s"
msgstr ""

Distribute it and people can start translating.


We can derive a local .po file from the template using the msginit program. For a german translation we'd do this:

msginit --locale=de --input=messages.po

This will create a file named 'de.po'. The translator needs to edit it either by hand or with tools such as poEdit. When they are done, it will could like this:

msgid "Mr."
msgstr "Hr."

#, python-format
msgid "%(title)s %(lastname)s %(firstname)s"
msgstr "%(title)s %(firstname)s %(lastname)s"

Finally, the .po files are compiled into a binary .mo file with msgfmt.

msgfmt de.po

These are now ready for distribution with the software package.


On Unix-type systems, the user sets the environment variable LC_MESSAGES, and the program will display strings in the selected language, if there is an .mo file for it.

Related Links

Personal tools
  • Log in
  • Login with OpenID
About OLPC
About the laptop
About the tablet
OLPC wiki