Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[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 authored February 16, 2010
4  django/utils/translation/trans_real.py
@@ -354,6 +354,10 @@ def get_language_from_request(request):
354 354
             return lang_code
355 355
 
356 356
     lang_code = request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME)
  357
+
  358
+    if lang_code and lang_code not in supported:
  359
+        lang_code = lang_code.split('-')[0] # e.g. if fr-ca is not supported fallback to fr
  360
+
357 361
     if lang_code and lang_code in supported and check_for_language(lang_code):
358 362
         return lang_code
359 363
 
25  tests/regressiontests/i18n/misc.py
@@ -85,6 +85,21 @@
85 85
 >>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'es-ar,de'}
86 86
 >>> g(r)
87 87
 'es-ar'
  88
+
  89
+# Now test that we parse language preferences stored in a cookie correctly.
  90
+>>> from django.conf import settings
  91
+>>> r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'pt-br'}
  92
+>>> r.META = {}
  93
+>>> g(r)
  94
+'pt-br'
  95
+>>> r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'pt'}
  96
+>>> r.META = {}
  97
+>>> g(r)
  98
+'pt'
  99
+>>> r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'es'}
  100
+>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'de'}
  101
+>>> g(r)
  102
+'es'
88 103
 """
89 104
 
90 105
 # Python 2.3 and 2.4 return slightly different results for completely bogus
@@ -98,9 +113,14 @@
98 113
 of the Spanish language, a safe assumption. When the user sets it
99 114
 as the preferred language, the main 'es' translation should be selected
100 115
 instead.
  116
+>>> r.COOKIES = {}
101 117
 >>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'es-us'}
102 118
 >>> g(r)
103 119
 'es'
  120
+>>> r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'es-us'}
  121
+>>> r.META = {}
  122
+>>> g(r)
  123
+'es'
104 124
 """
105 125
 
106 126
 tests += """
@@ -108,7 +128,12 @@
108 128
 translation of Django but there is a translation to variation (zh_CN)
109 129
 the user sets zh-cn as the preferred language, it should be selected by
110 130
 Django without falling back nor ignoring it.
  131
+>>> r.COOKIES = {}
111 132
 >>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'zh-cn,de'}
112 133
 >>> g(r)
113 134
 'zh-cn'
  135
+>>> r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'zh-cn'}
  136
+>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'de'}
  137
+>>> g(r)
  138
+'zh-cn'
114 139
 """

0 notes on commit df1ad56

Please sign in to comment.
Something went wrong with that request. Please try again.