2019-06-22 00:41:01 +08:00
|
|
|
|
import inspect
|
2017-01-06 23:33:07 +08:00
|
|
|
|
import os
|
2019-06-22 00:41:01 +08:00
|
|
|
|
from functools import partial, wraps
|
2019-04-12 21:15:18 +08:00
|
|
|
|
|
|
|
|
|
from asgiref.local import Local
|
2017-01-06 23:33:07 +08:00
|
|
|
|
|
|
|
|
|
from django.template import Context, Template, TemplateSyntaxError
|
2021-06-09 00:00:00 +08:00
|
|
|
|
from django.template.base import Token, TokenType
|
|
|
|
|
from django.templatetags.i18n import BlockTranslateNode
|
2017-01-06 23:33:07 +08:00
|
|
|
|
from django.test import SimpleTestCase, override_settings
|
|
|
|
|
from django.utils import translation
|
|
|
|
|
from django.utils.safestring import mark_safe
|
|
|
|
|
from django.utils.translation import trans_real
|
|
|
|
|
|
2019-06-22 00:41:01 +08:00
|
|
|
|
from ...utils import setup as base_setup
|
2019-03-04 02:33:48 +08:00
|
|
|
|
from .base import MultipleLocaleActivationTestCase, extended_locale_paths, here
|
2017-01-06 23:33:07 +08:00
|
|
|
|
|
|
|
|
|
|
2019-06-22 00:41:01 +08:00
|
|
|
|
def setup(templates, *args, **kwargs):
|
2019-09-23 23:31:21 +08:00
|
|
|
|
blocktranslate_setup = base_setup(templates, *args, **kwargs)
|
|
|
|
|
blocktrans_setup = base_setup(
|
|
|
|
|
{
|
|
|
|
|
name: template.replace("{% blocktranslate ", "{% blocktrans ").replace(
|
|
|
|
|
"{% endblocktranslate %}", "{% endblocktrans %}"
|
2019-06-22 00:41:01 +08:00
|
|
|
|
)
|
|
|
|
|
for name, template in templates.items()
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
tags = {
|
|
|
|
|
"blocktrans": blocktrans_setup,
|
|
|
|
|
"blocktranslate": blocktranslate_setup,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def decorator(func):
|
|
|
|
|
@wraps(func)
|
|
|
|
|
def inner(self, *args):
|
|
|
|
|
signature = inspect.signature(func)
|
|
|
|
|
for tag_name, setup_func in tags.items():
|
|
|
|
|
if "tag_name" in signature.parameters:
|
|
|
|
|
setup_func(partial(func, tag_name=tag_name))(self)
|
|
|
|
|
else:
|
|
|
|
|
setup_func(func)(self)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2019-06-22 00:41:01 +08:00
|
|
|
|
return inner
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2019-06-22 00:41:01 +08:00
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
2017-01-06 23:33:07 +08:00
|
|
|
|
class I18nBlockTransTagTests(SimpleTestCase):
|
|
|
|
|
libraries = {"i18n": "django.templatetags.i18n"}
|
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"i18n03": (
|
|
|
|
|
"{% load i18n %}{% blocktranslate %}{{ anton }}{% endblocktranslate %}"
|
2022-02-04 15:08:27 +08:00
|
|
|
|
)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
def test_i18n03(self):
|
|
|
|
|
"""simple translation of a variable"""
|
2017-02-07 16:17:38 +08:00
|
|
|
|
output = self.engine.render_to_string("i18n03", {"anton": "Å"})
|
2017-01-06 23:33:07 +08:00
|
|
|
|
self.assertEqual(output, "Å")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"i18n04": (
|
|
|
|
|
"{% load i18n %}{% blocktranslate with berta=anton|lower %}{{ berta }}"
|
|
|
|
|
"{% endblocktranslate %}"
|
2022-02-04 15:08:27 +08:00
|
|
|
|
)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
def test_i18n04(self):
|
|
|
|
|
"""simple translation of a variable and filter"""
|
2017-02-07 16:17:38 +08:00
|
|
|
|
output = self.engine.render_to_string("i18n04", {"anton": "Å"})
|
2017-01-06 23:33:07 +08:00
|
|
|
|
self.assertEqual(output, "å")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2017-01-06 23:33:07 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"legacyi18n04": (
|
|
|
|
|
"{% load i18n %}"
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% blocktranslate with anton|lower as berta %}{{ berta }}"
|
|
|
|
|
"{% endblocktranslate %}"
|
2022-02-04 15:08:27 +08:00
|
|
|
|
)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
def test_legacyi18n04(self):
|
|
|
|
|
"""simple translation of a variable and filter"""
|
2017-02-07 16:17:38 +08:00
|
|
|
|
output = self.engine.render_to_string("legacyi18n04", {"anton": "Å"})
|
2017-01-06 23:33:07 +08:00
|
|
|
|
self.assertEqual(output, "å")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"i18n05": (
|
|
|
|
|
"{% load i18n %}{% blocktranslate %}xxx{{ anton }}xxx"
|
|
|
|
|
"{% endblocktranslate %}"
|
2022-02-04 15:08:27 +08:00
|
|
|
|
)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
def test_i18n05(self):
|
|
|
|
|
"""simple translation of a string with interpolation"""
|
|
|
|
|
output = self.engine.render_to_string("i18n05", {"anton": "yyy"})
|
|
|
|
|
self.assertEqual(output, "xxxyyyxxx")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2017-01-06 23:33:07 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"i18n07": "{% load i18n %}"
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% blocktranslate count counter=number %}singular{% plural %}"
|
|
|
|
|
"{{ counter }} plural{% endblocktranslate %}"
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
def test_i18n07(self):
|
|
|
|
|
"""translation of singular form"""
|
|
|
|
|
output = self.engine.render_to_string("i18n07", {"number": 1})
|
|
|
|
|
self.assertEqual(output, "singular")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2017-01-06 23:33:07 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"legacyi18n07": "{% load i18n %}"
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% blocktranslate count number as counter %}singular{% plural %}"
|
|
|
|
|
"{{ counter }} plural{% endblocktranslate %}"
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
def test_legacyi18n07(self):
|
|
|
|
|
"""translation of singular form"""
|
|
|
|
|
output = self.engine.render_to_string("legacyi18n07", {"number": 1})
|
|
|
|
|
self.assertEqual(output, "singular")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2017-01-06 23:33:07 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"i18n08": "{% load i18n %}"
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% blocktranslate count number as counter %}singular{% plural %}"
|
|
|
|
|
"{{ counter }} plural{% endblocktranslate %}"
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
def test_i18n08(self):
|
|
|
|
|
"""translation of plural form"""
|
|
|
|
|
output = self.engine.render_to_string("i18n08", {"number": 2})
|
|
|
|
|
self.assertEqual(output, "2 plural")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2017-01-06 23:33:07 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"legacyi18n08": "{% load i18n %}"
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% blocktranslate count counter=number %}singular{% plural %}"
|
|
|
|
|
"{{ counter }} plural{% endblocktranslate %}"
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
def test_legacyi18n08(self):
|
|
|
|
|
"""translation of plural form"""
|
|
|
|
|
output = self.engine.render_to_string("legacyi18n08", {"number": 2})
|
|
|
|
|
self.assertEqual(output, "2 plural")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2017-01-06 23:33:07 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"i18n17": (
|
|
|
|
|
"{% load i18n %}"
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% blocktranslate with berta=anton|escape %}{{ berta }}"
|
|
|
|
|
"{% endblocktranslate %}"
|
2022-02-04 15:08:27 +08:00
|
|
|
|
)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
def test_i18n17(self):
|
|
|
|
|
"""
|
2019-06-22 00:41:01 +08:00
|
|
|
|
Escaping inside blocktranslate and translate works as if it was
|
|
|
|
|
directly in the template.
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"""
|
|
|
|
|
output = self.engine.render_to_string("i18n17", {"anton": "α & β"})
|
|
|
|
|
self.assertEqual(output, "α & β")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2017-01-06 23:33:07 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"i18n18": (
|
|
|
|
|
"{% load i18n %}"
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% blocktranslate with berta=anton|force_escape %}{{ berta }}"
|
|
|
|
|
"{% endblocktranslate %}"
|
2022-02-04 15:08:27 +08:00
|
|
|
|
)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
def test_i18n18(self):
|
|
|
|
|
output = self.engine.render_to_string("i18n18", {"anton": "α & β"})
|
|
|
|
|
self.assertEqual(output, "α & β")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"i18n19": (
|
|
|
|
|
"{% load i18n %}{% blocktranslate %}{{ andrew }}{% endblocktranslate %}"
|
2022-02-04 15:08:27 +08:00
|
|
|
|
)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
def test_i18n19(self):
|
|
|
|
|
output = self.engine.render_to_string("i18n19", {"andrew": "a & b"})
|
|
|
|
|
self.assertEqual(output, "a & b")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"i18n21": (
|
|
|
|
|
"{% load i18n %}{% blocktranslate %}{{ andrew }}{% endblocktranslate %}"
|
2022-02-04 15:08:27 +08:00
|
|
|
|
)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
def test_i18n21(self):
|
|
|
|
|
output = self.engine.render_to_string("i18n21", {"andrew": mark_safe("a & b")})
|
|
|
|
|
self.assertEqual(output, "a & b")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2017-01-06 23:33:07 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"legacyi18n17": (
|
|
|
|
|
"{% load i18n %}"
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% blocktranslate with anton|escape as berta %}{{ berta }}"
|
|
|
|
|
"{% endblocktranslate %}"
|
2022-02-04 15:08:27 +08:00
|
|
|
|
)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
def test_legacyi18n17(self):
|
|
|
|
|
output = self.engine.render_to_string("legacyi18n17", {"anton": "α & β"})
|
|
|
|
|
self.assertEqual(output, "α & β")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2017-01-06 23:33:07 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"legacyi18n18": "{% load i18n %}"
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% blocktranslate with anton|force_escape as berta %}"
|
|
|
|
|
"{{ berta }}{% endblocktranslate %}"
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
def test_legacyi18n18(self):
|
|
|
|
|
output = self.engine.render_to_string("legacyi18n18", {"anton": "α & β"})
|
|
|
|
|
self.assertEqual(output, "α & β")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2017-01-06 23:33:07 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"i18n26": "{% load i18n %}"
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% blocktranslate with extra_field=myextra_field count counter=number %}"
|
|
|
|
|
"singular {{ extra_field }}{% plural %}plural{% endblocktranslate %}"
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
def test_i18n26(self):
|
|
|
|
|
"""
|
|
|
|
|
translation of plural form with extra field in singular form (#13568)
|
|
|
|
|
"""
|
|
|
|
|
output = self.engine.render_to_string(
|
|
|
|
|
"i18n26", {"myextra_field": "test", "number": 1}
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(output, "singular test")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2017-01-06 23:33:07 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"legacyi18n26": (
|
|
|
|
|
"{% load i18n %}"
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% blocktranslate with myextra_field as extra_field "
|
|
|
|
|
"count number as counter %}singular {{ extra_field }}{% plural %}plural"
|
|
|
|
|
"{% endblocktranslate %}"
|
2022-02-04 15:08:27 +08:00
|
|
|
|
)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
def test_legacyi18n26(self):
|
|
|
|
|
output = self.engine.render_to_string(
|
|
|
|
|
"legacyi18n26", {"myextra_field": "test", "number": 1}
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(output, "singular test")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"i18n27": "{% load i18n %}{% blocktranslate count counter=number %}"
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"{{ counter }} result{% plural %}{{ counter }} results"
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% endblocktranslate %}"
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
def test_i18n27(self):
|
|
|
|
|
"""translation of singular form in Russian (#14126)"""
|
|
|
|
|
with translation.override("ru"):
|
|
|
|
|
output = self.engine.render_to_string("i18n27", {"number": 1})
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
output, "1 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"legacyi18n27": "{% load i18n %}"
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% blocktranslate count number as counter %}{{ counter }} result"
|
|
|
|
|
"{% plural %}{{ counter }} results{% endblocktranslate %}"
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
def test_legacyi18n27(self):
|
|
|
|
|
with translation.override("ru"):
|
|
|
|
|
output = self.engine.render_to_string("legacyi18n27", {"number": 1})
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
output, "1 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"i18n28": (
|
|
|
|
|
"{% load i18n %}"
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% blocktranslate with a=anton b=berta %}{{ a }} + {{ b }}"
|
|
|
|
|
"{% endblocktranslate %}"
|
2022-02-04 15:08:27 +08:00
|
|
|
|
)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
def test_i18n28(self):
|
|
|
|
|
"""simple translation of multiple variables"""
|
|
|
|
|
output = self.engine.render_to_string("i18n28", {"anton": "α", "berta": "β"})
|
|
|
|
|
self.assertEqual(output, "α + β")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2017-01-06 23:33:07 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"legacyi18n28": "{% load i18n %}"
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% blocktranslate with anton as a and berta as b %}"
|
|
|
|
|
"{{ a }} + {{ b }}{% endblocktranslate %}"
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
def test_legacyi18n28(self):
|
|
|
|
|
output = self.engine.render_to_string(
|
|
|
|
|
"legacyi18n28", {"anton": "α", "berta": "β"}
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(output, "α + β")
|
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
|
# blocktranslate handling of variables which are not in the context.
|
|
|
|
|
# this should work as if blocktranslate was not there (#19915)
|
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"i18n34": (
|
|
|
|
|
"{% load i18n %}{% blocktranslate %}{{ missing }}"
|
|
|
|
|
"{% endblocktranslate %}"
|
2022-02-04 15:08:27 +08:00
|
|
|
|
)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
def test_i18n34(self):
|
|
|
|
|
output = self.engine.render_to_string("i18n34")
|
|
|
|
|
if self.engine.string_if_invalid:
|
|
|
|
|
self.assertEqual(output, "INVALID")
|
|
|
|
|
else:
|
|
|
|
|
self.assertEqual(output, "")
|
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"i18n34_2": (
|
|
|
|
|
"{% load i18n %}{% blocktranslate with a='α' %}{{ missing }}"
|
|
|
|
|
"{% endblocktranslate %}"
|
2022-02-04 15:08:27 +08:00
|
|
|
|
)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
def test_i18n34_2(self):
|
|
|
|
|
output = self.engine.render_to_string("i18n34_2")
|
|
|
|
|
if self.engine.string_if_invalid:
|
|
|
|
|
self.assertEqual(output, "INVALID")
|
|
|
|
|
else:
|
|
|
|
|
self.assertEqual(output, "")
|
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"i18n34_3": (
|
|
|
|
|
"{% load i18n %}{% blocktranslate with a=anton %}{{ missing }}"
|
|
|
|
|
"{% endblocktranslate %}"
|
2022-02-04 15:08:27 +08:00
|
|
|
|
)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
def test_i18n34_3(self):
|
|
|
|
|
output = self.engine.render_to_string("i18n34_3", {"anton": "\xce\xb1"})
|
|
|
|
|
if self.engine.string_if_invalid:
|
|
|
|
|
self.assertEqual(output, "INVALID")
|
|
|
|
|
else:
|
|
|
|
|
self.assertEqual(output, "")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2017-01-06 23:33:07 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"i18n37": "{% load i18n %}"
|
2019-09-23 23:31:21 +08:00
|
|
|
|
'{% translate "Page not found" as page_not_found %}'
|
|
|
|
|
"{% blocktranslate %}Error: {{ page_not_found }}{% endblocktranslate %}"
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
def test_i18n37(self):
|
|
|
|
|
with translation.override("de"):
|
|
|
|
|
output = self.engine.render_to_string("i18n37")
|
|
|
|
|
self.assertEqual(output, "Error: Seite nicht gefunden")
|
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
|
# blocktranslate tag with asvar
|
2017-01-06 23:33:07 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"i18n39": (
|
|
|
|
|
"{% load i18n %}"
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% blocktranslate asvar page_not_found %}Page not found"
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"{% endblocktranslate %}>{{ page_not_found }}<"
|
2022-02-04 15:08:27 +08:00
|
|
|
|
)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2017-01-06 23:33:07 +08:00
|
|
|
|
)
|
|
|
|
|
def test_i18n39(self):
|
|
|
|
|
with translation.override("de"):
|
|
|
|
|
output = self.engine.render_to_string("i18n39")
|
|
|
|
|
self.assertEqual(output, ">Seite nicht gefunden<")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2017-01-06 23:33:07 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"i18n40": "{% load i18n %}"
|
2019-09-23 23:31:21 +08:00
|
|
|
|
'{% translate "Page not found" as pg_404 %}'
|
|
|
|
|
"{% blocktranslate with page_not_found=pg_404 asvar output %}"
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"Error: {{ page_not_found }}"
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% endblocktranslate %}"
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
def test_i18n40(self):
|
|
|
|
|
output = self.engine.render_to_string("i18n40")
|
|
|
|
|
self.assertEqual(output, "")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2017-01-06 23:33:07 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"i18n41": "{% load i18n %}"
|
2019-09-23 23:31:21 +08:00
|
|
|
|
'{% translate "Page not found" as pg_404 %}'
|
|
|
|
|
"{% blocktranslate with page_not_found=pg_404 asvar output %}"
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"Error: {{ page_not_found }}"
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% endblocktranslate %}"
|
2017-01-06 23:33:07 +08:00
|
|
|
|
">{{ output }}<"
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2017-01-06 23:33:07 +08:00
|
|
|
|
)
|
|
|
|
|
def test_i18n41(self):
|
|
|
|
|
with translation.override("de"):
|
|
|
|
|
output = self.engine.render_to_string("i18n41")
|
|
|
|
|
self.assertEqual(output, ">Error: Seite nicht gefunden<")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2022-07-14 14:11:17 +08:00
|
|
|
|
@setup(
|
|
|
|
|
{
|
|
|
|
|
"i18n_asvar_safestring": (
|
|
|
|
|
"{% load i18n %}"
|
|
|
|
|
"{% blocktranslate asvar the_title %}"
|
|
|
|
|
"{{title}}other text"
|
|
|
|
|
"{% endblocktranslate %}"
|
|
|
|
|
"{{ the_title }}"
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
def test_i18n_asvar_safestring(self):
|
|
|
|
|
context = {"title": "<Main Title>"}
|
|
|
|
|
output = self.engine.render_to_string("i18n_asvar_safestring", context=context)
|
|
|
|
|
self.assertEqual(output, "<Main Title>other text")
|
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"template": (
|
|
|
|
|
"{% load i18n %}{% blocktranslate asvar %}Yes{% endblocktranslate %}"
|
2022-02-04 15:08:27 +08:00
|
|
|
|
)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2019-06-22 00:41:01 +08:00
|
|
|
|
def test_blocktrans_syntax_error_missing_assignment(self, tag_name):
|
|
|
|
|
msg = "No argument provided to the '{}' tag for the asvar option.".format(
|
|
|
|
|
tag_name
|
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
|
|
|
|
self.engine.render_to_string("template")
|
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
|
@setup({"template": "{% load i18n %}{% blocktranslate %}%s{% endblocktranslate %}"})
|
2017-01-06 23:33:07 +08:00
|
|
|
|
def test_blocktrans_tag_using_a_string_that_looks_like_str_fmt(self):
|
|
|
|
|
output = self.engine.render_to_string("template")
|
|
|
|
|
self.assertEqual(output, "%s")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"template": (
|
|
|
|
|
"{% load i18n %}{% blocktranslate %}{% block b %} {% endblock %}"
|
|
|
|
|
"{% endblocktranslate %}"
|
2022-02-04 15:08:27 +08:00
|
|
|
|
)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2019-06-22 00:41:01 +08:00
|
|
|
|
def test_with_block(self, tag_name):
|
|
|
|
|
msg = "'{}' doesn't allow other block tags (seen 'block b') inside it".format(
|
|
|
|
|
tag_name
|
|
|
|
|
)
|
2018-03-21 15:20:04 +08:00
|
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
|
|
|
|
self.engine.render_to_string("template")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"template": (
|
|
|
|
|
"{% load i18n %}"
|
|
|
|
|
"{% blocktranslate %}{% for b in [1, 2, 3] %} {% endfor %}"
|
|
|
|
|
"{% endblocktranslate %}"
|
2022-02-04 03:24:19 +08:00
|
|
|
|
)
|
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2019-06-22 00:41:01 +08:00
|
|
|
|
def test_with_for(self, tag_name):
|
|
|
|
|
msg = (
|
|
|
|
|
f"'{tag_name}' doesn't allow other block tags (seen 'for b in [1, 2, 3]') "
|
|
|
|
|
f"inside it"
|
|
|
|
|
)
|
2018-03-21 15:20:04 +08:00
|
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
|
|
|
|
self.engine.render_to_string("template")
|
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"template": (
|
|
|
|
|
"{% load i18n %}{% blocktranslate with foo=bar with %}{{ foo }}"
|
|
|
|
|
"{% endblocktranslate %}"
|
2022-02-04 15:08:27 +08:00
|
|
|
|
)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2018-03-21 15:20:04 +08:00
|
|
|
|
def test_variable_twice(self):
|
|
|
|
|
with self.assertRaisesMessage(
|
|
|
|
|
TemplateSyntaxError, "The 'with' option was specified more than once"
|
|
|
|
|
):
|
|
|
|
|
self.engine.render_to_string("template", {"foo": "bar"})
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
|
@setup(
|
|
|
|
|
{"template": "{% load i18n %}{% blocktranslate with %}{% endblocktranslate %}"}
|
|
|
|
|
)
|
2019-06-22 00:41:01 +08:00
|
|
|
|
def test_no_args_with(self, tag_name):
|
|
|
|
|
msg = "\"with\" in '{}' tag needs at least one keyword argument.".format(
|
|
|
|
|
tag_name
|
|
|
|
|
)
|
2018-03-21 15:20:04 +08:00
|
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
|
|
|
|
self.engine.render_to_string("template")
|
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"template": (
|
|
|
|
|
"{% load i18n %}{% blocktranslate count a %}{% endblocktranslate %}"
|
2022-02-04 15:08:27 +08:00
|
|
|
|
)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
}
|
2019-09-23 23:31:21 +08:00
|
|
|
|
)
|
2019-06-22 00:41:01 +08:00
|
|
|
|
def test_count(self, tag_name):
|
|
|
|
|
msg = "\"count\" in '{}' tag expected exactly one keyword argument.".format(
|
|
|
|
|
tag_name
|
|
|
|
|
)
|
2018-03-21 15:20:04 +08:00
|
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
|
|
|
|
self.engine.render_to_string("template", {"a": [1, 2, 3]})
|
|
|
|
|
|
2020-09-27 03:03:13 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2020-09-27 03:03:13 +08:00
|
|
|
|
"template": (
|
|
|
|
|
"{% load i18n %}{% blocktranslate count counter=num %}{{ counter }}"
|
|
|
|
|
"{% plural %}{{ counter }}{% endblocktranslate %}"
|
2022-02-04 03:24:19 +08:00
|
|
|
|
)
|
|
|
|
|
}
|
2020-09-27 03:03:13 +08:00
|
|
|
|
)
|
|
|
|
|
def test_count_not_number(self, tag_name):
|
|
|
|
|
msg = "'counter' argument to '{}' tag must be a number.".format(tag_name)
|
|
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
|
|
|
|
self.engine.render_to_string("template", {"num": "1"})
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2018-03-21 15:20:04 +08:00
|
|
|
|
@setup(
|
2022-02-04 03:24:19 +08:00
|
|
|
|
{
|
2018-03-21 15:20:04 +08:00
|
|
|
|
"template": (
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% load i18n %}{% blocktranslate count count=var|length %}"
|
2018-03-21 15:20:04 +08:00
|
|
|
|
"There is {{ count }} object. {% block a %} {% endblock %}"
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% endblocktranslate %}"
|
2022-02-04 03:24:19 +08:00
|
|
|
|
)
|
|
|
|
|
}
|
2018-03-21 15:20:04 +08:00
|
|
|
|
)
|
2019-06-22 00:41:01 +08:00
|
|
|
|
def test_plural_bad_syntax(self, tag_name):
|
|
|
|
|
msg = "'{}' doesn't allow other block tags inside it".format(tag_name)
|
2018-03-21 15:20:04 +08:00
|
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
|
|
|
|
self.engine.render_to_string("template", {"var": [1, 2, 3]})
|
|
|
|
|
|
2017-01-06 23:33:07 +08:00
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
|
class TranslationBlockTranslateTagTests(SimpleTestCase):
|
|
|
|
|
tag_name = "blocktranslate"
|
2019-06-22 00:41:01 +08:00
|
|
|
|
|
|
|
|
|
def get_template(self, template_string):
|
|
|
|
|
return Template(
|
|
|
|
|
template_string.replace(
|
|
|
|
|
"{{% blocktranslate ", "{{% {}".format(self.tag_name)
|
|
|
|
|
).replace(
|
|
|
|
|
"{{% endblocktranslate %}}", "{{% end{} %}}".format(self.tag_name)
|
|
|
|
|
)
|
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
|
|
|
|
|
@override_settings(LOCALE_PATHS=extended_locale_paths)
|
|
|
|
|
def test_template_tags_pgettext(self):
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"""{% blocktranslate %} takes message contexts into account (#14806)."""
|
2019-04-12 21:15:18 +08:00
|
|
|
|
trans_real._active = Local()
|
2017-01-06 23:33:07 +08:00
|
|
|
|
trans_real._translations = {}
|
|
|
|
|
with translation.override("de"):
|
|
|
|
|
# Nonexistent context
|
2019-09-23 23:31:21 +08:00
|
|
|
|
t = self.get_template(
|
|
|
|
|
'{% load i18n %}{% blocktranslate context "nonexistent" %}May'
|
|
|
|
|
"{% endblocktranslate %}"
|
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
rendered = t.render(Context())
|
|
|
|
|
self.assertEqual(rendered, "May")
|
|
|
|
|
|
|
|
|
|
# Existing context... using a literal
|
2019-09-23 23:31:21 +08:00
|
|
|
|
t = self.get_template(
|
|
|
|
|
"{% load i18n %}"
|
|
|
|
|
'{% blocktranslate context "month name" %}May{% endblocktranslate %}'
|
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
rendered = t.render(Context())
|
|
|
|
|
self.assertEqual(rendered, "Mai")
|
2019-09-23 23:31:21 +08:00
|
|
|
|
t = self.get_template(
|
|
|
|
|
"{% load i18n %}"
|
|
|
|
|
'{% blocktranslate context "verb" %}May{% endblocktranslate %}'
|
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
rendered = t.render(Context())
|
|
|
|
|
self.assertEqual(rendered, "Kann")
|
|
|
|
|
|
|
|
|
|
# Using a variable
|
2019-09-23 23:31:21 +08:00
|
|
|
|
t = self.get_template(
|
|
|
|
|
"{% load i18n %}{% blocktranslate context message_context %}"
|
|
|
|
|
"May{% endblocktranslate %}"
|
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
rendered = t.render(Context({"message_context": "month name"}))
|
|
|
|
|
self.assertEqual(rendered, "Mai")
|
2019-09-23 23:31:21 +08:00
|
|
|
|
t = self.get_template(
|
|
|
|
|
"{% load i18n %}{% blocktranslate context message_context %}"
|
|
|
|
|
"May{% endblocktranslate %}"
|
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
rendered = t.render(Context({"message_context": "verb"}))
|
|
|
|
|
self.assertEqual(rendered, "Kann")
|
|
|
|
|
|
|
|
|
|
# Using a filter
|
2019-06-22 00:41:01 +08:00
|
|
|
|
t = self.get_template(
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% load i18n %}"
|
|
|
|
|
"{% blocktranslate context message_context|lower %}May"
|
|
|
|
|
"{% endblocktranslate %}"
|
2019-06-22 00:41:01 +08:00
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
rendered = t.render(Context({"message_context": "MONTH NAME"}))
|
|
|
|
|
self.assertEqual(rendered, "Mai")
|
2019-06-22 00:41:01 +08:00
|
|
|
|
t = self.get_template(
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% load i18n %}"
|
|
|
|
|
"{% blocktranslate context message_context|lower %}May"
|
|
|
|
|
"{% endblocktranslate %}"
|
2019-06-22 00:41:01 +08:00
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
rendered = t.render(Context({"message_context": "VERB"}))
|
|
|
|
|
self.assertEqual(rendered, "Kann")
|
|
|
|
|
|
|
|
|
|
# Using 'count'
|
2019-06-22 00:41:01 +08:00
|
|
|
|
t = self.get_template(
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% load i18n %}"
|
|
|
|
|
'{% blocktranslate count number=1 context "super search" %}{{ number }}'
|
|
|
|
|
" super result{% plural %}{{ number }} super results"
|
|
|
|
|
"{% endblocktranslate %}"
|
2017-01-06 23:33:07 +08:00
|
|
|
|
)
|
|
|
|
|
rendered = t.render(Context())
|
|
|
|
|
self.assertEqual(rendered, "1 Super-Ergebnis")
|
2019-06-22 00:41:01 +08:00
|
|
|
|
t = self.get_template(
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% load i18n %}"
|
|
|
|
|
'{% blocktranslate count number=2 context "super search" %}{{ number }}'
|
|
|
|
|
" super result{% plural %}{{ number }} super results"
|
|
|
|
|
"{% endblocktranslate %}"
|
2017-01-06 23:33:07 +08:00
|
|
|
|
)
|
|
|
|
|
rendered = t.render(Context())
|
|
|
|
|
self.assertEqual(rendered, "2 Super-Ergebnisse")
|
2019-06-22 00:41:01 +08:00
|
|
|
|
t = self.get_template(
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% load i18n %}"
|
|
|
|
|
'{% blocktranslate context "other super search" count number=1 %}'
|
|
|
|
|
"{{ number }} super result{% plural %}{{ number }} super results"
|
|
|
|
|
"{% endblocktranslate %}"
|
2017-01-06 23:33:07 +08:00
|
|
|
|
)
|
|
|
|
|
rendered = t.render(Context())
|
|
|
|
|
self.assertEqual(rendered, "1 anderen Super-Ergebnis")
|
2019-06-22 00:41:01 +08:00
|
|
|
|
t = self.get_template(
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% load i18n %}"
|
|
|
|
|
'{% blocktranslate context "other super search" count number=2 %}'
|
|
|
|
|
"{{ number }} super result{% plural %}{{ number }} super results"
|
|
|
|
|
"{% endblocktranslate %}"
|
2017-01-06 23:33:07 +08:00
|
|
|
|
)
|
|
|
|
|
rendered = t.render(Context())
|
|
|
|
|
self.assertEqual(rendered, "2 andere Super-Ergebnisse")
|
|
|
|
|
|
|
|
|
|
# Using 'with'
|
2019-06-22 00:41:01 +08:00
|
|
|
|
t = self.get_template(
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% load i18n %}"
|
|
|
|
|
'{% blocktranslate with num_comments=5 context "comment count" %}'
|
|
|
|
|
"There are {{ num_comments }} comments{% endblocktranslate %}"
|
2017-01-06 23:33:07 +08:00
|
|
|
|
)
|
|
|
|
|
rendered = t.render(Context())
|
|
|
|
|
self.assertEqual(rendered, "Es gibt 5 Kommentare")
|
2019-06-22 00:41:01 +08:00
|
|
|
|
t = self.get_template(
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% load i18n %}"
|
|
|
|
|
'{% blocktranslate with num_comments=5 context "other comment count" %}'
|
|
|
|
|
"There are {{ num_comments }} comments{% endblocktranslate %}"
|
2017-01-06 23:33:07 +08:00
|
|
|
|
)
|
|
|
|
|
rendered = t.render(Context())
|
|
|
|
|
self.assertEqual(rendered, "Andere: Es gibt 5 Kommentare")
|
|
|
|
|
|
|
|
|
|
# Using trimmed
|
2019-06-22 00:41:01 +08:00
|
|
|
|
t = self.get_template(
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% load i18n %}{% blocktranslate trimmed %}\n\nThere\n\t are 5 "
|
|
|
|
|
"\n\n comments\n{% endblocktranslate %}"
|
2017-01-06 23:33:07 +08:00
|
|
|
|
)
|
|
|
|
|
rendered = t.render(Context())
|
|
|
|
|
self.assertEqual(rendered, "There are 5 comments")
|
2019-06-22 00:41:01 +08:00
|
|
|
|
t = self.get_template(
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% load i18n %}"
|
|
|
|
|
'{% blocktranslate with num_comments=5 context "comment count" trimmed '
|
|
|
|
|
"%}\n\n"
|
|
|
|
|
"There are \t\n \t {{ num_comments }} comments\n\n"
|
|
|
|
|
"{% endblocktranslate %}"
|
2017-01-06 23:33:07 +08:00
|
|
|
|
)
|
|
|
|
|
rendered = t.render(Context())
|
|
|
|
|
self.assertEqual(rendered, "Es gibt 5 Kommentare")
|
2019-06-22 00:41:01 +08:00
|
|
|
|
t = self.get_template(
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% load i18n %}"
|
|
|
|
|
'{% blocktranslate context "other super search" count number=2 trimmed '
|
|
|
|
|
"%}\n{{ number }} super \n result{% plural %}{{ number }} super results"
|
|
|
|
|
"{% endblocktranslate %}"
|
2017-01-06 23:33:07 +08:00
|
|
|
|
)
|
|
|
|
|
rendered = t.render(Context())
|
|
|
|
|
self.assertEqual(rendered, "2 andere Super-Ergebnisse")
|
|
|
|
|
|
|
|
|
|
# Misuses
|
2019-09-23 23:31:21 +08:00
|
|
|
|
msg = "Unknown argument for 'blocktranslate' tag: %r."
|
2017-05-29 03:37:21 +08:00
|
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, msg % 'month="May"'):
|
2019-06-22 00:41:01 +08:00
|
|
|
|
self.get_template(
|
2019-09-23 23:31:21 +08:00
|
|
|
|
'{% load i18n %}{% blocktranslate context with month="May" %}'
|
|
|
|
|
"{{ month }}{% endblocktranslate %}"
|
2019-06-22 00:41:01 +08:00
|
|
|
|
)
|
2019-09-23 23:31:21 +08:00
|
|
|
|
msg = (
|
|
|
|
|
'"context" in %r tag expected exactly one argument.' % "blocktranslate"
|
2022-02-04 03:24:19 +08:00
|
|
|
|
)
|
2017-05-29 03:37:21 +08:00
|
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
2019-09-23 23:31:21 +08:00
|
|
|
|
self.get_template(
|
|
|
|
|
"{% load i18n %}{% blocktranslate context %}{% endblocktranslate %}"
|
|
|
|
|
)
|
2017-05-29 03:37:21 +08:00
|
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
2019-06-22 00:41:01 +08:00
|
|
|
|
self.get_template(
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% load i18n %}{% blocktranslate count number=2 context %}"
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"{{ number }} super result{% plural %}{{ number }}"
|
2019-09-23 23:31:21 +08:00
|
|
|
|
" super results{% endblocktranslate %}"
|
2017-01-06 23:33:07 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@override_settings(LOCALE_PATHS=[os.path.join(here, "other", "locale")])
|
|
|
|
|
def test_bad_placeholder_1(self):
|
|
|
|
|
"""
|
|
|
|
|
Error in translation file should not crash template rendering (#16516).
|
|
|
|
|
(%(person)s is translated as %(personne)s in fr.po).
|
|
|
|
|
"""
|
|
|
|
|
with translation.override("fr"):
|
2019-09-23 23:31:21 +08:00
|
|
|
|
t = Template(
|
|
|
|
|
"{% load i18n %}{% blocktranslate %}My name is {{ person }}."
|
|
|
|
|
"{% endblocktranslate %}"
|
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
rendered = t.render(Context({"person": "James"}))
|
|
|
|
|
self.assertEqual(rendered, "My name is James.")
|
|
|
|
|
|
|
|
|
|
@override_settings(LOCALE_PATHS=[os.path.join(here, "other", "locale")])
|
|
|
|
|
def test_bad_placeholder_2(self):
|
|
|
|
|
"""
|
|
|
|
|
Error in translation file should not crash template rendering (#18393).
|
|
|
|
|
(%(person) misses a 's' in fr.po, causing the string formatting to fail)
|
|
|
|
|
.
|
|
|
|
|
"""
|
|
|
|
|
with translation.override("fr"):
|
2019-09-23 23:31:21 +08:00
|
|
|
|
t = Template(
|
|
|
|
|
"{% load i18n %}{% blocktranslate %}My other name is {{ person }}."
|
|
|
|
|
"{% endblocktranslate %}"
|
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
rendered = t.render(Context({"person": "James"}))
|
|
|
|
|
self.assertEqual(rendered, "My other name is James.")
|
|
|
|
|
|
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
|
class TranslationBlockTransnTagTests(TranslationBlockTranslateTagTests):
|
|
|
|
|
tag_name = "blocktrans"
|
2019-06-22 00:41:01 +08:00
|
|
|
|
|
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
|
class MultipleLocaleActivationBlockTranslateTests(MultipleLocaleActivationTestCase):
|
|
|
|
|
tag_name = "blocktranslate"
|
2019-06-22 00:41:01 +08:00
|
|
|
|
|
|
|
|
|
def get_template(self, template_string):
|
|
|
|
|
return Template(
|
|
|
|
|
template_string.replace(
|
|
|
|
|
"{{% blocktranslate ", "{{% {}".format(self.tag_name)
|
|
|
|
|
).replace(
|
|
|
|
|
"{{% endblocktranslate %}}", "{{% end{} %}}".format(self.tag_name)
|
|
|
|
|
)
|
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
|
|
|
|
|
def test_single_locale_activation(self):
|
|
|
|
|
"""
|
|
|
|
|
Simple baseline behavior with one locale for all the supported i18n
|
|
|
|
|
constructs.
|
|
|
|
|
"""
|
|
|
|
|
with translation.override("fr"):
|
|
|
|
|
self.assertEqual(
|
2019-09-23 23:31:21 +08:00
|
|
|
|
self.get_template(
|
|
|
|
|
"{% load i18n %}{% blocktranslate %}Yes{% endblocktranslate %}"
|
|
|
|
|
).render(Context({})),
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"Oui",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_multiple_locale_btrans(self):
|
|
|
|
|
with translation.override("de"):
|
2019-09-23 23:31:21 +08:00
|
|
|
|
t = self.get_template(
|
|
|
|
|
"{% load i18n %}{% blocktranslate %}No{% endblocktranslate %}"
|
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
with translation.override(self._old_language), translation.override("nl"):
|
|
|
|
|
self.assertEqual(t.render(Context({})), "Nee")
|
|
|
|
|
|
|
|
|
|
def test_multiple_locale_deactivate_btrans(self):
|
|
|
|
|
with translation.override("de", deactivate=True):
|
2019-09-23 23:31:21 +08:00
|
|
|
|
t = self.get_template(
|
|
|
|
|
"{% load i18n %}{% blocktranslate %}No{% endblocktranslate %}"
|
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
with translation.override("nl"):
|
|
|
|
|
self.assertEqual(t.render(Context({})), "Nee")
|
|
|
|
|
|
|
|
|
|
def test_multiple_locale_direct_switch_btrans(self):
|
|
|
|
|
with translation.override("de"):
|
2019-09-23 23:31:21 +08:00
|
|
|
|
t = self.get_template(
|
|
|
|
|
"{% load i18n %}{% blocktranslate %}No{% endblocktranslate %}"
|
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
with translation.override("nl"):
|
|
|
|
|
self.assertEqual(t.render(Context({})), "Nee")
|
|
|
|
|
|
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
|
class MultipleLocaleActivationBlockTransTests(
|
|
|
|
|
MultipleLocaleActivationBlockTranslateTests
|
|
|
|
|
):
|
|
|
|
|
tag_name = "blocktrans"
|
2019-06-22 00:41:01 +08:00
|
|
|
|
|
|
|
|
|
|
2017-01-06 23:33:07 +08:00
|
|
|
|
class MiscTests(SimpleTestCase):
|
2019-06-22 00:41:01 +08:00
|
|
|
|
tag_name = "blocktranslate"
|
|
|
|
|
|
|
|
|
|
def get_template(self, template_string):
|
|
|
|
|
return Template(
|
|
|
|
|
template_string.replace(
|
|
|
|
|
"{{% blocktranslate ", "{{% {}".format(self.tag_name)
|
|
|
|
|
).replace(
|
|
|
|
|
"{{% endblocktranslate %}}", "{{% end{} %}}".format(self.tag_name)
|
|
|
|
|
)
|
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
|
|
|
|
|
@override_settings(LOCALE_PATHS=extended_locale_paths)
|
|
|
|
|
def test_percent_in_translatable_block(self):
|
2019-09-23 23:31:21 +08:00
|
|
|
|
t_sing = self.get_template(
|
|
|
|
|
"{% load i18n %}{% blocktranslate %}The result was {{ percent }}%"
|
|
|
|
|
"{% endblocktranslate %}"
|
|
|
|
|
)
|
2019-06-22 00:41:01 +08:00
|
|
|
|
t_plur = self.get_template(
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% load i18n %}{% blocktranslate count num as number %}"
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"{{ percent }}% represents {{ num }} object{% plural %}"
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{{ percent }}% represents {{ num }} objects{% endblocktranslate %}"
|
2017-01-06 23:33:07 +08:00
|
|
|
|
)
|
|
|
|
|
with translation.override("de"):
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
t_sing.render(Context({"percent": 42})), "Das Ergebnis war 42%"
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
t_plur.render(Context({"percent": 42, "num": 1})),
|
|
|
|
|
"42% stellt 1 Objekt dar",
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
t_plur.render(Context({"percent": 42, "num": 4})),
|
|
|
|
|
"42% stellt 4 Objekte dar",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@override_settings(LOCALE_PATHS=extended_locale_paths)
|
2019-06-22 00:41:01 +08:00
|
|
|
|
def test_percent_formatting_in_blocktranslate(self):
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"""
|
2019-06-22 00:41:01 +08:00
|
|
|
|
Python's %-formatting is properly escaped in blocktranslate, singular,
|
|
|
|
|
or plural.
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"""
|
2019-06-22 00:41:01 +08:00
|
|
|
|
t_sing = self.get_template(
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% load i18n %}{% blocktranslate %}There are %(num_comments)s comments"
|
|
|
|
|
"{% endblocktranslate %}"
|
2019-06-22 00:41:01 +08:00
|
|
|
|
)
|
|
|
|
|
t_plur = self.get_template(
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"{% load i18n %}{% blocktranslate count num as number %}"
|
2017-01-06 23:33:07 +08:00
|
|
|
|
"%(percent)s% represents {{ num }} object{% plural %}"
|
2019-09-23 23:31:21 +08:00
|
|
|
|
"%(percent)s% represents {{ num }} objects{% endblocktranslate %}"
|
2017-01-06 23:33:07 +08:00
|
|
|
|
)
|
|
|
|
|
with translation.override("de"):
|
|
|
|
|
# Strings won't get translated as they don't match after escaping %
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
t_sing.render(Context({"num_comments": 42})),
|
|
|
|
|
"There are %(num_comments)s comments",
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
t_plur.render(Context({"percent": 42, "num": 1})),
|
|
|
|
|
"%(percent)s% represents 1 object",
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
t_plur.render(Context({"percent": 42, "num": 4})),
|
|
|
|
|
"%(percent)s% represents 4 objects",
|
|
|
|
|
)
|
2019-06-22 00:41:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MiscBlockTranslationTests(MiscTests):
|
|
|
|
|
tag_name = "blocktrans"
|
2021-06-09 00:00:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BlockTranslateNodeTests(SimpleTestCase):
|
|
|
|
|
def test_repr(self):
|
|
|
|
|
block_translate_node = BlockTranslateNode(
|
|
|
|
|
extra_context={},
|
|
|
|
|
singular=[
|
|
|
|
|
Token(TokenType.TEXT, "content"),
|
|
|
|
|
Token(TokenType.VAR, "variable"),
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
repr(block_translate_node),
|
|
|
|
|
"<BlockTranslateNode: extra_context={} "
|
|
|
|
|
'singular=[<Text token: "content...">, <Var token: "variable...">] '
|
|
|
|
|
"plural=None>",
|
|
|
|
|
)
|