Ticket #7720: 7720-fallback-to-root-lang-when-lang-pref-stored-in-cookie.diff

File 7720-fallback-to-root-lang-when-lang-pref-stored-in-cookie.diff, 2.9 KB (added by Ramiro Morales, 15 years ago)

Patch with original fix by djoume plus tests

  • django/utils/translation/trans_real.py

    diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py
    a b  
    356356            return lang_code
    357357
    358358    lang_code = request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME)
     359
     360    if lang_code and lang_code not in supported:
     361        lang_code = lang_code[:2] # if fr-ca is not supported fallback to fr
     362
    359363    if lang_code and lang_code in supported and check_for_language(lang_code):
    360364        return lang_code
    361365
  • tests/regressiontests/i18n/tests.py

    diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py
    a b  
    453453        # by Django without falling back nor ignoring it.
    454454        r.META = {'HTTP_ACCEPT_LANGUAGE': 'zh-cn,de'}
    455455        self.assertEqual(g(r), 'zh-cn')
     456
     457    def test_parse_language_cookie(self):
     458        """
     459        Now test that we parse language preferences stored in a cookie correctly.
     460        """
     461        from django.utils.translation.trans_real import get_language_from_request
     462        g = get_language_from_request
     463        from django.http import HttpRequest
     464        r = HttpRequest
     465        r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'pt-br'}
     466        r.META = {}
     467        self.assertEqual('pt-br', g(r))
     468
     469        r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'pt'}
     470        r.META = {}
     471        self.assertEqual('pt', g(r))
     472
     473        r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'es'}
     474        r.META = {'HTTP_ACCEPT_LANGUAGE': 'de'}
     475        self.assertEqual('es', g(r))
     476
     477        # Python 2.3 and 2.4 return slightly different results for completely
     478        # bogus locales, so we omit this test for that anything below 2.4.
     479        # It's relatively harmless in any cases (GIGO). This also means this
     480        # won't be executed on Jython currently, but life's like that
     481        # sometimes. (On those platforms, passing in a truly bogus locale
     482        # will get you the default locale back.)
     483        if sys.version_info >= (2, 5):
     484            # This test assumes there won't be a Django translation to a US
     485            # variation of the Spanish language, a safe assumption. When the
     486            # user sets it as the preferred language, the main 'es'
     487            # translation should be selected instead.
     488            r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'es-us'}
     489            r.META = {}
     490            self.assertEqual(g(r), 'es')
     491
     492        # This tests the following scenario: there isn't a main language (zh)
     493        # translation of Django but there is a translation to variation (zh_CN)
     494        # the user sets zh-cn as the preferred language, it should be selected
     495        # by Django without falling back nor ignoring it.
     496        r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'zh-cn'}
     497        r.META = {'HTTP_ACCEPT_LANGUAGE': 'de'}
     498        self.assertEqual(g(r), 'zh-cn')
Back to Top