Python-Ref > GUI programming with PyGTK > Localization using gettext > Localization introduction
 
 

^^->
Klíčová slova
Moduly
Knihovní funkce

Localization introduction

Gettext is a free software system designed to make localization of software as easy as possible.
To localize your application the following steps have to be followed:
  • mark the text for translation in your code
  • export the text using gettext tools
  • translate the text
  • load the translation into your code
Not all of the above steps are usually done by one person. Very often the translation is done by a different person (a translator) who does not have to know anything about programming. The gettext system clearly separates the roles of programmer and translator.
The following code describes the process in more detail.
At first we create the program, import gettext and install it. It will put a function called "_" into the global namespace. We then "mark" all translatable text using this function. By default this function returns the string we put into it. Once the proper localization is provided, it would return the translated text.
Expand/Shrink
  1   import gettext
  2   # arguments to install
  3   #  - domain
  4   #  - where to search for mo files
  5   #  - whether to convert translation strings to unicode
  6   gettext.install( "my_example", "./", False)
  7   print _ # this function is installed by gettext
  8   print _( "Hello world.")
stdout:
<bound method NullTranslations.gettext of <gettext.NullTranslations instance at 0x2b3ec1e455f0>>
Hello world.
Doba běhu: 47.3 ms
Now we have to create the translation file and edit it to provide the translations.
xgettext localization1-1.py -d my_example
Or if we already have the translation file, we may update it using:
xgettext -j localization1-1.py -d my_example
We may edit the file in any program we choose, but there are special programs, such as POedit or KBabel available.
Now we have to compile and install the translation.
msgfmt my_example.po -o my_example.mo
mkdir -p cs/LC_MESSAGES
mv my_example.mo cs/LC_MESSAGES
The compiled translation should be placed into a directory "lang/LC_MESSAGES" where lang is a two-letter code of the translation language.
In our setup the appropriate translation will be picked up based on system configuration. If we want to force one translation, we may use the LANG environment variable to do so.
LANG=cs python localization1-1.py
Expand/Shrink
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2008-04-28 15:40+0200\n"
"PO-Revision-Date: 2008-04-28 15:46+0200\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: localization1-1.py:4
msgid "Hello world."
msgstr "Ahoj světe."
Argumenty příkazové řádky:
LC_ALL=cs python localization1-1.py 
  1   import gettext
  2   # arguments to install
  3   #  - domain
  4   #  - where to search for mo files
  5   #  - whether to convert translation strings to unicode
  6   gettext.install( "my_example", "./", False)
  7   print _ # this function is installed by gettext
  8   print _( "Hello world.")
stdout:
<bound method GNUTranslations.gettext of <gettext.GNUTranslations instance at 0x2abfc06e33f8>>
Ahoj světe.
Doba běhu: 49.7 ms