In order to prepare your code for I18N, you need to look at all the
strings in your files. Any string that needs to be translated
should be marked by wrapping it in _('...')
-- that is, a call
to the function _(). For example:
filename = 'mylog.txt' message = _('writing a log message') fp = open(filename, 'w') fp.write(message) fp.close()
In this example, the string 'writing a log message'
is marked as
a candidate for translation, while the strings 'mylog.txt'
and
'w'
are not.
The Python distribution comes with two tools which help you generate the message catalogs once you've prepared your source code. These may or may not be available from a binary distribution, but they can be found in a source distribution, in the Tools/i18n directory.
The pygettext21.3 program
scans all your Python source code looking for the strings you
previously marked as translatable. It is similar to the GNU
gettext program except that it understands all the
intricacies of Python source code, but knows nothing about C or C++
source code. You don't need GNU gettext
unless you're also
going to be translating C code (such as C extension modules).
pygettext generates textual Uniforum-style human readable message catalog .pot files, essentially structured human readable files which contain every marked string in the source code, along with a placeholder for the translation strings. pygettext is a command line script that supports a similar command line interface as xgettext; for details on its use, run:
pygettext.py --help
Copies of these .pot files are then handed over to the individual human translators who write language-specific versions for every supported natural language. They send you back the filled in language-specific versions as a .po file. Using the msgfmt.py21.4 program (in the Tools/i18n directory), you take the .po files from your translators and generate the machine-readable .mo binary catalog files. The .mo files are what the gettext module uses for the actual translation processing during run-time.
How you use the gettext module in your code depends on whether you are internationalizing a single module or your entire application. The next two sections will discuss each case.