[Product-Developers] how to translate strings in view class (Plone 4)

Carsten Senger senger at rehfisch.de
Mon Jan 18 14:45:36 UTC 2010


Hi Kees,

--On Montag, Januar 18, 2010 15:03:02 +0100 Kees Hink <hink at gw20e.com> 
wrote:

> I'm working on a Plone 4 product, starting from Martin's excellent
> dexterity development manual
> (http://plone.org/products/dexterity/documentation/manual/developer-manua
> l).
>
> My problem is I can't seem to get all strings translated. The product
> contains a dexterity-based content type, for which translatations are
> working fine. The translation file is in the i18n/ dir.
>
> The product also contains a page view (Products.Five.BrowserView), which
> is unrelated to the content type. Calling '_(u"label_whatever",
> default="Some message")' from the view class only returns the
> 'label_whatever', even if i submit strings which are translated well
> otherwise. I've tested this by putting a pdb break in one the view method
> of interest.
>
> '_' is the same messagefactory the content type uses, it's simply
> zope.i18nmessageid.MessageFactory("my_i18n_domain"). The '_domain'
> attribute appears to be set correctly.

I don't know why _() doesn't work. Did you try to use i18n:* attributes
in the template?

> I've browsed through
> http://plone.org/documentation/manual/upgrade-guide/version/upgrading-plo
> ne-3-x-to-4.0/updating-add-on-products-for-plone-4.0/referencemanual-all-
> pages and the code examples in
> http://dev.plone.org/collective/changeset/100277 to find an alternative
> way to do it. However, using zope.i18n.translate didn't work for me,
> which may also be of interest:
> (Pdb) from zope.i18n import translate
> (Pdb) translate('no_url', 'my_i18n_domain', context=self.context)
> *** TypeError: ('Could not adapt', <PloneSite at /plone>, <InterfaceClass
> zope.i18n.interfaces.IUserPreferredLanguages>)


The kwarg "context" in translate has a misleading name. The context of
translate has to be a request object::

  translate('no_url', 'my_i18n_domain', context=self.request)

..Carsten




More information about the Product-Developers mailing list