Author: jezdez
Date: 2010-02-16 06:52:04 -0600 (Tue, 16 Feb 2010)
New Revision: 12452

Modified:
   django/branches/releases/1.1.X/django/utils/translation/trans_real.py
   django/branches/releases/1.1.X/tests/regressiontests/i18n/misc.py
Log:
[1.1.X] Fixed #7720 - Fallback to the base language if the sub language given 
in the language cookie doesn't exist. Thanks, djoume and Ramiro Morales.

Backport of r12442.

Modified: django/branches/releases/1.1.X/django/utils/translation/trans_real.py
===================================================================
--- django/branches/releases/1.1.X/django/utils/translation/trans_real.py       
2010-02-16 12:51:43 UTC (rev 12451)
+++ django/branches/releases/1.1.X/django/utils/translation/trans_real.py       
2010-02-16 12:52:04 UTC (rev 12452)
@@ -354,6 +354,10 @@
             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
 

Modified: django/branches/releases/1.1.X/tests/regressiontests/i18n/misc.py
===================================================================
--- django/branches/releases/1.1.X/tests/regressiontests/i18n/misc.py   
2010-02-16 12:51:43 UTC (rev 12451)
+++ django/branches/releases/1.1.X/tests/regressiontests/i18n/misc.py   
2010-02-16 12:52:04 UTC (rev 12452)
@@ -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'
 """

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-upda...@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to