Changeset 7091
- Timestamp:
- 02/05/08 19:04:30 (5 months ago)
- Files:
-
- django/trunk/django/conf/global_settings.py (modified) (1 diff)
- django/trunk/django/utils/translation/trans_real.py (modified) (4 diffs)
- django/trunk/tests/regressiontests/i18n/misc.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/conf/global_settings.py
r6822 r7091 48 48 ('en', gettext_noop('English')), 49 49 ('es', gettext_noop('Spanish')), 50 ('es _AR', gettext_noop('Argentinean Spanish')),50 ('es-ar', gettext_noop('Argentinean Spanish')), 51 51 ('fa', gettext_noop('Persian')), 52 52 ('fi', gettext_noop('Finnish')), django/trunk/django/utils/translation/trans_real.py
r6701 r7091 43 43 44 44 def 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 """ 46 49 p = language.find('-') 47 50 if p >= 0: … … 358 361 359 362 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 == '*': 362 365 break 363 366 … … 365 368 # specifier, since they all must be UTF-8 and only one possible 366 369 # 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)) 370 375 if not normalized: 371 376 continue 372 373 377 # Remove the default encoding from locale_alias 374 378 normalized = normalized.split('.')[0] … … 379 383 return _accepted[normalized] 380 384 381 for lang in (normalized, normalized.split('_')[0]): 385 for lang, dirname in ((accept_lang, normalized), 386 (accept_lang.split('-')[0], normalized.split('_')[0])): 382 387 if lang not in supported: 383 388 continue 384 langfile = os.path.join(globalpath, lang, 'LC_MESSAGES',389 langfile = os.path.join(globalpath, dirname, 'LC_MESSAGES', 385 390 'django.mo') 386 391 if os.path.exists(langfile): django/trunk/tests/regressiontests/i18n/misc.py
r6612 r7091 2 2 >>> from django.utils.translation.trans_real import parse_accept_lang_header 3 3 >>> 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 # 4 9 5 10 Good headers. … … 55 60 [] 56 61 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 72 These tests assumes the es, es_AR, pt and pt_BR translations exit in the Django 73 source 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 87 This test assumes there won't be a Django translation to a US variation 88 of the Spanish language, a safe assumption. When the user sets it 89 as the preferred language, the main 'es' translation should be selected 90 instead. 91 >>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'es-us'} 92 >>> g(r) 93 'es' 94 95 This tests the following scenario: there isn't a main language (zh) 96 translation of Django but there is a translation to variation (zh_CN) 97 the user sets zh-cn as the preferred language, it should be selected by 98 Django without falling back nor ignoring it. 99 >>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'zh-cn,de'} 100 >>> g(r) 101 'zh-cn' 57 102 """
