| 1 |
from django.template import Context, Template |
| 2 |
from django.test import SimpleTestCase |
| 3 |
from django.utils import translation |
| 4 |
|
| 5 |
from ...utils import setup |
| 6 |
from .base import MultipleLocaleActivationTestCase |
| 7 |
|
| 8 |
|
| 9 |
class MultipleLocaleActivationTests(MultipleLocaleActivationTestCase): |
| 10 |
def test_single_locale_activation(self): |
| 11 |
""" |
| 12 |
Simple baseline behavior with one locale for all the supported i18n |
| 13 |
constructs. |
| 14 |
""" |
| 15 |
with translation.override("fr"): |
| 16 |
self.assertEqual(Template("{{ _('Yes') }}").render(Context({})), "Oui") |
| 17 |
|
| 18 |
# Literal marked up with _() in a filter expression |
| 19 |
|
| 20 |
def test_multiple_locale_filter(self): |
| 21 |
with translation.override("de"): |
| 22 |
t = Template("{% load i18n %}{{ 0|yesno:_('yes,no,maybe') }}") |
| 23 |
with translation.override(self._old_language), translation.override("nl"): |
| 24 |
self.assertEqual(t.render(Context({})), "nee") |
| 25 |
|
| 26 |
def test_multiple_locale_filter_deactivate(self): |
| 27 |
with translation.override("de", deactivate=True): |
| 28 |
t = Template("{% load i18n %}{{ 0|yesno:_('yes,no,maybe') }}") |
| 29 |
with translation.override("nl"): |
| 30 |
self.assertEqual(t.render(Context({})), "nee") |
| 31 |
|
| 32 |
def test_multiple_locale_filter_direct_switch(self): |
| 33 |
with translation.override("de"): |
| 34 |
t = Template("{% load i18n %}{{ 0|yesno:_('yes,no,maybe') }}") |
| 35 |
with translation.override("nl"): |
| 36 |
self.assertEqual(t.render(Context({})), "nee") |
| 37 |
|
| 38 |
# Literal marked up with _() |
| 39 |
|
| 40 |
def test_multiple_locale(self): |
| 41 |
with translation.override("de"): |
| 42 |
t = Template("{{ _('No') }}") |
| 43 |
with translation.override(self._old_language), translation.override("nl"): |
| 44 |
self.assertEqual(t.render(Context({})), "Nee") |
| 45 |
|
| 46 |
def test_multiple_locale_deactivate(self): |
| 47 |
with translation.override("de", deactivate=True): |
| 48 |
t = Template("{{ _('No') }}") |
| 49 |
with translation.override("nl"): |
| 50 |
self.assertEqual(t.render(Context({})), "Nee") |
| 51 |
|
| 52 |
def test_multiple_locale_direct_switch(self): |
| 53 |
with translation.override("de"): |
| 54 |
t = Template("{{ _('No') }}") |
| 55 |
with translation.override("nl"): |
| 56 |
self.assertEqual(t.render(Context({})), "Nee") |
| 57 |
|
| 58 |
# Literal marked up with _(), loading the i18n template tag library |
| 59 |
|
| 60 |
def test_multiple_locale_loadi18n(self): |
| 61 |
with translation.override("de"): |
| 62 |
t = Template("{% load i18n %}{{ _('No') }}") |
| 63 |
with translation.override(self._old_language), translation.override("nl"): |
| 64 |
self.assertEqual(t.render(Context({})), "Nee") |
| 65 |
|
| 66 |
def test_multiple_locale_loadi18n_deactivate(self): |
| 67 |
with translation.override("de", deactivate=True): |
| 68 |
t = Template("{% load i18n %}{{ _('No') }}") |
| 69 |
with translation.override("nl"): |
| 70 |
self.assertEqual(t.render(Context({})), "Nee") |
| 71 |
|
| 72 |
def test_multiple_locale_loadi18n_direct_switch(self): |
| 73 |
with translation.override("de"): |
| 74 |
t = Template("{% load i18n %}{{ _('No') }}") |
| 75 |
with translation.override("nl"): |
| 76 |
self.assertEqual(t.render(Context({})), "Nee") |
| 77 |
|
| 78 |
|
| 79 |
class I18nStringLiteralTests(SimpleTestCase): |
| 80 |
"""translation of constant strings""" |
| 81 |
|
| 82 |
libraries = {"i18n": "django.templatetags.i18n"} |
| 83 |
|
| 84 |
@setup({"i18n13": '{{ _("Password") }}'}) |
| 85 |
def test_i18n13(self): |
| 86 |
with translation.override("de"): |
| 87 |
output = self.engine.render_to_string("i18n13") |
| 88 |
self.assertEqual(output, "Passwort") |
| 89 |
|
| 90 |
@setup( |
| 91 |
{ |
| 92 |
"i18n14": ( |
| 93 |
'{% cycle "foo" _("Password") _(\'Password\') as c %} {% cycle c %} ' |
| 94 |
"{% cycle c %}" |
| 95 |
) |
| 96 |
} |
| 97 |
) |
| 98 |
def test_i18n14(self): |
| 99 |
with translation.override("de"): |
| 100 |
output = self.engine.render_to_string("i18n14") |
| 101 |
self.assertEqual(output, "foo Passwort Passwort") |
| 102 |
|
| 103 |
@setup({"i18n15": '{{ absent|default:_("Password") }}'}) |
| 104 |
def test_i18n15(self): |
| 105 |
with translation.override("de"): |
| 106 |
output = self.engine.render_to_string("i18n15", {"absent": ""}) |
| 107 |
self.assertEqual(output, "Passwort") |
| 108 |
|
| 109 |
@setup({"i18n16": '{{ _("<") }}'}) |
| 110 |
def test_i18n16(self): |
| 111 |
with translation.override("de"): |
| 112 |
output = self.engine.render_to_string("i18n16") |
| 113 |
self.assertEqual(output, "<") |