Skip to content

Commit

Permalink
[xs] fix the redirect language issues on logging in/out
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Feb 28, 2012
1 parent 282c100 commit 2482d71
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
23 changes: 20 additions & 3 deletions ckan/config/middleware.py
Expand Up @@ -133,6 +133,17 @@ def __init__(self, app, config):
self.default_locale = config.get('ckan.locale_default', 'en')
self.local_list = get_locales()

def get_cookie_lang(self, environ):
# get the lang from cookie if present
cookie = environ.get('HTTP_COOKIE')
if cookie:
cookies = [c.strip() for c in cookie.split(';')]
lang = [c.split('=')[1] for c in cookies \
if c.startswith('ckan_lang')][0]
if lang in self.local_list:
return lang
return None

def __call__(self, environ, start_response):
# strip the language selector from the requested url
# and set environ variables for the language selected
Expand All @@ -153,9 +164,15 @@ def __call__(self, environ, start_response):
else:
environ['PATH_INFO'] = '/'
else:
# use default language from config
environ['CKAN_LANG'] = self.default_locale
environ['CKAN_LANG_IS_DEFAULT'] = True
# use cookie lang or default language from config
cookie_lang = self.get_cookie_lang(environ)
if cookie_lang:
environ['CKAN_LANG'] = cookie_lang
environ['CKAN_LANG_IS_DEFAULT'] = False
else:
environ['CKAN_LANG'] = self.default_locale
environ['CKAN_LANG_IS_DEFAULT'] = True


# Current application url
path_info = environ['PATH_INFO']
Expand Down
10 changes: 8 additions & 2 deletions ckan/lib/i18n.py
Expand Up @@ -3,6 +3,7 @@
from babel import Locale, localedata
from babel.core import LOCALE_ALIASES
from pylons import config
from pylons import response
from pylons import i18n

import ckan.i18n
Expand Down Expand Up @@ -89,11 +90,16 @@ def get_available_locales():

def handle_request(request, tmpl_context):
''' Set the language for the request '''
lang = request.environ.get('CKAN_LANG',
config.get('ckan.locale_default', 'en'))
lang = request.environ.get('CKAN_LANG') or \
config.get('ckan.locale_default', 'en')
if lang != 'en':
i18n.set_lang(lang)
tmpl_context.language = lang

# set ckan_lang cookie if we have changed the language. We need to
# remember this because repoze.who does it's own redirect.
if request.cookies.get('ckan_lang') != lang:
response.set_cookie('ckan_lang', lang, max_age=3600)
return lang

def get_lang():
Expand Down

0 comments on commit 2482d71

Please sign in to comment.