Gettext

From OLPC
Revision as of 13:02, 24 November 2007 by Crazy-chris (talk | contribs) (initial edit)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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, where you change PROJECT_NAME and PROJECT_DIR to your needs:

# 1. Import Gettext
import gettext

# 2. Set Application Infos
gettext.bindtextdomain('PROJECT_NAME', '/ROJECT_DIR')
gettext.textdomain('PROJECT_NAME')

# 3. Alias gettext.gettext to _
_ = gettext.gettext

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:

import gettext
gettext.bindtextdomain('hello_world', '/home/hello/world')
gettext.textdomain('hello_world')
_ = gettext.gettext

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

name = _('%title %lastname %firstname')

Comments

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

# TRANSLATORS: Please let % as it is, just rearrange the 3 parts. They are exchanged by the program.
name = _('%title %lastname %firstname')

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:8
msgid "Mr."
msgstr ""
 
#. TRANSLATORS: Please let % as it is, just rearrange the 3 parts. Will be exchanged by the program.
#: test.py:13
msgid "%title %lastname %firstname"
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:8
msgid "Mr."
msgstr "Hr."
 
#: test.py:13
msgid "%title %lastname %firstname"
msgstr "%title %firstname %lastname"

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