Gettext

From OLPC
Jump to navigation Jump to search

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

Programming

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 (test.py) 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;

Comments

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 './setup.py 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 localized 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: test.py

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

#: test.py:3
msgid "Mr."
msgstr ""

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

Distribute it and people can start translating.

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:

#: test.py:4
msgid "Mr."
msgstr "Hr."

#: test.py:9
#, 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.

Running

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