zope.i18n.translate overrides mapping if passed a message

Bug #592753 reported by Christian Theune
14
This bug affects 3 people
Affects Status Importance Assigned to Milestone
zope.i18n
Won't Fix
Medium
Christian Theune

Bug Description

When using the module-level translation function passing in a message ID like shown below the interpolation will not happen because the translate function overrides the passed-in mapping with the (empty) default of the message object.

from foo import MessageFactory as _
X = _('some ${data}')
zope.i18n.translate(X, mapping=dict(data=1))

A work around for users is to explicitly turn the message ID back into a unicode object:

zope.i18n.translate(unicode(X), mapping=dict(data=1))

Revision history for this message
Vincent Fretin (vincent-fretin) wrote :

I usually do:

X = _('some ${data}', mapping=dict(data=1))
zope.i18n.translate(X, context=self.request)

But indeed when you use X as a constant, you don't have necessary the mapping right away.
In this case, what I do is to create a new Message object:

from zope.i18nmessageid import Message
zope.i18n.translate(Message(X, mapping=dict(data=1)), context=self.request)

This is the preferred way to do it right now. You lose the domain and default message when you do unicode(X).

Indeed, it may be less verbose to write:
zope.i18n.translate(X, mapping=dict(data=1), context=self.request)

If somebody change the code of the translate function so you can override mapping like this, I think in this case you should be able to override domain and default as well.

Revision history for this message
Vincent Fretin (vincent-fretin) wrote :

Christian: Do we replace or update the mapping that's on the message object?

When you create a Message object from a Message object, it replaces the mapping completely, so I'd say we should do the same here, replace the mapping.

Revision history for this message
Michael Howitz (mh-gocept) wrote :

Currently it is only possible to override the existing mapping but it might be helpful (in some cases) to update it.
I see no need to remove keys from the mapping, so it would be always perfect to update the mapping.

Revision history for this message
Christian Theune (ctheune) wrote :

This needs more discussion. My analysis and discussion is here:
https://mail.zope.org/pipermail/zope-dev/2010-October/041906.html

Changed in zope.i18n:
status: New → Incomplete
importance: Undecided → Medium
assignee: nobody → Christian Theune (ct-gocept)
Revision history for this message
Tres Seaver (tseaver) wrote :
Changed in zope.i18n:
status: Incomplete → Won't Fix
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.