Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.1.X] Fixed #7720 - Fallback to the base language if the sub langua…

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

Backport of r12442.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12452 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit df1ad561b124450388a696e668fa6549f2bbe446 1 parent 6a3c915
Jannis Leidel jezdez authored
4 django/utils/translation/trans_real.py
View
@@ -354,6 +354,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
25 tests/regressiontests/i18n/misc.py
View
@@ -85,6 +85,21 @@
>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'es-ar,de'}
>>> g(r)
'es-ar'
+
+# Now test that we parse language preferences stored in a cookie correctly.
+>>> from django.conf import settings
+>>> r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'pt-br'}
+>>> r.META = {}
+>>> g(r)
+'pt-br'
+>>> r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'pt'}
+>>> r.META = {}
+>>> g(r)
+'pt'
+>>> r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'es'}
+>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'de'}
+>>> g(r)
+'es'
"""
# Python 2.3 and 2.4 return slightly different results for completely bogus
@@ -98,9 +113,14 @@
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 = {}
>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'es-us'}
>>> g(r)
'es'
+>>> r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'es-us'}
+>>> r.META = {}
+>>> g(r)
+'es'
"""
tests += """
@@ -108,7 +128,12 @@
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 = {}
>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'zh-cn,de'}
>>> g(r)
'zh-cn'
+>>> r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'zh-cn'}
+>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'de'}
+>>> g(r)
+'zh-cn'
"""
Please sign in to comment.
Something went wrong with that request. Please try again.