November 21 2007posted by Santi

How to internationalize in KMKey

As you know, we use CPS as platform, but we also want to use Five when possible, so most of code produced be ready to jump to new versions of Zope.   One of firsts doubts when beginning to develop over CPS is how to internationalize our code.   CPS 3.4 uses Localizer, not Zope 3 internationalitzation utilities.   The better aproach was that one:

1) Use Z3 way to translate python code.   There are a lot of useful utilities to perform advanced operations, like variable interpolation, dates and times localizations and obtain names of days of week.   There is a good example in KMKeyResources/ranges.py.

    from zope.i18nmessageid import MessageFactory
    from zope.i18n import translate
    from Products.KMKeyCore.interfaces import IKMUserLanguage

    _ = MessageFactory('kmkeyresources')

    lang = IKMUserLanguage(self.context).getLanguage()
    msgid  = _(u'End DateTime should be greater than Start DateTime')
    msgstr = translate(msgid, target_language=lang)
    raise ValidationError(msgstr)


2) Use Five to create all new ZPT's, and Z3 way to translate them.    We just must take care to use .pt extension (to be detected by the extractor), and to use the i18n:domain=”kmkeyresouces” to force our own translations domain.   We will have a domain name per product.

3) It will remain some little thinks that should be translated using CPS way, like layout labels or type actions.   To proceed do the following:

    3. 1) Add an i18n directory in your Product
    3. 2) Add a default.pot in it
    3. 3) Add your msgid and msgstr manually
    3. 4) Create a profile (or add to existing), with a profiles/default/localizer/default.xml on it

        <?xml version="1.0"?>
        <object name="default">
         <translations product="KMKeyResources"/>
        </object>

    3. 5) Register your profile from your __init__ (see CPS products to take one example)
    3. 6) Merge existing translations using:  msgmerge -o es.po -s es.po default.pot


In each product, we should create a locales structure:

    cd KMKeyResources
    mkdir locales
    mkdir es
    mkdir es/LC_MESSAGES

   
To maintain .pot files updated from our python and ZPT code, we could use the Zope3 extractor.   If you have a Z3 instance, it could be called using:

    /usr/local/z3instance/zope/bin/i18nextract -p . -o locales -d kmkeyresources

And to merge with existing .po's

     /usr/local/z3instance/zope/bin/i18nmergeall -l locales

Note we should also generate .mo files after edit them (usign poedit, kbabel, or some other editor).  I could be done using

    cd es/LC_MESSAGES
    msgfmt -o kmkeyresources.mo kmkeyresources.po

Finally, remeber to register your translation domain in ZCML:

    <configure
        xmlns="http://namespaces.zope.org/zope"
        xmlns:i18n="http://namespaces.zope.org/i18n"
        xmlns:browser="http://namespaces.zope.org/browser"
        xmlns:five="http://namespaces.zope.org/five"
        i18n_domain="kmkeyresources">

      <i18n:registerTranslations directory="locales" />