Chameleon Internationalization

注釈

This recipe was created to document the process of internationalization (i18n) and localization (l10n) of chameleon templates. There is not much to it, really, but as the author was baffled by this fact, it seems a good idea to describe the few necessary steps.

We start off with a virtualenv and a fresh Pyramid project created via paster:

1$ virtualenv --no-site-packages env
2$ env/bin/pip install pyramid
3$ env bin/paster create -t pyramid_routesalchemy ChameleonI18n

Dependencies

First, add dependencies to your Pyramid project's setup.py:

 1requires = [
 2    ...
 3    'Babel',
 4    'lingua',
 5    ]
 6...
 7message_extractors = { '.': [
 8    ('**.py',   'lingua_python', None ),
 9    ('**.pt',   'lingua_xml', None ),
10    ]},

You will have to run ../env/bin/python setup.py develop after this to get Babel and lingua into your virtualenv and make the message extraction work.

A Folder for the locales

Next, add a folder for the locales POT & PO files:

1$ mkdir chameleoni18n/locale

What to translate

Well, let's translate some parts of the given template mytemplate.pt. Add a namespace and an i18n:domain to the <html> tag:

-<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" xmlns:tal="http://xml.zope.org/namespaces/tal">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" xmlns:tal="http://xml.zope.org/namespaces/tal"
+      xmlns:i18n="http://xml.zope.org/namespaces/i18n"
+      i18n:domain="ChameleonI18n">

The important bit -- the one the author was missing -- is that the i18n:domain must be spelled exactly like the POT/PO/MO files created later on, including case. Without this, the translations will not be picked up.

If your templates are organized in a template hierarchy, you must include i18n:domain in every file that contains messages to extract:

-<tal:block>
+<tal:block i18n:domain="ChameleonI18n">

So now we can mark a part of the template for translation:

-          <h2>Search documentation</h2>
+          <h2 i18n:translate="search_documentation">Search documentation</h2>

The i18n:translate attribute tells lingua to extract the contents of the h2 tag to the catalog POT. You don't have to add a description (like in this example 'search_documentation'), but it makes it easier for translators.

Commands for Translations

Now you need to run these commands in your project's directory:

1(env)$ python setup.py extract_messages
2(env)$ python setup.py init_catalog -l de
3(env)$ python setup.py init_catalog -l fr
4(env)$ python setup.py init_catalog -l es
5(env)$ python setup.py init_catalog -l it
6(env)$ python setup.py update_catalog
7(env)$ python setup.py compile_catalog

Repeat the init_catalog step for each of the languages you need.

The first command will extract the strings for translation to your project's locale/<project-name>.pot file, in this case ChameleonI18n.pot

The init commands create new catalogs for different languages and the update command will sync entries from the main POT to the languages POs.

At this point you can tell your translators to go edit the po files :-) Otherwise the translations will remain empty and defaults will be used.

Finally, the compile command will translate the POs to binary MO files that are actually used to get the relevant translations.

注釈

The gettext sub-directory of your project is locale/ in Pyramid, and not i18n/ as it was in Pylons. You'll notice that in the default setup.cfg of a Pyramid project, which has all the necessary settings to make the above commands work without parameters.

Add locale directory to projects config

At this point you'll also need to add your local directory to your project's configuration:

def main(...):
   ...
   config.add_translation_dirs('YOURPROJECT:locale')

where YOURPROJECT in our example would be 'chameleoni18n'.

Set a default locale

You can now change the default locale for your project in development.ini and see if the translations are being picked up.

1-  pyramid.default_locale_name = en
2+  pyramid.default_locale_name = de

Of course, you need to have edited your relevant PO file and added a translation of the relevant string, in this example search_documentation and have the PO file compiled to a MO file. Now you can fire up you app and check out the translated headline.