| 1 |
import inspect |
| 2 |
from functools import partial, wraps |
| 3 |
|
| 4 |
from asgiref.local import Local |
| 5 |
|
| 6 |
from django.template import Context, Template, TemplateSyntaxError |
| 7 |
from django.templatetags.l10n import LocalizeNode |
| 8 |
from django.test import SimpleTestCase, override_settings |
| 9 |
from django.utils import translation |
| 10 |
from django.utils.safestring import mark_safe |
| 11 |
from django.utils.translation import trans_real |
| 12 |
|
| 13 |
from ...utils import setup as base_setup |
| 14 |
from .base import MultipleLocaleActivationTestCase, extended_locale_paths |
| 15 |
|
| 16 |
|
| 17 |
def setup(templates, *args, **kwargs): |
| 18 |
translate_setup = base_setup(templates, *args, **kwargs) |
| 19 |
trans_setup = base_setup( |
| 20 |
{ |
| 21 |
name: template.replace("{% translate ", "{% trans ") |
| 22 |
for name, template in templates.items() |
| 23 |
} |
| 24 |
) |
| 25 |
|
| 26 |
tags = { |
| 27 |
"trans": trans_setup, |
| 28 |
"translate": translate_setup, |
| 29 |
} |
| 30 |
|
| 31 |
def decorator(func): |
| 32 |
@wraps(func) |
| 33 |
def inner(self, *args): |
| 34 |
signature = inspect.signature(func) |
| 35 |
for tag_name, setup_func in tags.items(): |
| 36 |
if "tag_name" in signature.parameters: |
| 37 |
setup_func(partial(func, tag_name=tag_name))(self) |
| 38 |
else: |
| 39 |
setup_func(func)(self) |
| 40 |
|
| 41 |
return inner |
| 42 |
|
| 43 |
return decorator |
| 44 |
|
| 45 |
|
| 46 |
class I18nTransTagTests(SimpleTestCase): |
| 47 |
libraries = {"i18n": "django.templatetags.i18n"} |
| 48 |
|
| 49 |
@setup({"i18n01": "{% load i18n %}{% translate 'xxxyyyxxx' %}"}) |
| 50 |
def test_i18n01(self): |
| 51 |
"""simple translation of a string delimited by '.""" |
| 52 |
output = self.engine.render_to_string("i18n01") |
| 53 |
self.assertEqual(output, "xxxyyyxxx") |
| 54 |
|
| 55 |
@setup({"i18n02": '{% load i18n %}{% translate "xxxyyyxxx" %}'}) |
| 56 |
def test_i18n02(self): |
| 57 |
"""simple translation of a string delimited by ".""" |
| 58 |
output = self.engine.render_to_string("i18n02") |
| 59 |
self.assertEqual(output, "xxxyyyxxx") |
| 60 |
|
| 61 |
@setup({"i18n06": '{% load i18n %}{% translate "Page not found" %}'}) |
| 62 |
def test_i18n06(self): |
| 63 |
"""simple translation of a string to German""" |
| 64 |
with translation.override("de"): |
| 65 |
output = self.engine.render_to_string("i18n06") |
| 66 |
self.assertEqual(output, "Seite nicht gefunden") |
| 67 |
|
| 68 |
@setup({"i18n09": '{% load i18n %}{% translate "Page not found" noop %}'}) |
| 69 |
def test_i18n09(self): |
| 70 |
"""simple non-translation (only marking) of a string to German""" |
| 71 |
with translation.override("de"): |
| 72 |
output = self.engine.render_to_string("i18n09") |
| 73 |
self.assertEqual(output, "Page not found") |
| 74 |
|
| 75 |
@setup({"i18n20": "{% load i18n %}{% translate andrew %}"}) |
| 76 |
def test_i18n20(self): |
| 77 |
output = self.engine.render_to_string("i18n20", {"andrew": "a & b"}) |
| 78 |
self.assertEqual(output, "a & b") |
| 79 |
|
| 80 |
@setup({"i18n22": "{% load i18n %}{% translate andrew %}"}) |
| 81 |
def test_i18n22(self): |
| 82 |
output = self.engine.render_to_string("i18n22", {"andrew": mark_safe("a & b")}) |
| 83 |
self.assertEqual(output, "a & b") |
| 84 |
|
| 85 |
@setup( |
| 86 |
{ |
| 87 |
"i18n23": ( |
| 88 |
'{% load i18n %}{% translate "Page not found"|capfirst|slice:"6:" %}' |
| 89 |
) |
| 90 |
} |
| 91 |
) |
| 92 |
def test_i18n23(self): |
| 93 |
"""Using filters with the {% translate %} tag (#5972).""" |
| 94 |
with translation.override("de"): |
| 95 |
output = self.engine.render_to_string("i18n23") |
| 96 |
self.assertEqual(output, "nicht gefunden") |
| 97 |
|
| 98 |
@setup({"i18n24": "{% load i18n %}{% translate 'Page not found'|upper %}"}) |
| 99 |
def test_i18n24(self): |
| 100 |
with translation.override("de"): |
| 101 |
output = self.engine.render_to_string("i18n24") |
| 102 |
self.assertEqual(output, "SEITE NICHT GEFUNDEN") |
| 103 |
|
| 104 |
@setup({"i18n25": "{% load i18n %}{% translate somevar|upper %}"}) |
| 105 |
def test_i18n25(self): |
| 106 |
with translation.override("de"): |
| 107 |
output = self.engine.render_to_string( |
| 108 |
"i18n25", {"somevar": "Page not found"} |
| 109 |
) |
| 110 |
self.assertEqual(output, "SEITE NICHT GEFUNDEN") |
| 111 |
|
| 112 |
# trans tag with as var |
| 113 |
@setup( |
| 114 |
{ |
| 115 |
"i18n35": ( |
| 116 |
'{% load i18n %}{% translate "Page not found" as page_not_found %}' |
| 117 |
"{{ page_not_found }}" |
| 118 |
) |
| 119 |
} |
| 120 |
) |
| 121 |
def test_i18n35(self): |
| 122 |
with translation.override("de"): |
| 123 |
output = self.engine.render_to_string("i18n35") |
| 124 |
self.assertEqual(output, "Seite nicht gefunden") |
| 125 |
|
| 126 |
@setup( |
| 127 |
{ |
| 128 |
"i18n36": ( |
| 129 |
'{% load i18n %}{% translate "Page not found" noop as page_not_found %}' |
| 130 |
"{{ page_not_found }}" |
| 131 |
) |
| 132 |
} |
| 133 |
) |
| 134 |
def test_i18n36(self): |
| 135 |
with translation.override("de"): |
| 136 |
output = self.engine.render_to_string("i18n36") |
| 137 |
self.assertEqual(output, "Page not found") |
| 138 |
|
| 139 |
@setup({"template": "{% load i18n %}{% translate %}A}"}) |
| 140 |
def test_syntax_error_no_arguments(self, tag_name): |
| 141 |
msg = "'{}' takes at least one argument".format(tag_name) |
| 142 |
with self.assertRaisesMessage(TemplateSyntaxError, msg): |
| 143 |
self.engine.render_to_string("template") |
| 144 |
|
| 145 |
@setup({"template": '{% load i18n %}{% translate "Yes" badoption %}'}) |
| 146 |
def test_syntax_error_bad_option(self, tag_name): |
| 147 |
msg = "Unknown argument for '{}' tag: 'badoption'".format(tag_name) |
| 148 |
with self.assertRaisesMessage(TemplateSyntaxError, msg): |
| 149 |
self.engine.render_to_string("template") |
| 150 |
|
| 151 |
@setup({"template": '{% load i18n %}{% translate "Yes" as %}'}) |
| 152 |
def test_syntax_error_missing_assignment(self, tag_name): |
| 153 |
msg = "No argument provided to the '{}' tag for the as option.".format(tag_name) |
| 154 |
with self.assertRaisesMessage(TemplateSyntaxError, msg): |
| 155 |
self.engine.render_to_string("template") |
| 156 |
|
| 157 |
@setup({"template": '{% load i18n %}{% translate "Yes" as var context %}'}) |
| 158 |
def test_syntax_error_missing_context(self, tag_name): |
| 159 |
msg = "No argument provided to the '{}' tag for the context option.".format( |
| 160 |
tag_name |
| 161 |
) |
| 162 |
with self.assertRaisesMessage(TemplateSyntaxError, msg): |
| 163 |
self.engine.render_to_string("template") |
| 164 |
|
| 165 |
@setup({"template": '{% load i18n %}{% translate "Yes" context as var %}'}) |
| 166 |
def test_syntax_error_context_as(self, tag_name): |
| 167 |
msg = ( |
| 168 |
f"Invalid argument 'as' provided to the '{tag_name}' tag for the context " |
| 169 |
f"option" |
| 170 |
) |
| 171 |
with self.assertRaisesMessage(TemplateSyntaxError, msg): |
| 172 |
self.engine.render_to_string("template") |
| 173 |
|
| 174 |
@setup({"template": '{% load i18n %}{% translate "Yes" context noop %}'}) |
| 175 |
def test_syntax_error_context_noop(self, tag_name): |
| 176 |
msg = ( |
| 177 |
f"Invalid argument 'noop' provided to the '{tag_name}' tag for the context " |
| 178 |
f"option" |
| 179 |
) |
| 180 |
with self.assertRaisesMessage(TemplateSyntaxError, msg): |
| 181 |
self.engine.render_to_string("template") |
| 182 |
|
| 183 |
@setup({"template": '{% load i18n %}{% translate "Yes" noop noop %}'}) |
| 184 |
def test_syntax_error_duplicate_option(self): |
| 185 |
msg = "The 'noop' option was specified more than once." |
| 186 |
with self.assertRaisesMessage(TemplateSyntaxError, msg): |
| 187 |
self.engine.render_to_string("template") |
| 188 |
|
| 189 |
@setup({"template": '{% load i18n %}{% translate "%s" %}'}) |
| 190 |
def test_trans_tag_using_a_string_that_looks_like_str_fmt(self): |
| 191 |
output = self.engine.render_to_string("template") |
| 192 |
self.assertEqual(output, "%s") |
| 193 |
|
| 194 |
|
| 195 |
class TranslationTransTagTests(SimpleTestCase): |
| 196 |
tag_name = "trans" |
| 197 |
|
| 198 |
def get_template(self, template_string): |
| 199 |
return Template( |
| 200 |
template_string.replace("{{% translate ", "{{% {}".format(self.tag_name)) |
| 201 |
) |
| 202 |
|
| 203 |
@override_settings(LOCALE_PATHS=extended_locale_paths) |
| 204 |
def test_template_tags_pgettext(self): |
| 205 |
"""{% translate %} takes message contexts into account (#14806).""" |
| 206 |
trans_real._active = Local() |
| 207 |
trans_real._translations = {} |
| 208 |
with translation.override("de"): |
| 209 |
# Nonexistent context... |
| 210 |
t = self.get_template( |
| 211 |
'{% load i18n %}{% translate "May" context "nonexistent" %}' |
| 212 |
) |
| 213 |
rendered = t.render(Context()) |
| 214 |
self.assertEqual(rendered, "May") |
| 215 |
|
| 216 |
# Existing context... using a literal |
| 217 |
t = self.get_template( |
| 218 |
'{% load i18n %}{% translate "May" context "month name" %}' |
| 219 |
) |
| 220 |
rendered = t.render(Context()) |
| 221 |
self.assertEqual(rendered, "Mai") |
| 222 |
t = self.get_template('{% load i18n %}{% translate "May" context "verb" %}') |
| 223 |
rendered = t.render(Context()) |
| 224 |
self.assertEqual(rendered, "Kann") |
| 225 |
|
| 226 |
# Using a variable |
| 227 |
t = self.get_template( |
| 228 |
'{% load i18n %}{% translate "May" context message_context %}' |
| 229 |
) |
| 230 |
rendered = t.render(Context({"message_context": "month name"})) |
| 231 |
self.assertEqual(rendered, "Mai") |
| 232 |
t = self.get_template( |
| 233 |
'{% load i18n %}{% translate "May" context message_context %}' |
| 234 |
) |
| 235 |
rendered = t.render(Context({"message_context": "verb"})) |
| 236 |
self.assertEqual(rendered, "Kann") |
| 237 |
|
| 238 |
# Using a filter |
| 239 |
t = self.get_template( |
| 240 |
'{% load i18n %}{% translate "May" context message_context|lower %}' |
| 241 |
) |
| 242 |
rendered = t.render(Context({"message_context": "MONTH NAME"})) |
| 243 |
self.assertEqual(rendered, "Mai") |
| 244 |
t = self.get_template( |
| 245 |
'{% load i18n %}{% translate "May" context message_context|lower %}' |
| 246 |
) |
| 247 |
rendered = t.render(Context({"message_context": "VERB"})) |
| 248 |
self.assertEqual(rendered, "Kann") |
| 249 |
|
| 250 |
# Using 'as' |
| 251 |
t = self.get_template( |
| 252 |
'{% load i18n %}{% translate "May" context "month name" as var %}' |
| 253 |
"Value: {{ var }}" |
| 254 |
) |
| 255 |
rendered = t.render(Context()) |
| 256 |
self.assertEqual(rendered, "Value: Mai") |
| 257 |
t = self.get_template( |
| 258 |
'{% load i18n %}{% translate "May" as var context "verb" %}Value: ' |
| 259 |
"{{ var }}" |
| 260 |
) |
| 261 |
rendered = t.render(Context()) |
| 262 |
self.assertEqual(rendered, "Value: Kann") |
| 263 |
|
| 264 |
|
| 265 |
class TranslationTranslateTagTests(TranslationTransTagTests): |
| 266 |
tag_name = "translate" |
| 267 |
|
| 268 |
|
| 269 |
class MultipleLocaleActivationTransTagTests(MultipleLocaleActivationTestCase): |
| 270 |
tag_name = "trans" |
| 271 |
|
| 272 |
def get_template(self, template_string): |
| 273 |
return Template( |
| 274 |
template_string.replace("{{% translate ", "{{% {}".format(self.tag_name)) |
| 275 |
) |
| 276 |
|
| 277 |
def test_single_locale_activation(self): |
| 278 |
""" |
| 279 |
Simple baseline behavior with one locale for all the supported i18n |
| 280 |
constructs. |
| 281 |
""" |
| 282 |
with translation.override("fr"): |
| 283 |
self.assertEqual( |
| 284 |
self.get_template("{% load i18n %}{% translate 'Yes' %}").render( |
| 285 |
Context({}) |
| 286 |
), |
| 287 |
"Oui", |
| 288 |
) |
| 289 |
|
| 290 |
def test_multiple_locale_trans(self): |
| 291 |
with translation.override("de"): |
| 292 |
t = self.get_template("{% load i18n %}{% translate 'No' %}") |
| 293 |
with translation.override(self._old_language), translation.override("nl"): |
| 294 |
self.assertEqual(t.render(Context({})), "Nee") |
| 295 |
|
| 296 |
def test_multiple_locale_deactivate_trans(self): |
| 297 |
with translation.override("de", deactivate=True): |
| 298 |
t = self.get_template("{% load i18n %}{% translate 'No' %}") |
| 299 |
with translation.override("nl"): |
| 300 |
self.assertEqual(t.render(Context({})), "Nee") |
| 301 |
|
| 302 |
def test_multiple_locale_direct_switch_trans(self): |
| 303 |
with translation.override("de"): |
| 304 |
t = self.get_template("{% load i18n %}{% translate 'No' %}") |
| 305 |
with translation.override("nl"): |
| 306 |
self.assertEqual(t.render(Context({})), "Nee") |
| 307 |
|
| 308 |
|
| 309 |
class MultipleLocaleActivationTranslateTagTests(MultipleLocaleActivationTransTagTests): |
| 310 |
tag_name = "translate" |
| 311 |
|
| 312 |
|
| 313 |
class LocalizeNodeTests(SimpleTestCase): |
| 314 |
def test_repr(self): |
| 315 |
node = LocalizeNode(nodelist=[], use_l10n=True) |
| 316 |
self.assertEqual(repr(node), "<LocalizeNode>") |