From 2482d7150d0870cd31cde846c2c5060fe7e2ba94 Mon Sep 17 00:00:00 2001 From: Toby Date: Tue, 28 Feb 2012 15:02:51 +0000 Subject: [PATCH 01/13] [xs] fix the redirect language issues on logging in/out --- ckan/config/middleware.py | 23 ++++++++++++++++++++--- ckan/lib/i18n.py | 10 ++++++++-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/ckan/config/middleware.py b/ckan/config/middleware.py index f28a896f378..16613bf78e1 100644 --- a/ckan/config/middleware.py +++ b/ckan/config/middleware.py @@ -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 @@ -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'] diff --git a/ckan/lib/i18n.py b/ckan/lib/i18n.py index 743ef880aad..e71d741f076 100644 --- a/ckan/lib/i18n.py +++ b/ckan/lib/i18n.py @@ -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 @@ -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(): From 46a1cf3a2c2b6930f69b7976912e598bbfada7d7 Mon Sep 17 00:00:00 2001 From: Toby Date: Tue, 28 Feb 2012 16:06:24 +0000 Subject: [PATCH 02/13] lang fixes --- ckan/config/middleware.py | 3 ++- ckan/lib/helpers.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/ckan/config/middleware.py b/ckan/config/middleware.py index 16613bf78e1..62a4dcde86b 100644 --- a/ckan/config/middleware.py +++ b/ckan/config/middleware.py @@ -168,7 +168,8 @@ def __call__(self, environ, start_response): cookie_lang = self.get_cookie_lang(environ) if cookie_lang: environ['CKAN_LANG'] = cookie_lang - environ['CKAN_LANG_IS_DEFAULT'] = False + 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 diff --git a/ckan/lib/helpers.py b/ckan/lib/helpers.py index 0a19353590e..ad544419194 100644 --- a/ckan/lib/helpers.py +++ b/ckan/lib/helpers.py @@ -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. From c8a32caa3a7bbfd135bcc0afd65d994b305ac3f9 Mon Sep 17 00:00:00 2001 From: Toby Date: Tue, 28 Feb 2012 16:31:17 +0000 Subject: [PATCH 03/13] fix broken template --- ckan/templates/home/index.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ckan/templates/home/index.html b/ckan/templates/home/index.html index b13a0c1f9e1..fbd2eb54a8c 100644 --- a/ckan/templates/home/index.html +++ b/ckan/templates/home/index.html @@ -13,10 +13,10 @@

Welcome to ${g.site_title}!

Find data

-
+
- ${g.site_title} contains ${c.package_count} datasets that you can + ${g.site_title} contains ${c.package_count} datasets that you can browse, learn about and download.
From b0b782293fb70618046887c68a9f5210f1705a6a Mon Sep 17 00:00:00 2001 From: Toby Date: Tue, 28 Feb 2012 16:32:03 +0000 Subject: [PATCH 04/13] remove useless route --- ckan/config/routing.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ckan/config/routing.py b/ckan/config/routing.py index 64ab7d73eab..b99178a2e67 100644 --- a/ckan/config/routing.py +++ b/ckan/config/routing.py @@ -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', From 891921399980823e186b7130afd498aec69b9957 Mon Sep 17 00:00:00 2001 From: John Glover Date: Wed, 29 Feb 2012 10:15:07 +0000 Subject: [PATCH 05/13] [xs][middleware] Bug fix: check that lang list is not empty (old cookie setup will give a server 500 error) --- ckan/config/middleware.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ckan/config/middleware.py b/ckan/config/middleware.py index 62a4dcde86b..026cf02722f 100644 --- a/ckan/config/middleware.py +++ b/ckan/config/middleware.py @@ -139,9 +139,9 @@ def get_cookie_lang(self, environ): 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 + 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): From 8346d5d57aaa74222eddb500efdf3ac7dcf53070 Mon Sep 17 00:00:00 2001 From: Toby Date: Wed, 29 Feb 2012 11:31:34 +0000 Subject: [PATCH 06/13] [xs] fix static files using h.url_for_static() --- ckan/templates/package/resource_read.html | 10 +++++----- ckan/templates/user/login.html | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ckan/templates/package/resource_read.html b/ckan/templates/package/resource_read.html index 71379161f6b..31de6576471 100644 --- a/ckan/templates/package/resource_read.html +++ b/ckan/templates/package/resource_read.html @@ -13,8 +13,8 @@ - - + + - - + + + diff --git a/ckan/templates/user/login.html b/ckan/templates/user/login.html index a24b866b016..3d1d159c345 100644 --- a/ckan/templates/user/login.html +++ b/ckan/templates/user/login.html @@ -5,10 +5,10 @@ - + - + +