translation from python

7 messages Options
Embed this post
Permalink
Wichert Akkerman

translation from python

Reply Threaded More More options
Print post
Permalink
I'm running into some problems doing translations from python code. As
far as I can see none of the documentation on plone.org is up to date.
The code snippet I have now looks like this:

  from zope.i18n import translate

  filename=_("filename_actionplan_report",
             default=u"Inventory ${title}.doc")
  mapping=dict(title=dbsession.title)
  filename=translate(filename,
                     mapping=mapping,
                     context=self.request,
 
default=string.Template(filename.default).substitute(**mapping))
  self.request.response.setHeader("Content-Disposition",
                      u"attachment; filename=%s" % filename)
  self.request.response.setHeader("Content-Type", "application/msword")

I've found that unless you give a default to translate it will happily
return something useless if no translation catalog has an entry for your
string. That makes it a somewhat stupid API, but something you can
easily deal with. Since I have no translation catalog entry for my
message I would expect the return value for the translate call to be the
default I've passed in, but instead it returns u'Inventory
${title}.doc'. Does anyone know what the right magic invocation of
translate is?

Wichert.

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Plone-developers mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/plone-developers
Mikel Larreategi

Re: translation from python

Reply Threaded More More options
Print post
Permalink
Wichert Akkerman(e)k dio:

> I'm running into some problems doing translations from python code. As
> far as I can see none of the documentation on plone.org is up to date.
> The code snippet I have now looks like this:
>
>   from zope.i18n import translate
>
>   filename=_("filename_actionplan_report",
>              default=u"Inventory ${title}.doc")
>   mapping=dict(title=dbsession.title)
>   filename=translate(filename,
>                      mapping=mapping,
>                      context=self.request,
>  
> default=string.Template(filename.default).substitute(**mapping))
>   self.request.response.setHeader("Content-Disposition",
>                       u"attachment; filename=%s" % filename)
>   self.request.response.setHeader("Content-Type", "application/msword")
>
> I've found that unless you give a default to translate it will happily
> return something useless if no translation catalog has an entry for your
> string. That makes it a somewhat stupid API, but something you can
> easily deal with. Since I have no translation catalog entry for my
> message I would expect the return value for the translate call to be the
> default I've passed in, but instead it returns u'Inventory
> ${title}.doc'. Does anyone know what the right magic invocation of
> translate is?
>

I found myself passing explicitly the target language to the translate
method:

translate(filename,
           mapping=mapping,
           target_language=self.request.LANGUAGE,
           context=self.request,
          )



Mikel

--
Mikel Larreategi
[hidden email]

CodeSyntax
Azitaingo Industrialdea 3 K
E-20600 Eibar
Tel: (+34) 943 82 17 80


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Plone-developers mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/plone-developers
Wichert Akkerman

Re: translation from python

Reply Threaded More More options
Print post
Permalink
On 10/13/09 11:46 , Mikel Larreategi wrote:

>
>
> Wichert Akkerman(e)k dio:
>> I'm running into some problems doing translations from python code. As
>> far as I can see none of the documentation on plone.org is up to date.
>> The code snippet I have now looks like this:
>>
>> from zope.i18n import translate
>>
>> filename=_("filename_actionplan_report",
>> default=u"Inventory ${title}.doc")
>> mapping=dict(title=dbsession.title)
>> filename=translate(filename,
>> mapping=mapping,
>> context=self.request,
>>
>> default=string.Template(filename.default).substitute(**mapping))
>> self.request.response.setHeader("Content-Disposition",
>> u"attachment; filename=%s" % filename)
>> self.request.response.setHeader("Content-Type", "application/msword")
>>
>> I've found that unless you give a default to translate it will happily
>> return something useless if no translation catalog has an entry for
>> your string. That makes it a somewhat stupid API, but something you
>> can easily deal with. Since I have no translation catalog entry for my
>> message I would expect the return value for the translate call to be
>> the default I've passed in, but instead it returns u'Inventory
>> ${title}.doc'. Does anyone know what the right magic invocation of
>> translate is?
>>
>
> I found myself passing explicitly the target language to the translate
> method:
>
> translate(filename,
> mapping=mapping,
> target_language=self.request.LANGUAGE,
> context=self.request,
> )

It appears the problem is that passing a mapping to translate() does not
work: you have to pass it in to the MessageFactory call. That poses a
bit of a problem in situations where you do not know the mapping in
advance, but I suspect modifying the mapping attribute of a Message
instance works.

Wichert.

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Plone-developers mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/plone-developers
Hedley Roos

Re: translation from python

Reply Threaded More More options
Print post
Permalink
In reply to this post by Mikel Larreategi
> I found myself passing explicitly the target language to the translate
> method:
>
> translate(filename,
>            mapping=mapping,
>            target_language=self.request.LANGUAGE,
>            context=self.request,
>           )
>
>
>
> Mikel
>

Yep, have a look at CMFPlone/tests/messages.txt to see how it's done.

Hedley

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Plone-developers mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/plone-developers
Hanno Schlichting-4

Re: translation from python

Reply Threaded More More options
Print post
Permalink
In reply to this post by Wichert Akkerman
Hi.

On Tue, Oct 13, 2009 at 11:33 AM, Wichert Akkerman <[hidden email]> wrote:
> I've found that unless you give a default to translate it will happily
> return something useless if no translation catalog has an entry for your
> string. That makes it a somewhat stupid API, but something you can
> easily deal with. Since I have no translation catalog entry for my
> message I would expect the return value for the translate call to be the
> default I've passed in, but instead it returns u'Inventory
> ${title}.doc'. Does anyone know what the right magic invocation of
> translate is?

Welcome to the lovely world of zope.i18n language negotiation. Yes
indeed, you need a message catalog for every language and domain
combination or it won't give you what you want.

This is a regression from the PTS code, which had a more sensible
behavior and would allow you to get the default message in English
without having to have a (empty) message catalog.

I consider this a bug and will try to fix it. PTS still has some say
in the negotiation and it should be able to make zope.i18n behave.

Hanno

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Plone-developers mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/plone-developers
Mikel Larreategi

Re: translation from python

Reply Threaded More More options
Print post
Permalink
Hanno Schlichting(e)k dio:

> Hi.
>
> On Tue, Oct 13, 2009 at 11:33 AM, Wichert Akkerman <[hidden email]> wrote:
>> I've found that unless you give a default to translate it will happily
>> return something useless if no translation catalog has an entry for your
>> string. That makes it a somewhat stupid API, but something you can
>> easily deal with. Since I have no translation catalog entry for my
>> message I would expect the return value for the translate call to be the
>> default I've passed in, but instead it returns u'Inventory
>> ${title}.doc'. Does anyone know what the right magic invocation of
>> translate is?
>
> Welcome to the lovely world of zope.i18n language negotiation. Yes
> indeed, you need a message catalog for every language and domain
> combination or it won't give you what you want.
>
> This is a regression from the PTS code, which had a more sensible
> behavior and would allow you to get the default message in English
> without having to have a (empty) message catalog.
>
> I consider this a bug and will try to fix it. PTS still has some say
> in the negotiation and it should be able to make zope.i18n behave.
>

This reply seems to explain the behaviour explained in this bug:

http://dev.plone.org/plone/ticket/9089


Mikel



--
Mikel Larreategi
[hidden email]

CodeSyntax
Azitaingo Industrialdea 3 K
E-20600 Eibar
Tel: (+34) 943 82 17 80


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Plone-developers mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/plone-developers
Hanno Schlichting-4

Re: translation from python

Reply Threaded More More options
Print post
Permalink
On Tue, Oct 13, 2009 at 11:58 AM, Mikel Larreategi
<[hidden email]> wrote:

> Hanno Schlichting(e)k dio:
>> This is a regression from the PTS code, which had a more sensible
>> behavior and would allow you to get the default message in English
>> without having to have a (empty) message catalog.
>>
>> I consider this a bug and will try to fix it. PTS still has some say
>> in the negotiation and it should be able to make zope.i18n behave.
>
> This reply seems to explain the behaviour explained in this bug:
>
> http://dev.plone.org/plone/ticket/9089

Sure. It's a known bug that exists for quite some time. With Plone 4.0
it just becomes more obvious, as we use the same logic to handle po
files in i18n and locales folders.

Hanno

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Plone-developers mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/plone-developers