[Zope-dev] How can I force the language for a particular template rendering?

Marius Gedminas marius at gedmin.as
Fri Apr 17 17:32:26 EDT 2009


On Fri, Apr 17, 2009 at 03:35:58PM -0400, Ethan Jucovy wrote:
> How about just monkeypatching the active negotiator?
> 
> {{{
> negotiator = getUtility(zope.i18n.interfaces.INegotiator)
> orig = negotiator.getLanguage
> negotiator.getLanguage = lambda foo, bar: 'fr'
> text = my_page_template()
> negotiator.getLanguage = orig
> }}}
>
> Haven't tested it, but in my (limited) understanding of the I18N system
> something like that ought to work..

Two words: "thread safety".

Zope 3 utilities are global, not thread-local.

Unless you have a persistent utility (theoretically; the default
INegotiator isn't), which would be thread-local.  (And in which case
your example code would try to pickle a bound method---I wonder if
that'd work?  I think it might, but just consider the conflict errors
you'd get if every request did this.)

"Monkey-patching outside unit tests is evil and will explode when you
don't expect" is a good rule of thumb.

Marius Gedminas
-- 
http://pov.lt/ -- Zope 3 consulting and development
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.zope.org/pipermail/zope-dev/attachments/20090417/0093263d/attachment.bin 


More information about the Zope-Dev mailing list