[Zope-dev] Getting translation of python code string to work

Leonardo Rochael Almeida leorochael at gmail.com
Sun Jun 5 18:10:10 EDT 2011


Hi Morten,

> OK.  Well I have this function now:
>
> def _(msgid, request):
>    language = request['LANGUAGE']
>    return translate(msgid, domain='SimpleChat', target_language=language)
>
> Which sends all the relevant bits to the translate function.  But, this
> doesn't work either, and I can see it is because the interpolate function
> call is used in translate.

It seems you misunderstood my explanation (or I misunderstood yours),
but are you trying to use your custom _() function instead of using a
MessageFactory?

the translation() function should be used WITH the results of calling
MessageFactory() objects, not INSTEAD of them.

First you create MessageFactory() objects in your code when creating
messages, preferably as early and as little as possible. THEN, you
call translate() on them, as late as possible, and as close as
possible to the time where the translation is to be transmitted to the
user.

> So do I need to setup some utility?

In theory, IMessageDomain utilities need to be registered, but if
translations are working inside Page Templates, then this is already
the case.

These utilities need to be configured before the translate() call
happens (for example, when it is called implicitly during ZPT
rendering), but don't need to be configured yet when calling _("some
message") (assuming the "_" object is a MessageFactory instance).

I hope I'm not making this even more of a mess for you to understand...

Cheers,

Leo

On Wed, Jun 1, 2011 at 09:02,  <lists at nidelven-it.no> wrote:
> [...]
>> On Tue, May 31, 2011 at 13:19,  <lists at nidelven-it.no> wrote:
>>> Hi,
>>>
>>> I have a oldschool style Zope 2 product, which has an i18n
>>> directory containing translations.
>>>
>>> Using i18n:domain="SimpleChat" in this product works fine in
>>> the page templates,
>>
>> Well, ZPTs generate message objects automatically from translated
>> content, but they also explicitly translate all the message objects
>> they get during interpolation between the literal parts of the
>> template and the dynamically generated ones.
>>
>>> but when I start translating text in
>>> a Python module using _("Translate me") I just get English
>>> text (instead of Norwegian, which is what I want).
>>
>> When you say "I just get English text", what does exactly "get" mean
>> in terms of code?
>>
>> Mind you, if you simple call unicode(message) you'll most likely only
>> get the English version back. Same thing if you do:
>>
>>   u"some other string: %s" % message
>>
>> To get an actual translation, you need to call
>> zope.i18n.translate(message), eventually passing the
>> "target_languate=..." parameter.
>>
>> Take a look at the zope.i18n module for details, specifically the
>> translate() and negotiate() functions.
>
> OK.  Well I have this function now:
>
> def _(msgid, request):
>    language = request['LANGUAGE']
>    return translate(msgid, domain='SimpleChat', target_language=language)
>
> Which sends all the relevant bits to the translate function.  But, this
> doesn't work either, and I can see it is because the interpolate function
> call is used in translate.
>
> So do I need to setup some utility?
>
> -Morten
>
>


More information about the Zope-Dev mailing list