| 1 |
import inspect |
| 2 |
import os |
| 3 |
from functools import partial, wraps |
| 4 |
|
| 5 |
from asgiref.local import Local |
| 6 |
|
| 7 |
from django.template import Context, Template, TemplateSyntaxError |
| 8 |
from django.template.base import Token, TokenType |
| 9 |
from django.templatetags.i18n import BlockTranslateNode |
| 10 |
from django.test import SimpleTestCase, override_settings |
| 11 |
from django.utils import translation |
| 12 |
from django.utils.safestring import mark_safe |
| 13 |
from django.utils.translation import trans_real |
| 14 |
|
| 15 |
from ...utils import setup as base_setup |
| 16 |
from .base import MultipleLocaleActivationTestCase, extended_locale_paths, here |
| 17 |
|
| 18 |
|
| 19 |
def setup(templates, *args, **kwargs): |
| 20 |
blocktranslate_setup = base_setup(templates, *args, **kwargs) |
| 21 |
blocktrans_setup = base_setup( |
| 22 |
{ |
| 23 |
name: template.replace("{% blocktranslate ", "{% blocktrans ").replace( |
| 24 |
"{% endblocktranslate %}", "{% endblocktrans %}" |
| 25 |
) |
| 26 |
for name, template in templates.items() |
| 27 |
} |
| 28 |
) |
| 29 |
|
| 30 |
tags = { |
| 31 |
"blocktrans": blocktrans_setup, |
| 32 |
"blocktranslate": blocktranslate_setup, |
| 33 |
} |
| 34 |
|
| 35 |
def decorator(func): |
| 36 |
@wraps(func) |
| 37 |
def inner(self, *args): |
| 38 |
signature = inspect.signature(func) |
| 39 |
for tag_name, setup_func in tags.items(): |
| 40 |
if "tag_name" in signature.parameters: |
| 41 |
setup_func(partial(func, tag_name=tag_name))(self) |
| 42 |
else: |
| 43 |
setup_func(func)(self) |
| 44 |
|
| 45 |
return inner |
| 46 |
|
| 47 |
return decorator |
| 48 |
|
| 49 |
|
| 50 |
class I18nBlockTransTagTests(SimpleTestCase): |
| 51 |
libraries = {"i18n": "django.templatetags.i18n"} |
| 52 |
|
| 53 |
@setup( |
| 54 |
{ |
| 55 |
"i18n03": ( |
| 56 |
"{% load i18n %}{% blocktranslate %}{{ anton }}{% endblocktranslate %}" |
| 57 |
) |
| 58 |
} |
| 59 |
) |
| 60 |
def test_i18n03(self): |
| 61 |
"""simple translation of a variable""" |
| 62 |
output = self.engine.render_to_string("i18n03", {"anton": "Å"}) |
| 63 |
self.assertEqual(output, "Å") |
| 64 |
|
| 65 |
@setup( |
| 66 |
{ |
| 67 |
"i18n04": ( |
| 68 |
"{% load i18n %}{% blocktranslate with berta=anton|lower %}{{ berta }}" |
| 69 |
"{% endblocktranslate %}" |
| 70 |
) |
| 71 |
} |
| 72 |
) |
| 73 |
def test_i18n04(self): |
| 74 |
"""simple translation of a variable and filter""" |
| 75 |
output = self.engine.render_to_string("i18n04", {"anton": "Å"}) |
| 76 |
self.assertEqual(output, "å") |
| 77 |
|
| 78 |
@setup( |
| 79 |
{ |
| 80 |
"legacyi18n04": ( |
| 81 |
"{% load i18n %}" |
| 82 |
"{% blocktranslate with anton|lower as berta %}{{ berta }}" |
| 83 |
"{% endblocktranslate %}" |
| 84 |
) |
| 85 |
} |
| 86 |
) |
| 87 |
def test_legacyi18n04(self): |
| 88 |
"""simple translation of a variable and filter""" |
| 89 |
output = self.engine.render_to_string("legacyi18n04", {"anton": "Å"}) |
| 90 |
self.assertEqual(output, "å") |
| 91 |
|
| 92 |
@setup( |
| 93 |
{ |
| 94 |
"i18n05": ( |
| 95 |
"{% load i18n %}{% blocktranslate %}xxx{{ anton }}xxx" |
| 96 |
"{% endblocktranslate %}" |
| 97 |
) |
| 98 |
} |
| 99 |
) |
| 100 |
def test_i18n05(self): |
| 101 |
"""simple translation of a string with interpolation""" |
| 102 |
output = self.engine.render_to_string("i18n05", {"anton": "yyy"}) |
| 103 |
self.assertEqual(output, "xxxyyyxxx") |
| 104 |
|
| 105 |
@setup( |
| 106 |
{ |
| 107 |
"i18n07": "{% load i18n %}" |
| 108 |
"{% blocktranslate count counter=number %}singular{% plural %}" |
| 109 |
"{{ counter }} plural{% endblocktranslate %}" |
| 110 |
} |
| 111 |
) |
| 112 |
def test_i18n07(self): |
| 113 |
"""translation of singular form""" |
| 114 |
output = self.engine.render_to_string("i18n07", {"number": 1}) |
| 115 |
self.assertEqual(output, "singular") |
| 116 |
|
| 117 |
@setup( |
| 118 |
{ |
| 119 |
"legacyi18n07": "{% load i18n %}" |
| 120 |
"{% blocktranslate count number as counter %}singular{% plural %}" |
| 121 |
"{{ counter }} plural{% endblocktranslate %}" |
| 122 |
} |
| 123 |
) |
| 124 |
def test_legacyi18n07(self): |
| 125 |
"""translation of singular form""" |
| 126 |
output = self.engine.render_to_string("legacyi18n07", {"number": 1}) |
| 127 |
self.assertEqual(output, "singular") |
| 128 |
|
| 129 |
@setup( |
| 130 |
{ |
| 131 |
"i18n08": "{% load i18n %}" |
| 132 |
"{% blocktranslate count number as counter %}singular{% plural %}" |
| 133 |
"{{ counter }} plural{% endblocktranslate %}" |
| 134 |
} |
| 135 |
) |
| 136 |
def test_i18n08(self): |
| 137 |
"""translation of plural form""" |
| 138 |
output = self.engine.render_to_string("i18n08", {"number": 2}) |
| 139 |
self.assertEqual(output, "2 plural") |
| 140 |
|
| 141 |
@setup( |
| 142 |
{ |
| 143 |
"legacyi18n08": "{% load i18n %}" |
| 144 |
"{% blocktranslate count counter=number %}singular{% plural %}" |
| 145 |
"{{ counter }} plural{% endblocktranslate %}" |
| 146 |
} |
| 147 |
) |
| 148 |
def test_legacyi18n08(self): |
| 149 |
"""translation of plural form""" |
| 150 |
output = self.engine.render_to_string("legacyi18n08", {"number": 2}) |
| 151 |
self.assertEqual(output, "2 plural") |
| 152 |
|
| 153 |
@setup( |
| 154 |
{ |
| 155 |
"i18n17": ( |
| 156 |
"{% load i18n %}" |
| 157 |
"{% blocktranslate with berta=anton|escape %}{{ berta }}" |
| 158 |
"{% endblocktranslate %}" |
| 159 |
) |
| 160 |
} |
| 161 |
) |
| 162 |
def test_i18n17(self): |
| 163 |
""" |
| 164 |
Escaping inside blocktranslate and translate works as if it was |
| 165 |
directly in the template. |
| 166 |
""" |
| 167 |
output = self.engine.render_to_string("i18n17", {"anton": "α & β"}) |
| 168 |
self.assertEqual(output, "α & β") |
| 169 |
|
| 170 |
@setup( |
| 171 |
{ |
| 172 |
"i18n18": ( |
| 173 |
"{% load i18n %}" |
| 174 |
"{% blocktranslate with berta=anton|force_escape %}{{ berta }}" |
| 175 |
"{% endblocktranslate %}" |
| 176 |
) |
| 177 |
} |
| 178 |
) |
| 179 |
def test_i18n18(self): |
| 180 |
output = self.engine.render_to_string("i18n18", {"anton": "α & β"}) |
| 181 |
self.assertEqual(output, "α & β") |
| 182 |
|
| 183 |
@setup( |
| 184 |
{ |
| 185 |
"i18n19": ( |
| 186 |
"{% load i18n %}{% blocktranslate %}{{ andrew }}{% endblocktranslate %}" |
| 187 |
) |
| 188 |
} |
| 189 |
) |
| 190 |
def test_i18n19(self): |
| 191 |
output = self.engine.render_to_string("i18n19", {"andrew": "a & b"}) |
| 192 |
self.assertEqual(output, "a & b") |
| 193 |
|
| 194 |
@setup( |
| 195 |
{ |
| 196 |
"i18n21": ( |
| 197 |
"{% load i18n %}{% blocktranslate %}{{ andrew }}{% endblocktranslate %}" |
| 198 |
) |
| 199 |
} |
| 200 |
) |
| 201 |
def test_i18n21(self): |
| 202 |
output = self.engine.render_to_string("i18n21", {"andrew": mark_safe("a & b")}) |
| 203 |
self.assertEqual(output, "a & b") |
| 204 |
|
| 205 |
@setup( |
| 206 |
{ |
| 207 |
"legacyi18n17": ( |
| 208 |
"{% load i18n %}" |
| 209 |
"{% blocktranslate with anton|escape as berta %}{{ berta }}" |
| 210 |
"{% endblocktranslate %}" |
| 211 |
) |
| 212 |
} |
| 213 |
) |
| 214 |
def test_legacyi18n17(self): |
| 215 |
output = self.engine.render_to_string("legacyi18n17", {"anton": "α & β"}) |
| 216 |
self.assertEqual(output, "α & β") |
| 217 |
|
| 218 |
@setup( |
| 219 |
{ |
| 220 |
"legacyi18n18": "{% load i18n %}" |
| 221 |
"{% blocktranslate with anton|force_escape as berta %}" |
| 222 |
"{{ berta }}{% endblocktranslate %}" |
| 223 |
} |
| 224 |
) |
| 225 |
def test_legacyi18n18(self): |
| 226 |
output = self.engine.render_to_string("legacyi18n18", {"anton": "α & β"}) |
| 227 |
self.assertEqual(output, "α & β") |
| 228 |
|
| 229 |
@setup( |
| 230 |
{ |
| 231 |
"i18n26": "{% load i18n %}" |
| 232 |
"{% blocktranslate with extra_field=myextra_field count counter=number %}" |
| 233 |
"singular {{ extra_field }}{% plural %}plural{% endblocktranslate %}" |
| 234 |
} |
| 235 |
) |
| 236 |
def test_i18n26(self): |
| 237 |
""" |
| 238 |
translation of plural form with extra field in singular form (#13568) |
| 239 |
""" |
| 240 |
output = self.engine.render_to_string( |
| 241 |
"i18n26", {"myextra_field": "test", "number": 1} |
| 242 |
) |
| 243 |
self.assertEqual(output, "singular test") |
| 244 |
|
| 245 |
@setup( |
| 246 |
{ |
| 247 |
"legacyi18n26": ( |
| 248 |
"{% load i18n %}" |
| 249 |
"{% blocktranslate with myextra_field as extra_field " |
| 250 |
"count number as counter %}singular {{ extra_field }}{% plural %}plural" |
| 251 |
"{% endblocktranslate %}" |
| 252 |
) |
| 253 |
} |
| 254 |
) |
| 255 |
def test_legacyi18n26(self): |
| 256 |
output = self.engine.render_to_string( |
| 257 |
"legacyi18n26", {"myextra_field": "test", "number": 1} |
| 258 |
) |
| 259 |
self.assertEqual(output, "singular test") |
| 260 |
|
| 261 |
@setup( |
| 262 |
{ |
| 263 |
"i18n27": "{% load i18n %}{% blocktranslate count counter=number %}" |
| 264 |
"{{ counter }} result{% plural %}{{ counter }} results" |
| 265 |
"{% endblocktranslate %}" |
| 266 |
} |
| 267 |
) |
| 268 |
def test_i18n27(self): |
| 269 |
"""translation of singular form in Russian (#14126)""" |
| 270 |
with translation.override("ru"): |
| 271 |
output = self.engine.render_to_string("i18n27", {"number": 1}) |
| 272 |
self.assertEqual( |
| 273 |
output, "1 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442" |
| 274 |
) |
| 275 |
|
| 276 |
@setup( |
| 277 |
{ |
| 278 |
"legacyi18n27": "{% load i18n %}" |
| 279 |
"{% blocktranslate count number as counter %}{{ counter }} result" |
| 280 |
"{% plural %}{{ counter }} results{% endblocktranslate %}" |
| 281 |
} |
| 282 |
) |
| 283 |
def test_legacyi18n27(self): |
| 284 |
with translation.override("ru"): |
| 285 |
output = self.engine.render_to_string("legacyi18n27", {"number": 1}) |
| 286 |
self.assertEqual( |
| 287 |
output, "1 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442" |
| 288 |
) |
| 289 |
|
| 290 |
@setup( |
| 291 |
{ |
| 292 |
"i18n28": ( |
| 293 |
"{% load i18n %}" |
| 294 |
"{% blocktranslate with a=anton b=berta %}{{ a }} + {{ b }}" |
| 295 |
"{% endblocktranslate %}" |
| 296 |
) |
| 297 |
} |
| 298 |
) |
| 299 |
def test_i18n28(self): |
| 300 |
"""simple translation of multiple variables""" |
| 301 |
output = self.engine.render_to_string("i18n28", {"anton": "α", "berta": "β"}) |
| 302 |
self.assertEqual(output, "α + β") |
| 303 |
|
| 304 |
@setup( |
| 305 |
{ |
| 306 |
"legacyi18n28": "{% load i18n %}" |
| 307 |
"{% blocktranslate with anton as a and berta as b %}" |
| 308 |
"{{ a }} + {{ b }}{% endblocktranslate %}" |
| 309 |
} |
| 310 |
) |
| 311 |
def test_legacyi18n28(self): |
| 312 |
output = self.engine.render_to_string( |
| 313 |
"legacyi18n28", {"anton": "α", "berta": "β"} |
| 314 |
) |
| 315 |
self.assertEqual(output, "α + β") |
| 316 |
|
| 317 |
# blocktranslate handling of variables which are not in the context. |
| 318 |
# this should work as if blocktranslate was not there (#19915) |
| 319 |
@setup( |
| 320 |
{ |
| 321 |
"i18n34": ( |
| 322 |
"{% load i18n %}{% blocktranslate %}{{ missing }}" |
| 323 |
"{% endblocktranslate %}" |
| 324 |
) |
| 325 |
} |
| 326 |
) |
| 327 |
def test_i18n34(self): |
| 328 |
output = self.engine.render_to_string("i18n34") |
| 329 |
if self.engine.string_if_invalid: |
| 330 |
self.assertEqual(output, "INVALID") |
| 331 |
else: |
| 332 |
self.assertEqual(output, "") |
| 333 |
|
| 334 |
@setup( |
| 335 |
{ |
| 336 |
"i18n34_2": ( |
| 337 |
"{% load i18n %}{% blocktranslate with a='α' %}{{ missing }}" |
| 338 |
"{% endblocktranslate %}" |
| 339 |
) |
| 340 |
} |
| 341 |
) |
| 342 |
def test_i18n34_2(self): |
| 343 |
output = self.engine.render_to_string("i18n34_2") |
| 344 |
if self.engine.string_if_invalid: |
| 345 |
self.assertEqual(output, "INVALID") |
| 346 |
else: |
| 347 |
self.assertEqual(output, "") |
| 348 |
|
| 349 |
@setup( |
| 350 |
{ |
| 351 |
"i18n34_3": ( |
| 352 |
"{% load i18n %}{% blocktranslate with a=anton %}{{ missing }}" |
| 353 |
"{% endblocktranslate %}" |
| 354 |
) |
| 355 |
} |
| 356 |
) |
| 357 |
def test_i18n34_3(self): |
| 358 |
output = self.engine.render_to_string("i18n34_3", {"anton": "\xce\xb1"}) |
| 359 |
if self.engine.string_if_invalid: |
| 360 |
self.assertEqual(output, "INVALID") |
| 361 |
else: |
| 362 |
self.assertEqual(output, "") |
| 363 |
|
| 364 |
@setup( |
| 365 |
{ |
| 366 |
"i18n37": "{% load i18n %}" |
| 367 |
'{% translate "Page not found" as page_not_found %}' |
| 368 |
"{% blocktranslate %}Error: {{ page_not_found }}{% endblocktranslate %}" |
| 369 |
} |
| 370 |
) |
| 371 |
def test_i18n37(self): |
| 372 |
with translation.override("de"): |
| 373 |
output = self.engine.render_to_string("i18n37") |
| 374 |
self.assertEqual(output, "Error: Seite nicht gefunden") |
| 375 |
|
| 376 |
# blocktranslate tag with asvar |
| 377 |
@setup( |
| 378 |
{ |
| 379 |
"i18n39": ( |
| 380 |
"{% load i18n %}" |
| 381 |
"{% blocktranslate asvar page_not_found %}Page not found" |
| 382 |
"{% endblocktranslate %}>{{ page_not_found }}<" |
| 383 |
) |
| 384 |
} |
| 385 |
) |
| 386 |
def test_i18n39(self): |
| 387 |
with translation.override("de"): |
| 388 |
output = self.engine.render_to_string("i18n39") |
| 389 |
self.assertEqual(output, ">Seite nicht gefunden<") |
| 390 |
|
| 391 |
@setup( |
| 392 |
{ |
| 393 |
"i18n40": "{% load i18n %}" |
| 394 |
'{% translate "Page not found" as pg_404 %}' |
| 395 |
"{% blocktranslate with page_not_found=pg_404 asvar output %}" |
| 396 |
"Error: {{ page_not_found }}" |
| 397 |
"{% endblocktranslate %}" |
| 398 |
} |
| 399 |
) |
| 400 |
def test_i18n40(self): |
| 401 |
output = self.engine.render_to_string("i18n40") |
| 402 |
self.assertEqual(output, "") |
| 403 |
|
| 404 |
@setup( |
| 405 |
{ |
| 406 |
"i18n41": "{% load i18n %}" |
| 407 |
'{% translate "Page not found" as pg_404 %}' |
| 408 |
"{% blocktranslate with page_not_found=pg_404 asvar output %}" |
| 409 |
"Error: {{ page_not_found }}" |
| 410 |
"{% endblocktranslate %}" |
| 411 |
">{{ output }}<" |
| 412 |
} |
| 413 |
) |
| 414 |
def test_i18n41(self): |
| 415 |
with translation.override("de"): |
| 416 |
output = self.engine.render_to_string("i18n41") |
| 417 |
self.assertEqual(output, ">Error: Seite nicht gefunden<") |
| 418 |
|
| 419 |
@setup( |
| 420 |
{ |
| 421 |
"i18n_asvar_safestring": ( |
| 422 |
"{% load i18n %}" |
| 423 |
"{% blocktranslate asvar the_title %}" |
| 424 |
"{{title}}other text" |
| 425 |
"{% endblocktranslate %}" |
| 426 |
"{{ the_title }}" |
| 427 |
) |
| 428 |
} |
| 429 |
) |
| 430 |
def test_i18n_asvar_safestring(self): |
| 431 |
context = {"title": "<Main Title>"} |
| 432 |
output = self.engine.render_to_string("i18n_asvar_safestring", context=context) |
| 433 |
self.assertEqual(output, "<Main Title>other text") |
| 434 |
|
| 435 |
@setup( |
| 436 |
{ |
| 437 |
"template": ( |
| 438 |
"{% load i18n %}{% blocktranslate asvar %}Yes{% endblocktranslate %}" |
| 439 |
) |
| 440 |
} |
| 441 |
) |
| 442 |
def test_blocktrans_syntax_error_missing_assignment(self, tag_name): |
| 443 |
msg = "No argument provided to the '{}' tag for the asvar option.".format( |
| 444 |
tag_name |
| 445 |
) |
| 446 |
with self.assertRaisesMessage(TemplateSyntaxError, msg): |
| 447 |
self.engine.render_to_string("template") |
| 448 |
|
| 449 |
@setup({"template": "{% load i18n %}{% blocktranslate %}%s{% endblocktranslate %}"}) |
| 450 |
def test_blocktrans_tag_using_a_string_that_looks_like_str_fmt(self): |
| 451 |
output = self.engine.render_to_string("template") |
| 452 |
self.assertEqual(output, "%s") |
| 453 |
|
| 454 |
@setup( |
| 455 |
{ |
| 456 |
"template": ( |
| 457 |
"{% load i18n %}{% blocktranslate %}{% block b %} {% endblock %}" |
| 458 |
"{% endblocktranslate %}" |
| 459 |
) |
| 460 |
} |
| 461 |
) |
| 462 |
def test_with_block(self, tag_name): |
| 463 |
msg = "'{}' doesn't allow other block tags (seen 'block b') inside it".format( |
| 464 |
tag_name |
| 465 |
) |
| 466 |
with self.assertRaisesMessage(TemplateSyntaxError, msg): |
| 467 |
self.engine.render_to_string("template") |
| 468 |
|
| 469 |
@setup( |
| 470 |
{ |
| 471 |
"template": ( |
| 472 |
"{% load i18n %}" |
| 473 |
"{% blocktranslate %}{% for b in [1, 2, 3] %} {% endfor %}" |
| 474 |
"{% endblocktranslate %}" |
| 475 |
) |
| 476 |
} |
| 477 |
) |
| 478 |
def test_with_for(self, tag_name): |
| 479 |
msg = ( |
| 480 |
f"'{tag_name}' doesn't allow other block tags (seen 'for b in [1, 2, 3]') " |
| 481 |
f"inside it" |
| 482 |
) |
| 483 |
with self.assertRaisesMessage(TemplateSyntaxError, msg): |
| 484 |
self.engine.render_to_string("template") |
| 485 |
|
| 486 |
@setup( |
| 487 |
{ |
| 488 |
"template": ( |
| 489 |
"{% load i18n %}{% blocktranslate with foo=bar with %}{{ foo }}" |
| 490 |
"{% endblocktranslate %}" |
| 491 |
) |
| 492 |
} |
| 493 |
) |
| 494 |
def test_variable_twice(self): |
| 495 |
with self.assertRaisesMessage( |
| 496 |
TemplateSyntaxError, "The 'with' option was specified more than once" |
| 497 |
): |
| 498 |
self.engine.render_to_string("template", {"foo": "bar"}) |
| 499 |
|
| 500 |
@setup( |
| 501 |
{"template": "{% load i18n %}{% blocktranslate with %}{% endblocktranslate %}"} |
| 502 |
) |
| 503 |
def test_no_args_with(self, tag_name): |
| 504 |
msg = "\"with\" in '{}' tag needs at least one keyword argument.".format( |
| 505 |
tag_name |
| 506 |
) |
| 507 |
with self.assertRaisesMessage(TemplateSyntaxError, msg): |
| 508 |
self.engine.render_to_string("template") |
| 509 |
|
| 510 |
@setup( |
| 511 |
{ |
| 512 |
"template": ( |
| 513 |
"{% load i18n %}{% blocktranslate count a %}{% endblocktranslate %}" |
| 514 |
) |
| 515 |
} |
| 516 |
) |
| 517 |
def test_count(self, tag_name): |
| 518 |
msg = "\"count\" in '{}' tag expected exactly one keyword argument.".format( |
| 519 |
tag_name |
| 520 |
) |
| 521 |
with self.assertRaisesMessage(TemplateSyntaxError, msg): |
| 522 |
self.engine.render_to_string("template", {"a": [1, 2, 3]}) |
| 523 |
|
| 524 |
@setup( |
| 525 |
{ |
| 526 |
"template": ( |
| 527 |
"{% load i18n %}{% blocktranslate count counter=num %}{{ counter }}" |
| 528 |
"{% plural %}{{ counter }}{% endblocktranslate %}" |
| 529 |
) |
| 530 |
} |
| 531 |
) |
| 532 |
def test_count_not_number(self, tag_name): |
| 533 |
msg = "'counter' argument to '{}' tag must be a number.".format(tag_name) |
| 534 |
with self.assertRaisesMessage(TemplateSyntaxError, msg): |
| 535 |
self.engine.render_to_string("template", {"num": "1"}) |
| 536 |
|
| 537 |
@setup( |
| 538 |
{ |
| 539 |
"template": ( |
| 540 |
"{% load i18n %}{% blocktranslate count count=var|length %}" |
| 541 |
"There is {{ count }} object. {% block a %} {% endblock %}" |
| 542 |
"{% endblocktranslate %}" |
| 543 |
) |
| 544 |
} |
| 545 |
) |
| 546 |
def test_plural_bad_syntax(self, tag_name): |
| 547 |
msg = "'{}' doesn't allow other block tags inside it".format(tag_name) |
| 548 |
with self.assertRaisesMessage(TemplateSyntaxError, msg): |
| 549 |
self.engine.render_to_string("template", {"var": [1, 2, 3]}) |
| 550 |
|
| 551 |
|
| 552 |
class TranslationBlockTranslateTagTests(SimpleTestCase): |
| 553 |
tag_name = "blocktranslate" |
| 554 |
|
| 555 |
def get_template(self, template_string): |
| 556 |
return Template( |
| 557 |
template_string.replace( |
| 558 |
"{{% blocktranslate ", "{{% {}".format(self.tag_name) |
| 559 |
).replace( |
| 560 |
"{{% endblocktranslate %}}", "{{% end{} %}}".format(self.tag_name) |
| 561 |
) |
| 562 |
) |
| 563 |
|
| 564 |
@override_settings(LOCALE_PATHS=extended_locale_paths) |
| 565 |
def test_template_tags_pgettext(self): |
| 566 |
"""{% blocktranslate %} takes message contexts into account (#14806).""" |
| 567 |
trans_real._active = Local() |
| 568 |
trans_real._translations = {} |
| 569 |
with translation.override("de"): |
| 570 |
# Nonexistent context |
| 571 |
t = self.get_template( |
| 572 |
'{% load i18n %}{% blocktranslate context "nonexistent" %}May' |
| 573 |
"{% endblocktranslate %}" |
| 574 |
) |
| 575 |
rendered = t.render(Context()) |
| 576 |
self.assertEqual(rendered, "May") |
| 577 |
|
| 578 |
# Existing context... using a literal |
| 579 |
t = self.get_template( |
| 580 |
"{% load i18n %}" |
| 581 |
'{% blocktranslate context "month name" %}May{% endblocktranslate %}' |
| 582 |
) |
| 583 |
rendered = t.render(Context()) |
| 584 |
self.assertEqual(rendered, "Mai") |
| 585 |
t = self.get_template( |
| 586 |
"{% load i18n %}" |
| 587 |
'{% blocktranslate context "verb" %}May{% endblocktranslate %}' |
| 588 |
) |
| 589 |
rendered = t.render(Context()) |
| 590 |
self.assertEqual(rendered, "Kann") |
| 591 |
|
| 592 |
# Using a variable |
| 593 |
t = self.get_template( |
| 594 |
"{% load i18n %}{% blocktranslate context message_context %}" |
| 595 |
"May{% endblocktranslate %}" |
| 596 |
) |
| 597 |
rendered = t.render(Context({"message_context": "month name"})) |
| 598 |
self.assertEqual(rendered, "Mai") |
| 599 |
t = self.get_template( |
| 600 |
"{% load i18n %}{% blocktranslate context message_context %}" |
| 601 |
"May{% endblocktranslate %}" |
| 602 |
) |
| 603 |
rendered = t.render(Context({"message_context": "verb"})) |
| 604 |
self.assertEqual(rendered, "Kann") |
| 605 |
|
| 606 |
# Using a filter |
| 607 |
t = self.get_template( |
| 608 |
"{% load i18n %}" |
| 609 |
"{% blocktranslate context message_context|lower %}May" |
| 610 |
"{% endblocktranslate %}" |
| 611 |
) |
| 612 |
rendered = t.render(Context({"message_context": "MONTH NAME"})) |
| 613 |
self.assertEqual(rendered, "Mai") |
| 614 |
t = self.get_template( |
| 615 |
"{% load i18n %}" |
| 616 |
"{% blocktranslate context message_context|lower %}May" |
| 617 |
"{% endblocktranslate %}" |
| 618 |
) |
| 619 |
rendered = t.render(Context({"message_context": "VERB"})) |
| 620 |
self.assertEqual(rendered, "Kann") |
| 621 |
|
| 622 |
# Using 'count' |
| 623 |
t = self.get_template( |
| 624 |
"{% load i18n %}" |
| 625 |
'{% blocktranslate count number=1 context "super search" %}{{ number }}' |
| 626 |
" super result{% plural %}{{ number }} super results" |
| 627 |
"{% endblocktranslate %}" |
| 628 |
) |
| 629 |
rendered = t.render(Context()) |
| 630 |
self.assertEqual(rendered, "1 Super-Ergebnis") |
| 631 |
t = self.get_template( |
| 632 |
"{% load i18n %}" |
| 633 |
'{% blocktranslate count number=2 context "super search" %}{{ number }}' |
| 634 |
" super result{% plural %}{{ number }} super results" |
| 635 |
"{% endblocktranslate %}" |
| 636 |
) |
| 637 |
rendered = t.render(Context()) |
| 638 |
self.assertEqual(rendered, "2 Super-Ergebnisse") |
| 639 |
t = self.get_template( |
| 640 |
"{% load i18n %}" |
| 641 |
'{% blocktranslate context "other super search" count number=1 %}' |
| 642 |
"{{ number }} super result{% plural %}{{ number }} super results" |
| 643 |
"{% endblocktranslate %}" |
| 644 |
) |
| 645 |
rendered = t.render(Context()) |
| 646 |
self.assertEqual(rendered, "1 anderen Super-Ergebnis") |
| 647 |
t = self.get_template( |
| 648 |
"{% load i18n %}" |
| 649 |
'{% blocktranslate context "other super search" count number=2 %}' |
| 650 |
"{{ number }} super result{% plural %}{{ number }} super results" |
| 651 |
"{% endblocktranslate %}" |
| 652 |
) |
| 653 |
rendered = t.render(Context()) |
| 654 |
self.assertEqual(rendered, "2 andere Super-Ergebnisse") |
| 655 |
|
| 656 |
# Using 'with' |
| 657 |
t = self.get_template( |
| 658 |
"{% load i18n %}" |
| 659 |
'{% blocktranslate with num_comments=5 context "comment count" %}' |
| 660 |
"There are {{ num_comments }} comments{% endblocktranslate %}" |
| 661 |
) |
| 662 |
rendered = t.render(Context()) |
| 663 |
self.assertEqual(rendered, "Es gibt 5 Kommentare") |
| 664 |
t = self.get_template( |
| 665 |
"{% load i18n %}" |
| 666 |
'{% blocktranslate with num_comments=5 context "other comment count" %}' |
| 667 |
"There are {{ num_comments }} comments{% endblocktranslate %}" |
| 668 |
) |
| 669 |
rendered = t.render(Context()) |
| 670 |
self.assertEqual(rendered, "Andere: Es gibt 5 Kommentare") |
| 671 |
|
| 672 |
# Using trimmed |
| 673 |
t = self.get_template( |
| 674 |
"{% load i18n %}{% blocktranslate trimmed %}\n\nThere\n\t are 5 " |
| 675 |
"\n\n comments\n{% endblocktranslate %}" |
| 676 |
) |
| 677 |
rendered = t.render(Context()) |
| 678 |
self.assertEqual(rendered, "There are 5 comments") |
| 679 |
t = self.get_template( |
| 680 |
"{% load i18n %}" |
| 681 |
'{% blocktranslate with num_comments=5 context "comment count" trimmed ' |
| 682 |
"%}\n\n" |
| 683 |
"There are \t\n \t {{ num_comments }} comments\n\n" |
| 684 |
"{% endblocktranslate %}" |
| 685 |
) |
| 686 |
rendered = t.render(Context()) |
| 687 |
self.assertEqual(rendered, "Es gibt 5 Kommentare") |
| 688 |
t = self.get_template( |
| 689 |
"{% load i18n %}" |
| 690 |
'{% blocktranslate context "other super search" count number=2 trimmed ' |
| 691 |
"%}\n{{ number }} super \n result{% plural %}{{ number }} super results" |
| 692 |
"{% endblocktranslate %}" |
| 693 |
) |
| 694 |
rendered = t.render(Context()) |
| 695 |
self.assertEqual(rendered, "2 andere Super-Ergebnisse") |
| 696 |
|
| 697 |
# Misuses |
| 698 |
msg = "Unknown argument for 'blocktranslate' tag: %r." |
| 699 |
with self.assertRaisesMessage(TemplateSyntaxError, msg % 'month="May"'): |
| 700 |
self.get_template( |
| 701 |
'{% load i18n %}{% blocktranslate context with month="May" %}' |
| 702 |
"{{ month }}{% endblocktranslate %}" |
| 703 |
) |
| 704 |
msg = ( |
| 705 |
'"context" in %r tag expected exactly one argument.' % "blocktranslate" |
| 706 |
) |
| 707 |
with self.assertRaisesMessage(TemplateSyntaxError, msg): |
| 708 |
self.get_template( |
| 709 |
"{% load i18n %}{% blocktranslate context %}{% endblocktranslate %}" |
| 710 |
) |
| 711 |
with self.assertRaisesMessage(TemplateSyntaxError, msg): |
| 712 |
self.get_template( |
| 713 |
"{% load i18n %}{% blocktranslate count number=2 context %}" |
| 714 |
"{{ number }} super result{% plural %}{{ number }}" |
| 715 |
" super results{% endblocktranslate %}" |
| 716 |
) |
| 717 |
|
| 718 |
@override_settings(LOCALE_PATHS=[os.path.join(here, "other", "locale")]) |
| 719 |
def test_bad_placeholder_1(self): |
| 720 |
""" |
| 721 |
Error in translation file should not crash template rendering (#16516). |
| 722 |
(%(person)s is translated as %(personne)s in fr.po). |
| 723 |
""" |
| 724 |
with translation.override("fr"): |
| 725 |
t = Template( |
| 726 |
"{% load i18n %}{% blocktranslate %}My name is {{ person }}." |
| 727 |
"{% endblocktranslate %}" |
| 728 |
) |
| 729 |
rendered = t.render(Context({"person": "James"})) |
| 730 |
self.assertEqual(rendered, "My name is James.") |
| 731 |
|
| 732 |
@override_settings(LOCALE_PATHS=[os.path.join(here, "other", "locale")]) |
| 733 |
def test_bad_placeholder_2(self): |
| 734 |
""" |
| 735 |
Error in translation file should not crash template rendering (#18393). |
| 736 |
(%(person) misses a 's' in fr.po, causing the string formatting to fail) |
| 737 |
. |
| 738 |
""" |
| 739 |
with translation.override("fr"): |
| 740 |
t = Template( |
| 741 |
"{% load i18n %}{% blocktranslate %}My other name is {{ person }}." |
| 742 |
"{% endblocktranslate %}" |
| 743 |
) |
| 744 |
rendered = t.render(Context({"person": "James"})) |
| 745 |
self.assertEqual(rendered, "My other name is James.") |
| 746 |
|
| 747 |
|
| 748 |
class TranslationBlockTransnTagTests(TranslationBlockTranslateTagTests): |
| 749 |
tag_name = "blocktrans" |
| 750 |
|
| 751 |
|
| 752 |
class MultipleLocaleActivationBlockTranslateTests(MultipleLocaleActivationTestCase): |
| 753 |
tag_name = "blocktranslate" |
| 754 |
|
| 755 |
def get_template(self, template_string): |
| 756 |
return Template( |
| 757 |
template_string.replace( |
| 758 |
"{{% blocktranslate ", "{{% {}".format(self.tag_name) |
| 759 |
).replace( |
| 760 |
"{{% endblocktranslate %}}", "{{% end{} %}}".format(self.tag_name) |
| 761 |
) |
| 762 |
) |
| 763 |
|
| 764 |
def test_single_locale_activation(self): |
| 765 |
""" |
| 766 |
Simple baseline behavior with one locale for all the supported i18n |
| 767 |
constructs. |
| 768 |
""" |
| 769 |
with translation.override("fr"): |
| 770 |
self.assertEqual( |
| 771 |
self.get_template( |
| 772 |
"{% load i18n %}{% blocktranslate %}Yes{% endblocktranslate %}" |
| 773 |
).render(Context({})), |
| 774 |
"Oui", |
| 775 |
) |
| 776 |
|
| 777 |
def test_multiple_locale_btrans(self): |
| 778 |
with translation.override("de"): |
| 779 |
t = self.get_template( |
| 780 |
"{% load i18n %}{% blocktranslate %}No{% endblocktranslate %}" |
| 781 |
) |
| 782 |
with translation.override(self._old_language), translation.override("nl"): |
| 783 |
self.assertEqual(t.render(Context({})), "Nee") |
| 784 |
|
| 785 |
def test_multiple_locale_deactivate_btrans(self): |
| 786 |
with translation.override("de", deactivate=True): |
| 787 |
t = self.get_template( |
| 788 |
"{% load i18n %}{% blocktranslate %}No{% endblocktranslate %}" |
| 789 |
) |
| 790 |
with translation.override("nl"): |
| 791 |
self.assertEqual(t.render(Context({})), "Nee") |
| 792 |
|
| 793 |
def test_multiple_locale_direct_switch_btrans(self): |
| 794 |
with translation.override("de"): |
| 795 |
t = self.get_template( |
| 796 |
"{% load i18n %}{% blocktranslate %}No{% endblocktranslate %}" |
| 797 |
) |
| 798 |
with translation.override("nl"): |
| 799 |
self.assertEqual(t.render(Context({})), "Nee") |
| 800 |
|
| 801 |
|
| 802 |
class MultipleLocaleActivationBlockTransTests( |
| 803 |
MultipleLocaleActivationBlockTranslateTests |
| 804 |
): |
| 805 |
tag_name = "blocktrans" |
| 806 |
|
| 807 |
|
| 808 |
class MiscTests(SimpleTestCase): |
| 809 |
tag_name = "blocktranslate" |
| 810 |
|
| 811 |
def get_template(self, template_string): |
| 812 |
return Template( |
| 813 |
template_string.replace( |
| 814 |
"{{% blocktranslate ", "{{% {}".format(self.tag_name) |
| 815 |
).replace( |
| 816 |
"{{% endblocktranslate %}}", "{{% end{} %}}".format(self.tag_name) |
| 817 |
) |
| 818 |
) |
| 819 |
|
| 820 |
@override_settings(LOCALE_PATHS=extended_locale_paths) |
| 821 |
def test_percent_in_translatable_block(self): |
| 822 |
t_sing = self.get_template( |
| 823 |
"{% load i18n %}{% blocktranslate %}The result was {{ percent }}%" |
| 824 |
"{% endblocktranslate %}" |
| 825 |
) |
| 826 |
t_plur = self.get_template( |
| 827 |
"{% load i18n %}{% blocktranslate count num as number %}" |
| 828 |
"{{ percent }}% represents {{ num }} object{% plural %}" |
| 829 |
"{{ percent }}% represents {{ num }} objects{% endblocktranslate %}" |
| 830 |
) |
| 831 |
with translation.override("de"): |
| 832 |
self.assertEqual( |
| 833 |
t_sing.render(Context({"percent": 42})), "Das Ergebnis war 42%" |
| 834 |
) |
| 835 |
self.assertEqual( |
| 836 |
t_plur.render(Context({"percent": 42, "num": 1})), |
| 837 |
"42% stellt 1 Objekt dar", |
| 838 |
) |
| 839 |
self.assertEqual( |
| 840 |
t_plur.render(Context({"percent": 42, "num": 4})), |
| 841 |
"42% stellt 4 Objekte dar", |
| 842 |
) |
| 843 |
|
| 844 |
@override_settings(LOCALE_PATHS=extended_locale_paths) |
| 845 |
def test_percent_formatting_in_blocktranslate(self): |
| 846 |
""" |
| 847 |
Python's %-formatting is properly escaped in blocktranslate, singular, |
| 848 |
or plural. |
| 849 |
""" |
| 850 |
t_sing = self.get_template( |
| 851 |
"{% load i18n %}{% blocktranslate %}There are %(num_comments)s comments" |
| 852 |
"{% endblocktranslate %}" |
| 853 |
) |
| 854 |
t_plur = self.get_template( |
| 855 |
"{% load i18n %}{% blocktranslate count num as number %}" |
| 856 |
"%(percent)s% represents {{ num }} object{% plural %}" |
| 857 |
"%(percent)s% represents {{ num }} objects{% endblocktranslate %}" |
| 858 |
) |
| 859 |
with translation.override("de"): |
| 860 |
# Strings won't get translated as they don't match after escaping % |
| 861 |
self.assertEqual( |
| 862 |
t_sing.render(Context({"num_comments": 42})), |
| 863 |
"There are %(num_comments)s comments", |
| 864 |
) |
| 865 |
self.assertEqual( |
| 866 |
t_plur.render(Context({"percent": 42, "num": 1})), |
| 867 |
"%(percent)s% represents 1 object", |
| 868 |
) |
| 869 |
self.assertEqual( |
| 870 |
t_plur.render(Context({"percent": 42, "num": 4})), |
| 871 |
"%(percent)s% represents 4 objects", |
| 872 |
) |
| 873 |
|
| 874 |
|
| 875 |
class MiscBlockTranslationTests(MiscTests): |
| 876 |
tag_name = "blocktrans" |
| 877 |
|
| 878 |
|
| 879 |
class BlockTranslateNodeTests(SimpleTestCase): |
| 880 |
def test_repr(self): |
| 881 |
block_translate_node = BlockTranslateNode( |
| 882 |
extra_context={}, |
| 883 |
singular=[ |
| 884 |
Token(TokenType.TEXT, "content"), |
| 885 |
Token(TokenType.VAR, "variable"), |
| 886 |
], |
| 887 |
) |
| 888 |
self.assertEqual( |
| 889 |
repr(block_translate_node), |
| 890 |
"<BlockTranslateNode: extra_context={} " |
| 891 |
'singular=[<Text token: "content...">, <Var token: "variable...">] ' |
| 892 |
"plural=None>", |
| 893 |
) |