[gen] Cached the user language on the request.

This commit is contained in:
Gaetan Delannay 2014-09-10 18:21:14 +02:00
parent 194b455816
commit 326523cc3a

View file

@ -1519,22 +1519,30 @@ class BaseMixin:
'''Gets the language (code) of the current user.''' '''Gets the language (code) of the current user.'''
if not hasattr(self, 'REQUEST'): if not hasattr(self, 'REQUEST'):
return self.getProductConfig().appConfig.languages[0] return self.getProductConfig().appConfig.languages[0]
# Return the cached value on the request object if present
rq = self.REQUEST
if hasattr(rq, 'userLanguage'): return rq.userLanguage
# Try the value which comes from the cookie. Indeed, if such a cookie is # Try the value which comes from the cookie. Indeed, if such a cookie is
# present, it means that the user has explicitly chosen this language # present, it means that the user has explicitly chosen this language
# via the language selector. # via the language selector.
rq = self.REQUEST if '_ZopeLg' in rq.cookies:
if '_ZopeLg' in rq.cookies: return rq.cookies['_ZopeLg'] res = rq.cookies['_ZopeLg']
# Try the LANGUAGE key from the request: it corresponds to the language else:
# as configured in the user's browser. # Try the LANGUAGE key from the request: it corresponds to the
res = self.REQUEST.get('LANGUAGE', None) # language as configured in the user's browser.
if res: return res res = rq.get('LANGUAGE', None)
# Try the HTTP_ACCEPT_LANGUAGE key from the request, which stores if not res:
# language preferences as defined in the user's browser. Several # Try the HTTP_ACCEPT_LANGUAGE key from the request, which
# languages can be listed, from most to less wanted. # stores language preferences as defined in the user's browser.
res = self.REQUEST.get('HTTP_ACCEPT_LANGUAGE', None) # Several languages can be listed, from most to less wanted.
if not res: return self.getProductConfig().appConfig.languages[0] res = rq.get('HTTP_ACCEPT_LANGUAGE', None)
if ',' in res: res = res[:res.find(',')] if res:
if '-' in res: res = res[:res.find('-')] if ',' in res: res = res[:res.find(',')]
if '-' in res: res = res[:res.find('-')]
else:
res = self.getProductConfig().appConfig.languages[0]
# Cache this result
rq.userLanguage = res
return res return res
def getLanguageDirection(self, lang): def getLanguageDirection(self, lang):