Thursday, July 21, 2011

Internationalization example with python gettext module

There is a plenty of documents on i18n with python gettext module on websites, but here is a minimal example showing how to translate a hard-coded string from English to Chinese. This example may be helpful for python developers to get a quick experience on the usage of gettext module. 

1. First, we need to tell python which string should be translated by marking all translatable strings in source codes with a prefix '_()', and for example:
import gettext
t = gettext.translation('cn', 'C:\locale', fallback=True)
_ = t.ugettext 

print _('Hello!') 
Note: 'C:\locale' tells the python interpreter where to search for the file, which contains python readable translated strings.

2. Use to extract all marked strings from .py file to a .pot file, and can be found at \Tools\i18n within the python installation directory. For example:
python -d cn -o cn.pot 
Note: cn is the domain name, which usually represents a language name.

3. Generate a .po file from the .pot file by modifying the header fields and adding the translated strings in the empty fields. In the cn.pot file, we need to make following changes:
"CHARSET" -> "gb2312"
"ENCODING" -> "utf8"
msgstr "" -> msgstr "你好!"

4. Get a gettext readable .mo file from the .po file by, for example: 
python -o cn.po

5. Run python, it will give you the following output: 

Some useful notes of Internationalization in Python is here.


  1. isnt their a standard set of words and their meaning so that instead of having to make a mo or po or pot files that store language translation, i mean simple word translation depending on what locale of python is running on the local machine?

  2. I recommend for best results with gettext, po, mo, pot, strings and other types of files also.

  3. i tried the above example.But it wont translated.Could you provide this us a video.It is easy understand.