Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #7720 - Fallback to the base language if the sub language given…

… in the language cookie doesn't exist. Thanks, djoume and Ramiro Morales.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12442 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 71da5f62dab825e015864fd5604be46815e24ff1 1 parent e50eefc
@jezdez jezdez authored
View
4 django/utils/translation/trans_real.py
@@ -356,6 +356,10 @@ def get_language_from_request(request):
return lang_code
lang_code = request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME)
+
+ if lang_code and lang_code not in supported:
+ lang_code = lang_code.split('-')[0] # e.g. if fr-ca is not supported fallback to fr
+
if lang_code and lang_code in supported and check_for_language(lang_code):
return lang_code
View
43 tests/regressiontests/i18n/tests.py
@@ -454,3 +454,46 @@ def test_parse_literal_http_header(self):
# by Django without falling back nor ignoring it.
r.META = {'HTTP_ACCEPT_LANGUAGE': 'zh-cn,de'}
self.assertEqual(g(r), 'zh-cn')
+
+ def test_parse_language_cookie(self):
+ """
+ Now test that we parse language preferences stored in a cookie correctly.
+ """
+ from django.utils.translation.trans_real import get_language_from_request
+ g = get_language_from_request
+ from django.http import HttpRequest
+ r = HttpRequest
+ r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'pt-br'}
+ r.META = {}
+ self.assertEqual('pt-br', g(r))
+
+ r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'pt'}
+ r.META = {}
+ self.assertEqual('pt', g(r))
+
+ r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'es'}
+ r.META = {'HTTP_ACCEPT_LANGUAGE': 'de'}
+ self.assertEqual('es', g(r))
+
+ # Python 2.3 and 2.4 return slightly different results for completely
+ # bogus locales, so we omit this test for that anything below 2.4.
+ # It's relatively harmless in any cases (GIGO). This also means this
+ # won't be executed on Jython currently, but life's like that
+ # sometimes. (On those platforms, passing in a truly bogus locale
+ # will get you the default locale back.)
+ if sys.version_info >= (2, 5):
+ # This test assumes there won't be a Django translation to a US
+ # variation of the Spanish language, a safe assumption. When the
+ # user sets it as the preferred language, the main 'es'
+ # translation should be selected instead.
+ r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'es-us'}
+ r.META = {}
+ self.assertEqual(g(r), 'es')
+
+ # This tests the following scenario: there isn't a main language (zh)
+ # translation of Django but there is a translation to variation (zh_CN)
+ # the user sets zh-cn as the preferred language, it should be selected
+ # by Django without falling back nor ignoring it.
+ r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'zh-cn'}
+ r.META = {'HTTP_ACCEPT_LANGUAGE': 'de'}
+ self.assertEqual(g(r), 'zh-cn')
Please sign in to comment.
Something went wrong with that request. Please try again.