[Grok-dev] Use grok/zope i18 translation mechanisms in external non grok/zope application

Hector Blanco white.lists at gmail.com
Fri Apr 13 16:41:24 UTC 2012

Hello everyone.

I have a web server (called "my_server", of class
"my_server.app.My_server") that is properly using the i18 translation
features implemented in Grok. I followed this guide:
and the translations are working very well when "inside" the server.

Now, I have another Python application that is non-related to this
Grok/Zope server (it just uses its ZoDB for persistence and as a
"common storage" area) which could actually benefit from the
translation infrastructure provided by Grok/Zope.

Is there any way of accessing my server's translations from an
external application? Said external application is executed via an
script generated as an "entry point" of the "my_server" package. I can
actually invoke the script with "my_server/bin/myStartupScript" so at
least the paths used for the "my_server" application, like
$HOME/.buildout/ and all that should be available (thanks to Kevin
Teague for the hint he gave me on that a while ago

The closest thing I've been able to do is loading the catalogs and
translate through them:

>>> catalog = zope.i18n.gettextmessagecatalog.GettextMessageCatalog("zh", "zope-i18n", "/home/yomismo/my_server/src/my_server/locales/zh/LC_MESSAGES/my_server.mo")
>>> print catalog.getMessage("Next Step")

This works, and I could always use this as a fallback, but I would
like to be able to call "translate" and get the translations as I can
do when the "my_server" application is running:

>>> zope.i18n.translate(u"Next Step", domain="zope-i18n", context=None, mapping=None, target_language="zh", default=None)
u'Next Step'

This doesn't work. I've tried many variations...
>>> zope.i18n.translate(u"Next Step", domain="my_server", context=None, mapping=None, target_language="zh", default=None)
>>> zope.i18n.translate(u"Next Step", domain="my_server", context=app.My_server, mapping=None, target_language="zh", default=None)

... all of them without luck ...

I am attaching my
"/home/yomismo/my_server/src/my_server/configure.zcml", just in case
it helps, but it looks pretty generic to me:

<configure xmlns="http://namespaces.zope.org/zope"
	  <include package="grok" />
	  <include package="zope.i18n" />
	  <includeDependencies package="." />
	  <grok:grok package="." />
	  <grok:grok package="backlib.utils.languagePreferences.PreferredLanguage" />
	  <i18n:registerTranslations directory="locales" />

(The PreferredLanguage is the application that returns the language
the user prefers. It basically reads a cookie from the HTTPRequest)

My only guess (which is probably wrong) is that when I start the
server (with "my_server/bin/paster") something gets registered or read
or... something and this doesn't happen when the translation is called
from an outside script.

Any hint will be appreciated!

Thank you in advance.

More information about the Grok-dev mailing list