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

2009-04-19 Thread Chris Withers
Marius Gedminas wrote:
 Does this work?  IIRC Zope 3 request objects use __slots__ and therefore
 cannot have any extra attributes imposed upon them from the outside.

Good thing I'm using Zope 2 request objects then ;-)

Does a Zope 3 request still haev an 'other' dictionary? If so, you could 
stick it in there...

Chris

-- 
Simplistix - Content Management, Zope  Python Consulting
- http://www.simplistix.co.uk
___
Zope-Dev maillist  -  Zope-Dev@zope.org
http://mail.zope.org/mailman/listinfo/zope-dev
**  No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope )


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

2009-04-18 Thread Marius Gedminas
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


signature.asc
Description: Digital signature
___
Zope-Dev maillist  -  Zope-Dev@zope.org
http://mail.zope.org/mailman/listinfo/zope-dev
**  No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope )


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

2009-04-17 Thread Chris Withers
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'
 de = self.template(self)
 del self.request._force_language
 def2 = self.template(self)
 return def1+de+def2

Hope someone else finds this useful some day :-)

Chris

-- 
Simplistix - Content Management, Zope  Python Consulting
- http://www.simplistix.co.uk
___
Zope-Dev maillist  -  Zope-Dev@zope.org
http://mail.zope.org/mailman/listinfo/zope-dev
**  No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope )