Django

Code

Changeset 7091

Show
Ignore:
Timestamp:
02/05/08 19:04:30 (5 months ago)
Author:
mtredinnick
Message:

Fixed #6409 -- Unbreak compound locale name parsing (e.g. zh-cn).

This was inadvertently broken back in [6608]. Slightly backwards-incompatible:
people specifying "es_AR" in their LANGUAGES list will need to change that to
"es-ar". Thanks, simonb and Ramiro Morales for making the effort to fix this.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/conf/global_settings.py

    r6822 r7091  
    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')), 
  • django/trunk/django/utils/translation/trans_real.py

    r6701 r7091  
    4343 
    4444def to_locale(language, to_lower=False): 
    45     "Turns a language name (en-us) into a locale name (en_US)." 
     45    """ 
     46    Turns a language name (en-us) into a locale name (en_US). If 'to_lower' is 
     47    True, the last component is lower-cased (en_us). 
     48    """ 
    4649    p = language.find('-') 
    4750    if p >= 0: 
     
    358361 
    359362    accept = request.META.get('HTTP_ACCEPT_LANGUAGE', '') 
    360     for lang, unused in parse_accept_lang_header(accept): 
    361         if lang == '*': 
     363    for accept_lang, unused in parse_accept_lang_header(accept): 
     364        if accept_lang == '*': 
    362365            break 
    363366 
     
    365368        # specifier, since they all must be UTF-8 and only one possible 
    366369        # language each time. So we avoid the overhead of gettext.find() and 
    367         # look up the MO file manually. 
    368  
    369         normalized = locale.locale_alias.get(to_locale(lang, True)) 
     370        # work out the MO file manually. 
     371 
     372        # 'normalized' is the root name of the locale in POSIX format (which is 
     373        # the format used for the directories holding the MO files). 
     374        normalized = locale.locale_alias.get(to_locale(accept_lang, True)) 
    370375        if not normalized: 
    371376            continue 
    372  
    373377        # Remove the default encoding from locale_alias 
    374378        normalized = normalized.split('.')[0] 
     
    379383            return _accepted[normalized] 
    380384 
    381         for lang in (normalized, normalized.split('_')[0]): 
     385        for lang, dirname in ((accept_lang, normalized), 
     386                (accept_lang.split('-')[0], normalized.split('_')[0])): 
    382387            if lang not in supported: 
    383388                continue 
    384             langfile = os.path.join(globalpath, lang, 'LC_MESSAGES', 
     389            langfile = os.path.join(globalpath, dirname, 'LC_MESSAGES', 
    385390                    'django.mo') 
    386391            if os.path.exists(langfile): 
  • django/trunk/tests/regressiontests/i18n/misc.py

    r6612 r7091  
    22>>> from django.utils.translation.trans_real import parse_accept_lang_header 
    33>>> p = parse_accept_lang_header 
     4 
     5# 
     6# Testing HTTP header parsing. First, we test that we can parse the values 
     7# according to the spec (and that we extract all the pieces in the right order). 
     8# 
    49 
    510Good headers. 
     
    5560[] 
    5661 
     62# 
     63# Now test that we parse a literal HTTP header correctly. 
     64# 
     65 
     66>>> from django.utils.translation.trans_real import get_language_from_request 
     67>>> g = get_language_from_request 
     68>>> from django.http import HttpRequest 
     69>>> r = HttpRequest 
     70>>> r.COOKIES = {} 
     71 
     72These tests assumes the es, es_AR, pt and pt_BR translations exit in the Django 
     73source tree. 
     74>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'pt-br'} 
     75>>> g(r) 
     76'pt-br' 
     77>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'pt'} 
     78>>> g(r) 
     79'pt' 
     80>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'es,de'} 
     81>>> g(r) 
     82'es' 
     83>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'es-ar,de'} 
     84>>> g(r) 
     85'es-ar' 
     86 
     87This test assumes there won't be a Django translation to a US variation 
     88of the Spanish language, a safe assumption. When the user sets it 
     89as the preferred language, the main 'es' translation should be selected 
     90instead. 
     91>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'es-us'} 
     92>>> g(r) 
     93'es' 
     94 
     95This tests the following scenario: there isn't a main language (zh) 
     96translation of Django but there is a translation to variation (zh_CN) 
     97the user sets zh-cn as the preferred language, it should be selected by 
     98Django without falling back nor ignoring it. 
     99>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'zh-cn,de'} 
     100>>> g(r) 
     101'zh-cn' 
    57102"""