Current

This document is valid for the current version of Plone.

Marking translatable strings in code

by Plone Documentation Team last modified Dec 12, 2009 10:33 PM
Contributors: Mikko Ohtamma, Martin Aspeli, Kamon Ayeva, Israel Saeta Pérez

Each module declares its own MessageFactory which is a callable and marks strings with translation domain. MessageFactory is declared at main __init__.py file of your package.

from zope.i18nmessageid import MessageFactory

# your.app.package must match domain declaration in .po files
 yourAppMessageFactory = MessageFactory('your.app.package')

You also need to have the following ZCML entry:

<configure
    xmlns:i18n="http://namespaces.zope.org/i18n">

    <i18n:registerTranslations directory="locales" />

</configure>

After the set-up above you can use message factory to mark strings with translation domains.

i18ndude translation utilities seek underscore _ function to mark translatable strings (gettext message ids). So i18ndude will harvest all text from Python files which is inside _() function, no matter from where the function is actually imported. Message ids must be unicode strings.

from your.app.package import yourAppMessageFactory as _

my_translatable_text = _(u"My text") # my_text is zope.

The object will still look like a string:

>>> my_translatable_text
u'Test'

But in reality it is zope.i18nmessageid.message.Message object:

>>> my_translatable_text.__class__
<type 'zope.i18nmessageid.message.Message'>

>>> my_translatable_text.domain
'your.app.package'