summaryrefslogtreecommitdiff
path: root/tests/template_tests/syntax_tests/test_i18n.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/template_tests/syntax_tests/test_i18n.py')
-rw-r--r--tests/template_tests/syntax_tests/test_i18n.py399
1 files changed, 399 insertions, 0 deletions
diff --git a/tests/template_tests/syntax_tests/test_i18n.py b/tests/template_tests/syntax_tests/test_i18n.py
new file mode 100644
index 0000000000..9a2581fc88
--- /dev/null
+++ b/tests/template_tests/syntax_tests/test_i18n.py
@@ -0,0 +1,399 @@
+# coding: utf-8
+from __future__ import unicode_literals
+
+from django.conf import settings
+from django.test import TestCase
+from django.utils.safestring import mark_safe
+
+from .utils import render, setup
+
+
+class I18nTagTests(TestCase):
+
+ @setup({'i18n01': '{% load i18n %}{% trans \'xxxyyyxxx\' %}'})
+ def test_i18n01(self):
+ """
+ simple translation of a string delimited by '
+ """
+ output = render('i18n01')
+ self.assertEqual(output, 'xxxyyyxxx')
+
+ @setup({'i18n02': '{% load i18n %}{% trans "xxxyyyxxx" %}'})
+ def test_i18n02(self):
+ """
+ simple translation of a string delimited by "
+ """
+ output = render('i18n02')
+ self.assertEqual(output, 'xxxyyyxxx')
+
+ @setup({'i18n03': '{% load i18n %}{% blocktrans %}{{ anton }}{% endblocktrans %}'})
+ def test_i18n03(self):
+ """
+ simple translation of a variable
+ """
+ output = render('i18n03', {'anton': b'\xc3\x85'})
+ self.assertEqual(output, 'Å')
+
+ @setup({'i18n04': '{% load i18n %}{% blocktrans with berta=anton|lower %}{{ berta }}{% endblocktrans %}'})
+ def test_i18n04(self):
+ """
+ simple translation of a variable and filter
+ """
+ output = render('i18n04', {'anton': b'\xc3\x85'})
+ self.assertEqual(output, 'å')
+
+ @setup({'legacyi18n04': '{% load i18n %}'
+ '{% blocktrans with anton|lower as berta %}{{ berta }}{% endblocktrans %}'})
+ def test_legacyi18n04(self):
+ """
+ simple translation of a variable and filter
+ """
+ output = render('legacyi18n04', {'anton': b'\xc3\x85'})
+ self.assertEqual(output, 'å')
+
+ @setup({'i18n05': '{% load i18n %}{% blocktrans %}xxx{{ anton }}xxx{% endblocktrans %}'})
+ def test_i18n05(self):
+ """
+ simple translation of a string with interpolation
+ """
+ output = render('i18n05', {'anton': 'yyy'})
+ self.assertEqual(output, 'xxxyyyxxx')
+
+ @setup({'i18n06': '{% load i18n %}{% trans "Page not found" %}'})
+ def test_i18n06(self):
+ """
+ simple translation of a string to german
+ """
+ output = render('i18n06', {'LANGUAGE_CODE': 'de'})
+ self.assertEqual(output, 'Seite nicht gefunden')
+
+ @setup({'i18n07': '{% load i18n %}'
+ '{% blocktrans count counter=number %}singular{% plural %}'
+ '{{ counter }} plural{% endblocktrans %}'})
+ def test_i18n07(self):
+ """
+ translation of singular form
+ """
+ output = render('i18n07', {'number': 1})
+ self.assertEqual(output, 'singular')
+
+ @setup({'legacyi18n07': '{% load i18n %}'
+ '{% blocktrans count number as counter %}singular{% plural %}'
+ '{{ counter }} plural{% endblocktrans %}'})
+ def test_legacyi18n07(self):
+ """
+ translation of singular form
+ """
+ output = render('legacyi18n07', {'number': 1})
+ self.assertEqual(output, 'singular')
+
+ @setup({'i18n08': '{% load i18n %}'
+ '{% blocktrans count number as counter %}singular{% plural %}'
+ '{{ counter }} plural{% endblocktrans %}'})
+ def test_i18n08(self):
+ """
+ translation of plural form
+ """
+ output = render('i18n08', {'number': 2})
+ self.assertEqual(output, '2 plural')
+
+ @setup({'legacyi18n08': '{% load i18n %}'
+ '{% blocktrans count counter=number %}singular{% plural %}'
+ '{{ counter }} plural{% endblocktrans %}'})
+ def test_legacyi18n08(self):
+ """
+ translation of plural form
+ """
+ output = render('legacyi18n08', {'number': 2})
+ self.assertEqual(output, '2 plural')
+
+ @setup({'i18n09': '{% load i18n %}{% trans "Page not found" noop %}'})
+ def test_i18n09(self):
+ """
+ simple non-translation (only marking) of a string to german
+ """
+ output = render('i18n09', {'LANGUAGE_CODE': 'de'})
+ self.assertEqual(output, 'Page not found')
+
+ @setup({'i18n10': '{{ bool|yesno:_("yes,no,maybe") }}'})
+ def test_i18n10(self):
+ """
+ translation of a variable with a translated filter
+ """
+ output = render('i18n10', {'bool': True, 'LANGUAGE_CODE': 'de'})
+ self.assertEqual(output, 'Ja')
+
+ @setup({'i18n11': '{{ bool|yesno:"ja,nein" }}'})
+ def test_i18n11(self):
+ """
+ translation of a variable with a non-translated filter
+ """
+ output = render('i18n11', {'bool': True})
+ self.assertEqual(output, 'ja')
+
+ @setup({'i18n12': '{% load i18n %}'
+ '{% get_available_languages as langs %}{% for lang in langs %}'
+ '{% ifequal lang.0 "de" %}{{ lang.0 }}{% endifequal %}{% endfor %}'})
+ def test_i18n12(self):
+ """
+ usage of the get_available_languages tag
+ """
+ output = render('i18n12')
+ self.assertEqual(output, 'de')
+
+ @setup({'i18n13': '{{ _("Password") }}'})
+ def test_i18n13(self):
+ """
+ translation of constant strings
+ """
+ output = render('i18n13', {'LANGUAGE_CODE': 'de'})
+ self.assertEqual(output, 'Passwort')
+
+ @setup({'i18n14': '{% cycle "foo" _("Password") _(\'Password\') as c %} {% cycle c %} {% cycle c %}'})
+ def test_i18n14(self):
+ """
+ translation of constant strings
+ """
+ output = render('i18n14', {'LANGUAGE_CODE': 'de'})
+ self.assertEqual(output, 'foo Passwort Passwort')
+
+ @setup({'i18n15': '{{ absent|default:_("Password") }}'})
+ def test_i18n15(self):
+ """
+ translation of constant strings
+ """
+ output = render('i18n15', {'absent': '', 'LANGUAGE_CODE': 'de'})
+ self.assertEqual(output, 'Passwort')
+
+ @setup({'i18n16': '{{ _("<") }}'})
+ def test_i18n16(self):
+ """
+ translation of constant strings
+ """
+ output = render('i18n16', {'LANGUAGE_CODE': 'de'})
+ self.assertEqual(output, '<')
+
+ @setup({'i18n17': '{% load i18n %}'
+ '{% blocktrans with berta=anton|escape %}{{ berta }}{% endblocktrans %}'})
+ def test_i18n17(self):
+ """
+ Escaping inside blocktrans and trans works as if it was directly in the template.
+ """
+ output = render('i18n17', {'anton': 'α & β'})
+ self.assertEqual(output, 'α &amp; β')
+
+ @setup({'i18n18': '{% load i18n %}'
+ '{% blocktrans with berta=anton|force_escape %}{{ berta }}{% endblocktrans %}'})
+ def test_i18n18(self):
+ output = render('i18n18', {'anton': 'α & β'})
+ self.assertEqual(output, 'α &amp; β')
+
+ @setup({'i18n19': '{% load i18n %}{% blocktrans %}{{ andrew }}{% endblocktrans %}'})
+ def test_i18n19(self):
+ output = render('i18n19', {'andrew': 'a & b'})
+ self.assertEqual(output, 'a &amp; b')
+
+ @setup({'i18n20': '{% load i18n %}{% trans andrew %}'})
+ def test_i18n20(self):
+ output = render('i18n20', {'andrew': 'a & b'})
+ self.assertEqual(output, 'a &amp; b')
+
+ @setup({'i18n21': '{% load i18n %}{% blocktrans %}{{ andrew }}{% endblocktrans %}'})
+ def test_i18n21(self):
+ output = render('i18n21', {'andrew': mark_safe('a & b')})
+ self.assertEqual(output, 'a & b')
+
+ @setup({'i18n22': '{% load i18n %}{% trans andrew %}'})
+ def test_i18n22(self):
+ output = render('i18n22', {'andrew': mark_safe('a & b')})
+ self.assertEqual(output, 'a & b')
+
+ @setup({'legacyi18n17': '{% load i18n %}'
+ '{% blocktrans with anton|escape as berta %}{{ berta }}{% endblocktrans %}'})
+ def test_legacyi18n17(self):
+ output = render('legacyi18n17', {'anton': 'α & β'})
+ self.assertEqual(output, 'α &amp; β')
+
+ @setup({'legacyi18n18': '{% load i18n %}'
+ '{% blocktrans with anton|force_escape as berta %}'
+ '{{ berta }}{% endblocktrans %}'})
+ def test_legacyi18n18(self):
+ output = render('legacyi18n18', {'anton': 'α & β'})
+ self.assertEqual(output, 'α &amp; β')
+
+ @setup({'i18n23': '{% load i18n %}{% trans "Page not found"|capfirst|slice:"6:" %}'})
+ def test_i18n23(self):
+ """
+ #5972 - Use filters with the {% trans %} tag
+ """
+ output = render('i18n23', {'LANGUAGE_CODE': 'de'})
+ self.assertEqual(output, 'nicht gefunden')
+
+ @setup({'i18n24': '{% load i18n %}{% trans \'Page not found\'|upper %}'})
+ def test_i18n24(self):
+ output = render('i18n24', {'LANGUAGE_CODE': 'de'})
+ self.assertEqual(output, 'SEITE NICHT GEFUNDEN')
+
+ @setup({'i18n25': '{% load i18n %}{% trans somevar|upper %}'})
+ def test_i18n25(self):
+ output = render('i18n25', {'somevar': 'Page not found', 'LANGUAGE_CODE': 'de'})
+ self.assertEqual(output, 'SEITE NICHT GEFUNDEN')
+
+ @setup({'i18n26': '{% load i18n %}'
+ '{% blocktrans with extra_field=myextra_field count counter=number %}'
+ 'singular {{ extra_field }}{% plural %}plural{% endblocktrans %}'})
+ def test_i18n26(self):
+ """
+ translation of plural form with extra field in singular form (#13568)
+ """
+ output = render('i18n26', {'myextra_field': 'test', 'number': 1})
+ self.assertEqual(output, 'singular test')
+
+ @setup({'legacyi18n26': '{% load i18n %}'
+ '{% blocktrans with myextra_field as extra_field count number as counter %}'
+ 'singular {{ extra_field }}{% plural %}plural{% endblocktrans %}'})
+ def test_legacyi18n26(self):
+ output = render('legacyi18n26', {'myextra_field': 'test', 'number': 1})
+ self.assertEqual(output, 'singular test')
+
+ @setup({'i18n27': '{% load i18n %}{% blocktrans count counter=number %}'
+ '{{ counter }} result{% plural %}{{ counter }} results'
+ '{% endblocktrans %}'})
+ def test_i18n27(self):
+ """
+ translation of singular form in russian (#14126)
+ """
+ output = render('i18n27', {'number': 1, 'LANGUAGE_CODE': 'ru'})
+ self.assertEqual(output, '1 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442')
+
+ @setup({'legacyi18n27': '{% load i18n %}'
+ '{% blocktrans count number as counter %}{{ counter }} result'
+ '{% plural %}{{ counter }} results{% endblocktrans %}'})
+ def test_legacyi18n27(self):
+ output = render('legacyi18n27', {'number': 1, 'LANGUAGE_CODE': 'ru'})
+ self.assertEqual(output, '1 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442')
+
+ @setup({'i18n28': '{% load i18n %}'
+ '{% blocktrans with a=anton b=berta %}{{ a }} + {{ b }}{% endblocktrans %}'})
+ def test_i18n28(self):
+ """
+ simple translation of multiple variables
+ """
+ output = render('i18n28', {'anton': 'α', 'berta': 'β'})
+ self.assertEqual(output, 'α + β')
+
+ @setup({'legacyi18n28': '{% load i18n %}'
+ '{% blocktrans with anton as a and berta as b %}'
+ '{{ a }} + {{ b }}{% endblocktrans %}'})
+ def test_legacyi18n28(self):
+ output = render('legacyi18n28', {'anton': 'α', 'berta': 'β'})
+ self.assertEqual(output, 'α + β')
+
+ # retrieving language information
+ @setup({'i18n28_2': '{% load i18n %}'
+ '{% get_language_info for "de" as l %}'
+ '{{ l.code }}: {{ l.name }}/{{ l.name_local }} bidi={{ l.bidi }}'})
+ def test_i18n28_2(self):
+ output = render('i18n28_2')
+ self.assertEqual(output, 'de: German/Deutsch bidi=False')
+
+ @setup({'i18n29': '{% load i18n %}'
+ '{% get_language_info for LANGUAGE_CODE as l %}'
+ '{{ l.code }}: {{ l.name }}/{{ l.name_local }} bidi={{ l.bidi }}'})
+ def test_i18n29(self):
+ output = render('i18n29', {'LANGUAGE_CODE': 'fi'})
+ self.assertEqual(output, 'fi: Finnish/suomi bidi=False')
+
+ @setup({'i18n30': '{% load i18n %}'
+ '{% get_language_info_list for langcodes as langs %}'
+ '{% for l in langs %}{{ l.code }}: {{ l.name }}/'
+ '{{ l.name_local }} bidi={{ l.bidi }}; {% endfor %}'})
+ def test_i18n30(self):
+ output = render('i18n30', {'langcodes': ['it', 'no']})
+ self.assertEqual(output, 'it: Italian/italiano bidi=False; no: Norwegian/norsk bidi=False; ')
+
+ @setup({'i18n31': '{% load i18n %}'
+ '{% get_language_info_list for langcodes as langs %}'
+ '{% for l in langs %}{{ l.code }}: {{ l.name }}/'
+ '{{ l.name_local }} bidi={{ l.bidi }}; {% endfor %}'})
+ def test_i18n31(self):
+ output = render('i18n31', {'langcodes': (('sl', 'Slovenian'), ('fa', 'Persian'))})
+ self.assertEqual(
+ output,
+ 'sl: Slovenian/Sloven\u0161\u010dina bidi=False; '
+ 'fa: Persian/\u0641\u0627\u0631\u0633\u06cc bidi=True; '
+ )
+
+ @setup({'i18n32': '{% load i18n %}{{ "hu"|language_name }} '
+ '{{ "hu"|language_name_local }} {{ "hu"|language_bidi }}'})
+ def test_i18n32(self):
+ output = render('i18n32')
+ self.assertEqual(output, 'Hungarian Magyar False')
+
+ @setup({'i18n33': '{% load i18n %}'
+ '{{ langcode|language_name }} {{ langcode|language_name_local }} '
+ '{{ langcode|language_bidi }}'})
+ def test_i18n33(self):
+ output = render('i18n33', {'langcode': 'nl'})
+ self.assertEqual(output, 'Dutch Nederlands False')
+
+ # blocktrans handling of variables which are not in the context.
+ # this should work as if blocktrans was not there (#19915)
+ @setup({'i18n34': '{% load i18n %}{% blocktrans %}{{ missing }}{% endblocktrans %}'})
+ def test_i18n34(self):
+ output = render('i18n34')
+ if settings.TEMPLATE_STRING_IF_INVALID:
+ self.assertEqual(output, 'INVALID')
+ else:
+ self.assertEqual(output, '')
+
+ @setup({'i18n34_2': '{% load i18n %}{% blocktrans with a=\'α\' %}{{ missing }}{% endblocktrans %}'})
+ def test_i18n34_2(self):
+ output = render('i18n34_2')
+ if settings.TEMPLATE_STRING_IF_INVALID:
+ self.assertEqual(output, 'INVALID')
+ else:
+ self.assertEqual(output, '')
+
+ @setup({'i18n34_3': '{% load i18n %}{% blocktrans with a=anton %}{{ missing }}{% endblocktrans %}'})
+ def test_i18n34_3(self):
+ output = render('i18n34_3', {'anton': '\xce\xb1'})
+ if settings.TEMPLATE_STRING_IF_INVALID:
+ self.assertEqual(output, 'INVALID')
+ else:
+ self.assertEqual(output, '')
+
+ # trans tag with as var
+ @setup({'i18n35': '{% load i18n %}{% trans "Page not found" as page_not_found %}{{ page_not_found }}'})
+ def test_i18n35(self):
+ output = render('i18n35', {'LANGUAGE_CODE': 'de'})
+ self.assertEqual(output, 'Seite nicht gefunden')
+
+ @setup({'i18n36': '{% load i18n %}'
+ '{% trans "Page not found" noop as page_not_found %}{{ page_not_found }}'})
+ def test_i18n36(self):
+ output = render('i18n36', {'LANGUAGE_CODE': 'de'})
+ self.assertEqual(output, 'Page not found')
+
+ @setup({'i18n37': '{% load i18n %}'
+ '{% trans "Page not found" as page_not_found %}'
+ '{% blocktrans %}Error: {{ page_not_found }}{% endblocktrans %}'})
+ def test_i18n37(self):
+ output = render('i18n37', {'LANGUAGE_CODE': 'de'})
+ self.assertEqual(output, 'Error: Seite nicht gefunden')
+
+ # Test whitespace in filter arguments
+ @setup({'i18n38': '{% load i18n custom %}'
+ '{% get_language_info for "de"|noop:"x y" as l %}'
+ '{{ l.code }}: {{ l.name }}/{{ l.name_local }} bidi={{ l.bidi }}'})
+ def test_i18n38(self):
+ output = render('i18n38')
+ self.assertEqual(output, 'de: German/Deutsch bidi=False')
+
+ @setup({'i18n38_2': '{% load i18n custom %}'
+ '{% get_language_info_list for langcodes|noop:"x y" as langs %}'
+ '{% for l in langs %}{{ l.code }}: {{ l.name }}/'
+ '{{ l.name_local }} bidi={{ l.bidi }}; {% endfor %}'})
+ def test_i18n38_2(self):
+ output = render('i18n38_2', {'langcodes': ['it', 'no']})
+ self.assertEqual(output, 'it: Italian/italiano bidi=False; no: Norwegian/norsk bidi=False; ')