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: bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
jezdez committed Feb 16, 2010
1 parent e50eefc commit 71da5f62dab825e015864fd5604be46815e24ff1
Showing with 47 additions and 0 deletions.
  1. +4 −0 django/utils/translation/
  2. +43 −0 tests/regressiontests/i18n/
@@ -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
@@ -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.META = {}
self.assertEqual('pt', g(r))
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'}
self.assertEqual(g(r), 'zh-cn')

0 comments on commit 71da5f6

Please sign in to comment.