Karrigell Documentation

Version 2.2.1 13 12 05

Français

15. Internationalization

As you'll have guessed by reading this documentation, I'm not from an English-speaking country (I'm French, and more precisely Breton - the name Karrigell is a Breton word). So I've included a program to facilitate internationalization of scripts

15.1 Introduction

Translation is made using the gettext module provided in Python standard library. The tranlation is held in a text file with a .po extension, in a directory called language/LC_MESSAGES ; in this file the english message is on a line beginning with msgid and the translation on the next line beginning with msgstr. For instance :

msgid "Server got itself in trouble"
msgstr "Quelque chose ne va pas dans le serveur"

This text file is transformed into a binary file with a .mo extension by a Python program called msgfmt.py, written by Martin v. Löwis (it's included in Karrigell's Server Directory)

In a script, every time you want a message translated into a given language, instead of writing the message as a normal string with quotes, it's written using a function called _, this way :

print _("Server got itself in trouble")

In order to have it translated into the requested language :

import gettext
t=gettext.translation(domain,baseDir,language)
t.install()

where domain is the name of the .po file, baseDir the directory in which the subdirectory language/LC_MESSAGES is stored, and language is the language used

For instance, if the language is "francais" and the .po file is called messages.po, stored in c:\My documents\Translations\francais\LC_MESSAGES you'll write :

import gettext
t=gettext.translation("messages","c:\My documents\Translations","francais")
t.install()

15.2 Internationalization program

The raw use of gettext may be difficult when writing or debugging a program ; for each new string you want to translate you have to create the whole .po file then the .mo file without erasing previous translations. Karrigell provides a simple web interface to create and modify translations of strings, hiding the underlying gettext mechanism

For security reasons, this script is reserved to the administrator. An authentication script is run, relying on md5 digests stored in a file called admin.ini, which the administrator must set by running the script k_password.py

With your browser, call the script http://localhost/admin/internat.pih . It opens a directory browser with which you can access all the files which may contain strings to translate (that is, all the files with an extension .py, .pyw, .pih, .hip). When clicking on a file name, a page appears with all the strings to tranlate (the arguments of the _() function) and asks for a translation in all the langages currently chosen in the browser language preferences(1). If translations have already been made they appear in the form fields

Fill in the fields and validate the form ; this creates or updates the translations

You can check the effect by calling the script you've just modified and changing the language order in the preferences

Translation is held in a file which is common to all the files in the same directory. You can also edit the whole dictionary clicking on the first item in the script list


(1) On Microsoft Internet Explorer the language preference is set by Tools/Internet Options/General/Languages ; accepted languages are chosen from a list and ordered by preference. On Netscape use Edition/Preferences/Languages