[Grok-dev] I18n howto

Vincent Fretin vincent.fretin at gmail.com
Sat Jan 12 12:48:53 EST 2008

Hello everybody,

My name is Vincent Fretin, I'm a french student in computer science,
22 years old.
I'm interested in zope3 and grok. I tested zope3 first, now I'm
testing grok and it looks pretty cool.
I read a lot of documentation about i18n, old or new, for zope2, five,
zope3 and so on...
I saw there is no i18n how-to on the grok.zope.org, so I made a very
quick how-to to internationalize your grokproject.
I hope it will be useful. Feel free to add it to the how-to section,
so anybody can update it.
Let me know if I'm doing the right thing or if there is a simpler way
to do this.
I don't have time to polish it, sorry, here it is:
How to internationalize your grok application
Author: Vincent Fretin (vincent dot fretin (AT) gmail dot com)

Modify configure.zcml like this:
<configure xmlns="http://namespaces.zope.org/zope"
  <include package="grok" />
  <grok:grok package="." />
  <i18n:registerTranslations directory="locales" />

in app.py, add :
from zope.i18nmessageid import MessageFactory
_ = MessageFactory('PROJECTNAME')
replace PROJECTNAME by your project

now to internationalize your string, use:
_(u'Hello World')
You will have in your generated POT:
msgid "Hello World"
msgstr ""

_(u'hello_msg', u'Hello World')
You will have in your generated POT:
#. Default: "Hello World"
msgid "hello_msg"
msgstr ""

In a template :
<html i18n:domain="PROJECTNAME">
    <title>My project</title>
  <a tal:attributes="href python: view.url('creategame')"
     >Create new game</a>
  <a tal:attributes="href python: view.url('createcontact')"
     >Create new contact</a>

You will have in your generated POT:
#. Default: "Create new game"
msgid "link_create_new_game"
msgstr "Créer un nouveau jeu"

msgid "Create new contact"
msgstr ""

Extract all internationalizable string:
in your grokproject directory:

This generate src/PROJECTNAME/locales/PROJECTNAME.pot
Begin to translate into french (in your src directory):
mkdir -p fr/LC_MESSAGES
translate fr/LC_MESSAGES/PROJECTNAME.po with your favorite translation tool.

To update the po file later, in your grokproject directory:

Generate the mo file

Restart your server and see the translation in french.

Further reading :
http://worldcookery.com/files/fivei18n/ (Zope 3 part)

Best regards,
Vincent Fretin

More information about the Grok-dev mailing list