Ticket #6409: 6409-6446-with-regression-tests.diff

File 6409-6446-with-regression-tests.diff, 3.6 KB (added by Ramiro Morales, 17 years ago)

New version of simonb's patch plus regression tests and fixing also #6446

  • django/conf/global_settings.py

    diff -r f0b754562bac django/conf/global_settings.py
    a b LANGUAGES = (  
    4747    ('el', gettext_noop('Greek')),
    4848    ('en', gettext_noop('English')),
    4949    ('es', gettext_noop('Spanish')),
    50     ('es_AR', gettext_noop('Argentinean Spanish')),
     50    ('es-ar', gettext_noop('Argentinean Spanish')),
    5151    ('fa', gettext_noop('Persian')),
    5252    ('fi', gettext_noop('Finnish')),
    5353    ('fr', gettext_noop('French')),
  • django/utils/translation/trans_real.py

    diff -r f0b754562bac django/utils/translation/trans_real.py
    a b def get_language_from_request(request):  
    357357        return lang_code
    358358
    359359    accept = request.META.get('HTTP_ACCEPT_LANGUAGE', '')
    360     for lang, unused in parse_accept_lang_header(accept):
    361         if lang == '*':
     360    for accept_lang, unused in parse_accept_lang_header(accept):
     361        if accept_lang == '*':
    362362            break
    363363
    364364        # We have a very restricted form for our language files (no encoding
    def get_language_from_request(request):  
    366366        # language each time. So we avoid the overhead of gettext.find() and
    367367        # look up the MO file manually.
    368368
    369         normalized = locale.locale_alias.get(to_locale(lang, True))
     369        normalized = locale.locale_alias.get(to_locale(accept_lang, True))
    370370        if not normalized:
    371371            continue
    372372
    def get_language_from_request(request):  
    378378            # need to check again.
    379379            return _accepted[normalized]
    380380
    381         for lang in (normalized, normalized.split('_')[0]):
     381        for lang in (accept_lang, accept_lang.split('-')[0]):
    382382            if lang not in supported:
    383383                continue
    384             langfile = os.path.join(globalpath, lang, 'LC_MESSAGES',
     384            normalized = locale.locale_alias.get(to_locale(lang, True)).split('.')[0]
     385            langfile = os.path.join(globalpath, normalized, 'LC_MESSAGES',
    385386                    'django.mo')
    386387            if os.path.exists(langfile):
    387388                _accepted[normalized] = lang
  • tests/regressiontests/i18n/misc.py

    diff -r f0b754562bac tests/regressiontests/i18n/misc.py
    a b Bad headers; should always return [].  
    5454>>> p('')
    5555[]
    5656
     57>>> from django.utils.translation.trans_real import get_language_from_request
     58>>> g = get_language_from_request
     59>>> from django.http import HttpRequest
     60>>> r = HttpRequest
     61>>> r.COOKIES = {}
     62
     63These tests assumes the es, es_AR, pt and pt_BR translations aren't going to
     64be deleted from the Django source tree.
     65>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'pt-br'}
     66>>> g(r)
     67'pt-br'
     68>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'pt'}
     69>>> g(r)
     70'pt'
     71>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'es,de'}
     72>>> g(r)
     73'es'
     74>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'es-ar,de'}
     75>>> g(r)
     76'es-ar'
     77
     78This test assumes there won't be a Django translation to a US variation
     79of the Spanish language, a safe assumption. When the user sets it
     80as the preferred language, the main 'es' translation should be selected
     81instead.
     82>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'es-us'}
     83>>> g(r)
     84'es'
     85
     86This tests the following scenario: there isn't a main language (zh)
     87translation of Django but there is a translation to variation (zh_CN)
     88the user sets zh-cn as the preferred language, it should be selected by
     89Django without falling back nor ignoring it.
     90>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'zh-cn,de'}
     91>>> g(r)
     92'zh-cn'
     93
    5794"""
Back to Top