Skip to content

Commit

Permalink
Merge branch 'master' into enhancement-1792-api-logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Feb 28, 2012
2 parents ae0f7eb + 2482d71 commit 679b13a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
25 changes: 21 additions & 4 deletions ckan/config/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ def make_app(global_conf, full_stack=True, static_files=True, **app_conf):
# Routing/Session/Cache Middleware
app = RoutesMiddleware(app, config['routes.map'])
app = SessionMiddleware(app, config)
app = I18nMiddleware(app, config)
app = CacheMiddleware(app, config)

# CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
Expand Down Expand Up @@ -101,6 +100,7 @@ def make_app(global_conf, full_stack=True, static_files=True, **app_conf):
who_parser.remote_user_key,
)

app = I18nMiddleware(app, config)
# Establish the Registry for this application
app = RegistryManager(app)

Expand Down 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
Original file line number Diff line number Diff line change
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 679b13a

Please sign in to comment.