4
>>> from django.utils.translation.trans_real import parse_accept_lang_header
5
>>> p = parse_accept_lang_header
8
# Testing HTTP header parsing. First, we test that we can parse the values
9
# according to the spec (and that we extract all the pieces in the right order).
19
>>> p('en-AU;q=0.123')
22
[('en-au', 0.10000000000000001)]
25
>>> p('da, en-gb;q=0.25, en;q=0.5')
26
[('da', 1.0), ('en', 0.5), ('en-gb', 0.25)]
29
>>> p('de,en-au;q=0.75,en-us;q=0.5,en;q=0.25,es;q=0.125,fa;q=0.125')
30
[('de', 1.0), ('en-au', 0.75), ('en-us', 0.5), ('en', 0.25), ('es', 0.125), ('fa', 0.125)]
38
Bad headers; should always return [].
39
>>> p('en-gb;q=1.0000')
51
>>> p('en-au;q=0.1.0')
53
>>> p('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZ,en')
55
>>> p('da, en-gb;q=0.8, en;q=0.7,#')
65
# Now test that we parse a literal HTTP header correctly.
68
>>> from django.utils.translation.trans_real import get_language_from_request
69
>>> g = get_language_from_request
70
>>> from django.http import HttpRequest
74
These tests assumes the es, es_AR, pt and pt_BR translations exit in the Django
76
>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'pt-br'}
79
>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'pt'}
82
>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'es,de'}
85
>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'es-ar,de'}
90
# Python 2.3 and 2.4 return slightly different results for completely bogus
91
# locales, so we omit this test for that anything below 2.4. It's relatively
92
# harmless in any cases (GIGO). This also means this won't be executed on
93
# Jython currently, but life's like that sometimes. (On those platforms,
94
# passing in a truly bogus locale will get you the default locale back.)
95
if sys.version_info >= (2, 5):
97
This test assumes there won't be a Django translation to a US variation
98
of the Spanish language, a safe assumption. When the user sets it
99
as the preferred language, the main 'es' translation should be selected
101
>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'es-us'}
107
This tests the following scenario: there isn't a main language (zh)
108
translation of Django but there is a translation to variation (zh_CN)
109
the user sets zh-cn as the preferred language, it should be selected by
110
Django without falling back nor ignoring it.
111
>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'zh-cn,de'}