[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"
           xmlns:grok="http://namespaces.zope.org/grok"
           xmlns:i18n="http://namespaces.zope.org/i18n">
  <include package="grok" />
  <grok:grok package="." />
  <i18n:registerTranslations directory="locales" />
</configure>

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 ""

or
_(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">
<head>
    <title>My project</title>
</head>
<body>
  <a tal:attributes="href python: view.url('creategame')"
     i18n:translate="link_create_new_game"
     >Create new game</a>
  <a tal:attributes="href python: view.url('createcontact')"
     i18n:translate=""
     >Create new contact</a>
</body>
</html>

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:
./bin/i18nextract

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

To update the po file later, in your grokproject directory:
./bin/i18nextract
./bin/i18nmergeall

Generate the mo file
msgfmt -o fr/LC_MESSAGES/PROJECTNAME.mo fr/LC_MESSAGES/PROJECTNAME.po

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