2019-06-22 00:41:01 +08:00
|
|
|
import inspect
|
|
|
|
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
|
2017-02-04 18:43:14 +08:00
|
|
|
from django.templatetags.l10n import LocalizeNode
|
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
|
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
|
|
|
translate_setup = base_setup(templates, *args, **kwargs)
|
|
|
|
trans_setup = base_setup({
|
|
|
|
name: template.replace('{% translate ', '{% trans ')
|
2019-06-22 00:41:01 +08:00
|
|
|
for name, template in templates.items()
|
|
|
|
})
|
|
|
|
|
|
|
|
tags = {
|
|
|
|
'trans': trans_setup,
|
|
|
|
'translate': translate_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)
|
|
|
|
return inner
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
2017-01-06 23:33:07 +08:00
|
|
|
class I18nTransTagTests(SimpleTestCase):
|
|
|
|
libraries = {'i18n': 'django.templatetags.i18n'}
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
@setup({'i18n01': '{% load i18n %}{% translate \'xxxyyyxxx\' %}'})
|
2017-01-06 23:33:07 +08:00
|
|
|
def test_i18n01(self):
|
|
|
|
"""simple translation of a string delimited by '."""
|
|
|
|
output = self.engine.render_to_string('i18n01')
|
|
|
|
self.assertEqual(output, 'xxxyyyxxx')
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
@setup({'i18n02': '{% load i18n %}{% translate "xxxyyyxxx" %}'})
|
2017-01-06 23:33:07 +08:00
|
|
|
def test_i18n02(self):
|
|
|
|
"""simple translation of a string delimited by "."""
|
|
|
|
output = self.engine.render_to_string('i18n02')
|
|
|
|
self.assertEqual(output, 'xxxyyyxxx')
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
@setup({'i18n06': '{% load i18n %}{% translate "Page not found" %}'})
|
2017-01-06 23:33:07 +08:00
|
|
|
def test_i18n06(self):
|
|
|
|
"""simple translation of a string to German"""
|
|
|
|
with translation.override('de'):
|
|
|
|
output = self.engine.render_to_string('i18n06')
|
|
|
|
self.assertEqual(output, 'Seite nicht gefunden')
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
@setup({'i18n09': '{% load i18n %}{% translate "Page not found" noop %}'})
|
2017-01-06 23:33:07 +08:00
|
|
|
def test_i18n09(self):
|
|
|
|
"""simple non-translation (only marking) of a string to German"""
|
|
|
|
with translation.override('de'):
|
|
|
|
output = self.engine.render_to_string('i18n09')
|
|
|
|
self.assertEqual(output, 'Page not found')
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
@setup({'i18n20': '{% load i18n %}{% translate andrew %}'})
|
2017-01-06 23:33:07 +08:00
|
|
|
def test_i18n20(self):
|
|
|
|
output = self.engine.render_to_string('i18n20', {'andrew': 'a & b'})
|
|
|
|
self.assertEqual(output, 'a & b')
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
@setup({'i18n22': '{% load i18n %}{% translate andrew %}'})
|
2017-01-06 23:33:07 +08:00
|
|
|
def test_i18n22(self):
|
|
|
|
output = self.engine.render_to_string('i18n22', {'andrew': mark_safe('a & b')})
|
|
|
|
self.assertEqual(output, 'a & b')
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
@setup({'i18n23': '{% load i18n %}{% translate "Page not found"|capfirst|slice:"6:" %}'})
|
2017-01-06 23:33:07 +08:00
|
|
|
def test_i18n23(self):
|
2019-09-23 23:31:21 +08:00
|
|
|
"""Using filters with the {% translate %} tag (#5972)."""
|
2017-01-06 23:33:07 +08:00
|
|
|
with translation.override('de'):
|
|
|
|
output = self.engine.render_to_string('i18n23')
|
|
|
|
self.assertEqual(output, 'nicht gefunden')
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
@setup({'i18n24': '{% load i18n %}{% translate \'Page not found\'|upper %}'})
|
2017-01-06 23:33:07 +08:00
|
|
|
def test_i18n24(self):
|
|
|
|
with translation.override('de'):
|
|
|
|
output = self.engine.render_to_string('i18n24')
|
|
|
|
self.assertEqual(output, 'SEITE NICHT GEFUNDEN')
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
@setup({'i18n25': '{% load i18n %}{% translate somevar|upper %}'})
|
2017-01-06 23:33:07 +08:00
|
|
|
def test_i18n25(self):
|
|
|
|
with translation.override('de'):
|
|
|
|
output = self.engine.render_to_string('i18n25', {'somevar': 'Page not found'})
|
|
|
|
self.assertEqual(output, 'SEITE NICHT GEFUNDEN')
|
|
|
|
|
|
|
|
# trans tag with as var
|
2019-09-23 23:31:21 +08:00
|
|
|
@setup({'i18n35': '{% load i18n %}{% translate "Page not found" as page_not_found %}{{ page_not_found }}'})
|
2017-01-06 23:33:07 +08:00
|
|
|
def test_i18n35(self):
|
|
|
|
with translation.override('de'):
|
|
|
|
output = self.engine.render_to_string('i18n35')
|
|
|
|
self.assertEqual(output, 'Seite nicht gefunden')
|
|
|
|
|
|
|
|
@setup({'i18n36': '{% load i18n %}'
|
2019-09-23 23:31:21 +08:00
|
|
|
'{% translate "Page not found" noop as page_not_found %}{{ page_not_found }}'})
|
2017-01-06 23:33:07 +08:00
|
|
|
def test_i18n36(self):
|
|
|
|
with translation.override('de'):
|
|
|
|
output = self.engine.render_to_string('i18n36')
|
|
|
|
self.assertEqual(output, 'Page not found')
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
@setup({'template': '{% load i18n %}{% translate %}A}'})
|
2019-06-22 00:41:01 +08:00
|
|
|
def test_syntax_error_no_arguments(self, tag_name):
|
|
|
|
msg = "'{}' takes at least one argument".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 %}{% translate "Yes" badoption %}'})
|
2019-06-22 00:41:01 +08:00
|
|
|
def test_syntax_error_bad_option(self, tag_name):
|
|
|
|
msg = "Unknown argument for '{}' tag: 'badoption'".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 %}{% translate "Yes" as %}'})
|
2019-06-22 00:41:01 +08:00
|
|
|
def test_syntax_error_missing_assignment(self, tag_name):
|
|
|
|
msg = "No argument provided to the '{}' tag for the as 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 %}{% translate "Yes" as var context %}'})
|
2019-06-22 00:41:01 +08:00
|
|
|
def test_syntax_error_missing_context(self, tag_name):
|
|
|
|
msg = "No argument provided to the '{}' tag for the context 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 %}{% translate "Yes" context as var %}'})
|
2019-06-22 00:41:01 +08:00
|
|
|
def test_syntax_error_context_as(self, tag_name):
|
|
|
|
msg = "Invalid argument 'as' provided to the '{}' tag for the context 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 %}{% translate "Yes" context noop %}'})
|
2019-06-22 00:41:01 +08:00
|
|
|
def test_syntax_error_context_noop(self, tag_name):
|
|
|
|
msg = "Invalid argument 'noop' provided to the '{}' tag for the context 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 %}{% translate "Yes" noop noop %}'})
|
2017-01-06 23:33:07 +08:00
|
|
|
def test_syntax_error_duplicate_option(self):
|
|
|
|
msg = "The 'noop' option was specified more than once."
|
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
|
|
|
self.engine.render_to_string('template')
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
@setup({'template': '{% load i18n %}{% translate "%s" %}'})
|
2017-01-06 23:33:07 +08:00
|
|
|
def test_trans_tag_using_a_string_that_looks_like_str_fmt(self):
|
|
|
|
output = self.engine.render_to_string('template')
|
|
|
|
self.assertEqual(output, '%s')
|
|
|
|
|
|
|
|
|
|
|
|
class TranslationTransTagTests(SimpleTestCase):
|
2019-06-22 00:41:01 +08:00
|
|
|
tag_name = 'trans'
|
|
|
|
|
|
|
|
def get_template(self, template_string):
|
|
|
|
return Template(
|
|
|
|
template_string.replace(
|
2019-09-23 23:31:21 +08:00
|
|
|
'{{% translate ',
|
2019-06-22 00:41:01 +08:00
|
|
|
'{{% {}'.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
|
|
|
"""{% translate %} 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 %}{% translate "May" context "nonexistent" %}')
|
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 %}{% translate "May" context "month name" %}')
|
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 %}{% translate "May" context "verb" %}')
|
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 %}{% translate "May" context message_context %}')
|
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 %}{% translate "May" context message_context %}')
|
2017-01-06 23:33:07 +08:00
|
|
|
rendered = t.render(Context({'message_context': 'verb'}))
|
|
|
|
self.assertEqual(rendered, 'Kann')
|
|
|
|
|
|
|
|
# Using a filter
|
2019-09-23 23:31:21 +08:00
|
|
|
t = self.get_template('{% load i18n %}{% translate "May" context message_context|lower %}')
|
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 %}{% translate "May" context message_context|lower %}')
|
2017-01-06 23:33:07 +08:00
|
|
|
rendered = t.render(Context({'message_context': 'VERB'}))
|
|
|
|
self.assertEqual(rendered, 'Kann')
|
|
|
|
|
|
|
|
# Using 'as'
|
2019-09-23 23:31:21 +08:00
|
|
|
t = self.get_template('{% load i18n %}{% translate "May" context "month name" as var %}Value: {{ var }}')
|
2017-01-06 23:33:07 +08:00
|
|
|
rendered = t.render(Context())
|
|
|
|
self.assertEqual(rendered, 'Value: Mai')
|
2019-09-23 23:31:21 +08:00
|
|
|
t = self.get_template('{% load i18n %}{% translate "May" as var context "verb" %}Value: {{ var }}')
|
2017-01-06 23:33:07 +08:00
|
|
|
rendered = t.render(Context())
|
|
|
|
self.assertEqual(rendered, 'Value: Kann')
|
|
|
|
|
|
|
|
|
2019-06-22 00:41:01 +08:00
|
|
|
class TranslationTranslateTagTests(TranslationTransTagTests):
|
|
|
|
tag_name = 'translate'
|
|
|
|
|
|
|
|
|
2017-01-06 23:33:07 +08:00
|
|
|
class MultipleLocaleActivationTransTagTests(MultipleLocaleActivationTestCase):
|
2019-06-22 00:41:01 +08:00
|
|
|
tag_name = 'trans'
|
|
|
|
|
|
|
|
def get_template(self, template_string):
|
|
|
|
return Template(
|
|
|
|
template_string.replace(
|
2019-09-23 23:31:21 +08:00
|
|
|
'{{% translate ',
|
2019-06-22 00:41:01 +08:00
|
|
|
'{{% {}'.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'):
|
2019-06-22 00:41:01 +08:00
|
|
|
self.assertEqual(
|
2019-09-23 23:31:21 +08:00
|
|
|
self.get_template("{% load i18n %}{% translate 'Yes' %}").render(Context({})),
|
2019-06-22 00:41:01 +08:00
|
|
|
'Oui'
|
|
|
|
)
|
2017-01-06 23:33:07 +08:00
|
|
|
|
|
|
|
def test_multiple_locale_trans(self):
|
|
|
|
with translation.override('de'):
|
2019-09-23 23:31:21 +08:00
|
|
|
t = self.get_template("{% load i18n %}{% translate 'No' %}")
|
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_trans(self):
|
|
|
|
with translation.override('de', deactivate=True):
|
2019-09-23 23:31:21 +08:00
|
|
|
t = self.get_template("{% load i18n %}{% translate 'No' %}")
|
2017-01-06 23:33:07 +08:00
|
|
|
with translation.override('nl'):
|
|
|
|
self.assertEqual(t.render(Context({})), 'Nee')
|
|
|
|
|
|
|
|
def test_multiple_locale_direct_switch_trans(self):
|
|
|
|
with translation.override('de'):
|
2019-09-23 23:31:21 +08:00
|
|
|
t = self.get_template("{% load i18n %}{% translate 'No' %}")
|
2017-01-06 23:33:07 +08:00
|
|
|
with translation.override('nl'):
|
|
|
|
self.assertEqual(t.render(Context({})), 'Nee')
|
2017-02-04 18:43:14 +08:00
|
|
|
|
|
|
|
|
2019-06-22 00:41:01 +08:00
|
|
|
class MultipleLocaleActivationTranslateTagTests(MultipleLocaleActivationTransTagTests):
|
|
|
|
tag_name = 'translate'
|
|
|
|
|
|
|
|
|
2017-02-04 18:43:14 +08:00
|
|
|
class LocalizeNodeTests(SimpleTestCase):
|
|
|
|
def test_repr(self):
|
|
|
|
node = LocalizeNode(nodelist=[], use_l10n=True)
|
|
|
|
self.assertEqual(repr(node), '<LocalizeNode>')
|