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:

#example.py
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 cn.mo file, which contains python readable translated strings.

2. Use pygettext.py to extract all marked strings from .py file to a .pot file, and pygettext.py can be found at \Tools\i18n within the python installation directory. For example:
python pygettext.py -d cn -o cn.pot example.py 
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 msgfmt.py, for example: 
python msgfmt.py -o cn.mo cn.po

5. Run python example.py, it will give you the following output: 
你好!


Some useful notes of Internationalization in Python is here.

4 comments:

  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?

    ReplyDelete
  2. I recommend https://poeditor.com/ for best results with gettext, po, mo, pot, strings and other types of files also.

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

    ReplyDelete
  4. Does not work. The problem is with 'localedir'. You should have 'C:\locale\LC_MESSAGES' as LC_MESSAGES folder is a standard name where translator searches for *.mo files.

    ReplyDelete