[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:
http://grok.zope.org/documentation/how-to/how-to-internationalize-your-application
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
http://python.6.n6.nabble.com/Get-grok-site-outside-the-server-in-an-external-script-tp1931799p1931801.html).

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"
		           xmlns:grok="http://namespaces.zope.org/grok"
		           xmlns:i18n="http://namespaces.zope.org/i18n"
		           i18n_domain="my_server">
	  <include package="grok" />
	  <include package="zope.i18n" />
	  <includeDependencies package="." />
	  <grok:grok package="." />
	  <grok:grok package="backlib.utils.languagePreferences.PreferredLanguage" />
	  <i18n:registerTranslations directory="locales" />
</configure>

(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