Django

Code

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

File 6409-6446-with-regression-tests.diff, 3.6 kB (added by ramiro, 7 months ago)

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

  • a/django/conf/global_settings.py

    old new  
    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')), 
  • a/django/utils/translation/trans_real.py

    old new  
    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 
     
    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 
     
    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 
  • a/tests/regressiontests/i18n/misc.py

    old new  
    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"""