[silva-dev] i18n-translation depending on document language
Samuel Schluep
schluep at ethz.ch
Wed May 10 18:36:51 CEST 2006
Hi altogether
> Is it possible to translate specific strings by the help of the Five
> machinery depending on the document's language setting. The standard
> i18n procedure for Silva is targeted mainly at the translation of the
> SMI. In my case, I would like to translate the label of a forms button
> in a Silva extension which shows up on public pages. In this case it
> makes more sense to set the language to the document's language rather
> than the user preferences (set in either Silva's user settings or the
> user's browser setting). This translation might be performed in a
> Python Script.
I have just found a solution on my own. In case someone is interested,
here it is:
In my helpers.py module I created the following function (the functions
in helpers.py are enabled to be used in Python Scripts):
def translate_to_doc_lang(msgid, model):
"""Translate a messageid to the language of the document
Translates a string to the language of the Silva object being rendered
using the dlcms domain.
Arguments:
msgid -- a MessageId object containing the string and the domain
model -- the Silva object being rendered
"""
from zope.i18n import translate
# get language setting of Silva object being rendered
try:
binding = model.service_metadata.getMetadata(model)
lang = binding.get('silva-extra', 'language')
except:
# No binding found..
lang = 'en'
lang = lang.lower() # silva-extra uses upper-case codes which do
not translate
return translate(msgid, target_language=lang)
In the script I then use something like the following (note I use this
in the SilvaDLCMS extension product):
from Products.SilvaDLCMS.i18n import translate as _
from Products.SilvaDLCMS.helpers import translate_to_doc_lang
model = context.REQUEST.model
return '<input type="button" onClick="doSomething(this.form)" value=" ' + \
translate_to_doc_lang(_('press the button'), model) + \
'" />'
Et voilà, the message is shown in the language specified by the language
metadata setting, provided there is a translation somewhere.
Regards
Sam
More information about the silva-dev
mailing list