[Zope-dev] force the language for a particular template: solution

Marius Gedminas marius at gedmin.as
Sat Apr 18 07:43:53 EDT 2009


On Sat, Apr 18, 2009 at 12:34:19AM +0100, Chris Withers wrote:
> Hanno Schlichting wrote:
> > Or attach a marker interface to the request and register a different
> > IUserPreferredLanguages adapter for it.
> 
> This won't quite work as I still need to get the language I'm forcing 
> from somewhere.
> 
> However, I realised I already had a custom IUserPreferredLanguages 
> adapter on this project (to get the language from the user object, not 
> the browser) so I added a bit of code in there. The simplest version of 
> this would be:
> 
> from zope.publisher.browser import BrowserLanguages
> 
> class Languages(BrowserLanguages):
> 
>      def getPreferredLanguages(self):
>          force = getattr(self.request,'_force_language',None)
>          if force:
>              return [force]
> 	return super(Languages, self).getPreferredLanguages()
> 
> Then this zcml:
> 
>    <adapter
>       for="zope.publisher.interfaces.http.IHTTPRequest"
>       provides="zope.i18n.interfaces.IUserPreferredLanguages"
>       factory=".languages.Languages"
>      />
> 
> ...and then you can do the following in a view:
> 
>      def __call__(self):
>          def1 = self.template(self)
>          self.request._force_language = 'de'

Does this work?  IIRC Zope 3 request objects use __slots__ and therefore
cannot have any extra attributes imposed upon them from the outside.

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/20090418/9a55b428/attachment.bin 


More information about the Zope-Dev mailing list