| | 456 | |
| | 457 | def test_parse_language_cookie(self): |
| | 458 | """ |
| | 459 | Now test that we parse language preferences stored in a cookie correctly. |
| | 460 | """ |
| | 461 | from django.utils.translation.trans_real import get_language_from_request |
| | 462 | g = get_language_from_request |
| | 463 | from django.http import HttpRequest |
| | 464 | r = HttpRequest |
| | 465 | r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'pt-br'} |
| | 466 | r.META = {} |
| | 467 | self.assertEqual('pt-br', g(r)) |
| | 468 | |
| | 469 | r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'pt'} |
| | 470 | r.META = {} |
| | 471 | self.assertEqual('pt', g(r)) |
| | 472 | |
| | 473 | r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'es'} |
| | 474 | r.META = {'HTTP_ACCEPT_LANGUAGE': 'de'} |
| | 475 | self.assertEqual('es', g(r)) |
| | 476 | |
| | 477 | # Python 2.3 and 2.4 return slightly different results for completely |
| | 478 | # bogus locales, so we omit this test for that anything below 2.4. |
| | 479 | # It's relatively harmless in any cases (GIGO). This also means this |
| | 480 | # won't be executed on Jython currently, but life's like that |
| | 481 | # sometimes. (On those platforms, passing in a truly bogus locale |
| | 482 | # will get you the default locale back.) |
| | 483 | if sys.version_info >= (2, 5): |
| | 484 | # This test assumes there won't be a Django translation to a US |
| | 485 | # variation of the Spanish language, a safe assumption. When the |
| | 486 | # user sets it as the preferred language, the main 'es' |
| | 487 | # translation should be selected instead. |
| | 488 | r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'es-us'} |
| | 489 | r.META = {} |
| | 490 | self.assertEqual(g(r), 'es') |
| | 491 | |
| | 492 | # This tests the following scenario: there isn't a main language (zh) |
| | 493 | # translation of Django but there is a translation to variation (zh_CN) |
| | 494 | # the user sets zh-cn as the preferred language, it should be selected |
| | 495 | # by Django without falling back nor ignoring it. |
| | 496 | r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'zh-cn'} |
| | 497 | r.META = {'HTTP_ACCEPT_LANGUAGE': 'de'} |
| | 498 | self.assertEqual(g(r), 'zh-cn') |