Internationalize a Python application


Contents

Internationalization of a python application (in Maemo)

This page explains step by step how to add support for translations in a python application for maemo.

There are already some tutorials online about how to internationalize (i18n) python applications, but i find them difficult to follow and they lack some needed code and final tricks to make everything work fine. This is the steps i followed to add i18n in my maemo project Mussorgsky.

To support i18n we need to accomplish 5 tasks:

  1. Define correctly a function '_()' that translates strings
  2. Mark the strings in the code with that function
  3. Generate a template for the translators
  4. Add translations
  5. Include the translations in the installation

The overall process

From the source code, using a command line tool, we will generate a ".pot" file. It is plain text file containing all strings that need translation in the project. This .pot file only needs to be generated/recreated when the strings change in the code (and _not_ in every build!). There is only one .pot file per project.

Then, from the .pot files and using another command line tool, we will generate the .po files for each language. There is one .po file per language, and it is the file that the translators need to complete. Both .pot and all .po files should be committed in the repository.

But once your program is running, it doesn't use directly .po files, but a binary (compiled) version of them: the .mo files. These .mo files must be re-created on build time (when creating the package) and installed in the right location in the system. These are generated files, so don't commit them in the repository.

From the code point of view, you just need to call few functions to tell gettext where are the translation files, what language do you want to use (usually the locale of the environment), and then it will use its implementation to find the right string.

Ok, this is what we need to do. Let's code.

Configure gettext Define the '_()' function

If you don't care too much about the details, just copy this i18n.py file in your src/ directory (change the APP_NAME variable there!), and in every .py file in your module that needs to translate any string, add these lines at the top:

import i18n
_ = i18n.language.gettext

Done. If you are curious about what is going on there, the file is plenty of comments explaining every line.

Mark strings for i18n

This is one of the easiest parts. Browse the source code files that show something on the screen, and wrap the visible strings (window titles, dialog titles, notifications, labels, button labels and so on) with the '_()' function.

For example:

# import the _() function!
import i18n
_ = i18n.language.gettext

class Example (hildon.StackableWindow):

   def __init__ (self):
       hildon.StackableWindow.__init__ (self)
       self.set_title ( _("All music") )   <------- ADD THIS!

Generate template for the translators

Create a po folder in your project (usually at the same level as src/) and run the following command from the top directory of your project. You can do this inside or outside scratchbox, there is no difference.

xgettext --language=Python --keyword=_ --output=po/PROJECTNAME.pot `find . -name "*.py"`

It will parse files, written in Python, looking for strings marked with the keyword (function name) '_' and saving the output in a file called 'po/PROJECTNAME.pot'. The list of files is the last argument. I like to use "find", but you can manually put a list of files there.

That command will generate the .pot file. Easy, isn't it?

How to add translations

Now you want to get a translation in an specific language. You need to generate a .po for it.

Go to the po/ folder and run this command (again, inside/outside scratchbox doesn't matter):

msginit --input=PROJECTNAME.pot --locale=LOCALE

For instance, if i want a .po file for the spanish translation of my project Mussorgsky, i write:

msginit --input=mussorgsky.pot --locale=es_ES

It will generate a file "es.po" and translators can/must work on that file.

If you don't know the locale of a language, can check this list.


Include translations in your installation

TODO