mirror of
https://github.com/django/django.git
synced 2024-12-23 01:25:58 +00:00
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
This commit is contained in:
parent
e50eefceb6
commit
71da5f62da
@ -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 @@ class MiscTests(TestCase):
|
||||
# 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')
|
||||
|
Loading…
Reference in New Issue
Block a user