[Zope3-Users] I18N: Recursive Translations / translations of mappings - how?

Hermann Himmelbauer dusty at qwer.tk
Thu Mar 6 02:47:23 EST 2008


Hi,
I have the quite generic problem that I need to translate strings that contain 
strings which also should be translated, for instance:

----------- snip ------------
from zope.component import provideUtility
from zope.i18n import translate
from zope.i18n.simpletranslationdomain import SimpleTranslationDomain
from zope.i18nmessageid import Message, MessageFactory
_ = MessageFactory('xyz')

messages = {
    ('en', u'pink'): u'pink',
    ('de', u'pink'): u'rosa',
    ('en', u'colour'): u'The colour is $pink',
    ('de', u'colour'): u'Die Farbe ist $pink'}

xyz = SimpleTranslationDomain('xyz', messages)
provideUtility(xyz, name=u'xyz')

pink = _(u'pink', u'pink')
pink = Message(pink)
print translate(pink, target_language='en')
print translate(pink, target_language='de')

colour = _(u'colour', u'The colour is $pink')
colour = Message(colour, mapping={'pink' : pink})
print translate(colour, target_language='en')
print translate(colour, target_language='de')
------------ snip -----------------

The output of the above program is:

------
pink
rosa
The colour is pink
Die Farbe ist pink
------

It can be seen that the string "pink" is not translated if used as mapping in 
another string to be translated.

One very helpful guy on Freenode suggested to solve this the following way:

colour = Message(colour, mapping={'pink' : translate=(pink, context=request)})

This works, however my problem is that the code is in another module that is 
and should not be aware of the context as it's not really zope-related, so 
It's somehow unclean to use the context/request in that module.

So I was wondering if it is possible to tell the translate() function to 
recursively translate the string, hence look at the mappings, check if they 
are of the type zope.i18nmessageid.message.Message and if yes, either descend 
into it and it's mappings and translate it until every mapping is translated, 
too.

What do you think?

Best Regards,
Hermann

-- 
hermann at qwer.tk
GPG key ID: 299893C7 (on keyservers)
FP: 0124 2584 8809 EF2A DBF9  4902 64B4 D16B 2998 93C7


More information about the Zope3-users mailing list