| 1 |
import sys |
|---|
| 2 |
|
|---|
| 3 |
tests = """ |
|---|
| 4 |
>>> from django.utils.translation.trans_real import parse_accept_lang_header |
|---|
| 5 |
>>> p = parse_accept_lang_header |
|---|
| 6 |
|
|---|
| 7 |
# |
|---|
| 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). |
|---|
| 10 |
# |
|---|
| 11 |
|
|---|
| 12 |
Good headers. |
|---|
| 13 |
>>> p('de') |
|---|
| 14 |
[('de', 1.0)] |
|---|
| 15 |
>>> p('en-AU') |
|---|
| 16 |
[('en-AU', 1.0)] |
|---|
| 17 |
>>> p('*;q=1.00') |
|---|
| 18 |
[('*', 1.0)] |
|---|
| 19 |
>>> p('en-AU;q=0.123') |
|---|
| 20 |
[('en-AU', 0.123)] |
|---|
| 21 |
>>> p('en-au;q=0.1') |
|---|
| 22 |
[('en-au', 0.10000000000000001)] |
|---|
| 23 |
>>> p('en-au;q=1.0') |
|---|
| 24 |
[('en-au', 1.0)] |
|---|
| 25 |
>>> p('da, en-gb;q=0.25, en;q=0.5') |
|---|
| 26 |
[('da', 1.0), ('en', 0.5), ('en-gb', 0.25)] |
|---|
| 27 |
>>> p('en-au-xx') |
|---|
| 28 |
[('en-au-xx', 1.0)] |
|---|
| 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)] |
|---|
| 31 |
>>> p('*') |
|---|
| 32 |
[('*', 1.0)] |
|---|
| 33 |
>>> p('de;q=0.') |
|---|
| 34 |
[('de', 1.0)] |
|---|
| 35 |
>>> p('') |
|---|
| 36 |
[] |
|---|
| 37 |
|
|---|
| 38 |
Bad headers; should always return []. |
|---|
| 39 |
>>> p('en-gb;q=1.0000') |
|---|
| 40 |
[] |
|---|
| 41 |
>>> p('en;q=0.1234') |
|---|
| 42 |
[] |
|---|
| 43 |
>>> p('en;q=.2') |
|---|
| 44 |
[] |
|---|
| 45 |
>>> p('abcdefghi-au') |
|---|
| 46 |
[] |
|---|
| 47 |
>>> p('**') |
|---|
| 48 |
[] |
|---|
| 49 |
>>> p('en,,gb') |
|---|
| 50 |
[] |
|---|
| 51 |
>>> p('en-au;q=0.1.0') |
|---|
| 52 |
[] |
|---|
| 53 |
>>> p('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZ,en') |
|---|
| 54 |
[] |
|---|
| 55 |
>>> p('da, en-gb;q=0.8, en;q=0.7,#') |
|---|
| 56 |
[] |
|---|
| 57 |
>>> p('de;q=2.0') |
|---|
| 58 |
[] |
|---|
| 59 |
>>> p('de;q=0.a') |
|---|
| 60 |
[] |
|---|
| 61 |
>>> p('') |
|---|
| 62 |
[] |
|---|
| 63 |
|
|---|
| 64 |
# |
|---|
| 65 |
# Now test that we parse a literal HTTP header correctly. |
|---|
| 66 |
# |
|---|
| 67 |
|
|---|
| 68 |
>>> from django.utils.translation.trans_real import get_language_from_request |
|---|
| 69 |
>>> g = get_language_from_request |
|---|
| 70 |
>>> from django.http import HttpRequest |
|---|
| 71 |
>>> r = HttpRequest |
|---|
| 72 |
>>> r.COOKIES = {} |
|---|
| 73 |
|
|---|
| 74 |
These tests assumes the es, es_AR, pt and pt_BR translations exit in the Django |
|---|
| 75 |
source tree. |
|---|
| 76 |
>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'pt-br'} |
|---|
| 77 |
>>> g(r) |
|---|
| 78 |
'pt-br' |
|---|
| 79 |
>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'pt'} |
|---|
| 80 |
>>> g(r) |
|---|
| 81 |
'pt' |
|---|
| 82 |
>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'es,de'} |
|---|
| 83 |
>>> g(r) |
|---|
| 84 |
'es' |
|---|
| 85 |
>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'es-ar,de'} |
|---|
| 86 |
>>> g(r) |
|---|
| 87 |
'es-ar' |
|---|
| 88 |
""" |
|---|
| 89 |
|
|---|
| 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): |
|---|
| 96 |
tests += """ |
|---|
| 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 |
|---|
| 100 |
instead. |
|---|
| 101 |
>>> r.META = {'HTTP_ACCEPT_LANGUAGE': 'es-us'} |
|---|
| 102 |
>>> g(r) |
|---|
| 103 |
'es' |
|---|
| 104 |
""" |
|---|
| 105 |
|
|---|
| 106 |
tests += """ |
|---|
| 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'} |
|---|
| 112 |
>>> g(r) |
|---|
| 113 |
'zh-cn' |
|---|
| 114 |
""" |
|---|