Skip to content

Commit

Permalink
Merge branch 'master' into feature-1797-webstore-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
rufuspollock committed Feb 29, 2012
2 parents b6a7308 + f25934b commit a205cd2
Show file tree
Hide file tree
Showing 19 changed files with 2,111 additions and 1,412 deletions.
24 changes: 21 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')]
if lang and lang[0] in self.local_list:
return lang[0]
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,16 @@ 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
default = (cookie_lang == self.default_locale)
environ['CKAN_LANG_IS_DEFAULT'] = default
else:
environ['CKAN_LANG'] = self.default_locale
environ['CKAN_LANG_IS_DEFAULT'] = True


# Current application url
path_info = environ['PATH_INFO']
Expand Down
1 change: 0 additions & 1 deletion ckan/config/routing.py
Expand Up @@ -164,7 +164,6 @@ def make_map():
]))
)

m.connect('/dataset', action='index')
m.connect('/dataset/{action}/{id}/{revision}', action='read_ajax',
requirements=dict(action='|'.join([
'read',
Expand Down
3 changes: 2 additions & 1 deletion ckan/lib/helpers.py
Expand Up @@ -96,7 +96,8 @@ def _add_i18n_to_url(url_to_amend, **kw):
except TypeError:
root = ''
if default_locale:
url = '%s%s' % (root, url_to_amend)
url = url_to_amend[len(root):]
url = '%s%s' % (root, url)
else:
# we need to strip the root from the url and the add it before
# the language specification.
Expand Down
14 changes: 12 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,20 @@ 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.
try:
if request.cookies.get('ckan_lang') != lang:
response.set_cookie('ckan_lang', lang, max_age=3600)
except AttributeError:
# when testing FakeRequest does not have cookies
pass
return lang

def get_lang():
Expand Down

0 comments on commit a205cd2

Please sign in to comment.