[Grok-dev] Trigger i18n translation in the rendering method of a view

Sebastian Ware sebastian at urbantalk.se
Mon Sep 26 08:30:48 EST 2011


Thanks!

It seems that I will need to do something like this to make the code concise.
Does this look reasonable?

  from translation import ApplicationMessageFactory as MsgF
  from zope.i18n import translate
  class HelpTranslate(grok.View):
      def render(self):
          def _(txt):
              return translate(MsgF(txt), context=self.request)
          outp = []
          outp.append(_("Alla glada?"))
          return "".join(outp)


Mvh Sebastian


26 sep 2011 kl. 15.17 skrev Souheil CHELFOUH:

> This is because the request is the object containing the browser
> information related to the language, it's all stored in the Locales
> object in the request
> 
> 2011/9/26 Sebastian Ware <sebastian at urbantalk.se>:
>> Turns out one needed to supply the request-object as context. This works:
>> 
>>  from zope.i18n import translate
>>  class ForceTranslate(grok.View):
>>      def render(self):
>>          return translate(_("Allt val?"), context=self.request)
>> 
>> Mvh Sebastian
>> 
>> 
>> 26 sep 2011 kl. 14.56 skrev Alexandre Garel:
>> 
>>> Le 26/09/2011 14:43, Sebastian Ware a écrit :
>>>> I can get i18n translation to work fine in PageTemplates, but how do I force translation of text that isn't inserted into templates?
>>>> 
>>>> I want to do this and get the string to be translated:
>>>> 
>>>>   from . import ApplicationMessageFactory as _
>>>>   class TestTranslate(grok.View):
>>>>       def render(self):
>>>>           return _("I need to be translated")
>>>> 
>>>> I have set my breakpoints and I am trying to figure out how the TAL-engine does this, but I am not making much progress.
>>> 
>>> In fact  ApplicationMessageFactory  just return a string containing
>>> informations for translation.
>>> 
>>> You need :
>>> 
>>> from zope.i18n import translate
>>> return translate(_("I need to be translated"))
>>> 
>>> and this shall be ok, I think.
>>> 
>>> Hope this helps,
>>> 
>>> Alex
>>> _______________________________________________
>>> Grok-dev mailing list
>>> Grok-dev at zope.org
>>> https://mail.zope.org/mailman/listinfo/grok-dev
>> 
>> 
>> _______________________________________________
>> Grok-dev mailing list
>> Grok-dev at zope.org
>> https://mail.zope.org/mailman/listinfo/grok-dev
>> 




More information about the Grok-dev mailing list