diff options
| author | Mike Hansen <mike@rover.com> | 2019-06-21 09:41:01 -0700 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-12-18 13:15:38 +0100 |
| commit | d291c72bf24387e4abe8d9883ebe9c69abdd26d0 (patch) | |
| tree | 72bd9196d31c4cdecaae59d02a30217c4d87718a /tests | |
| parent | 70d95682b1be6a85abdeaea0205a897f7530f8bc (diff) | |
Fixed #30585 -- Added {% translate %} and {% blocktranslate %} template tags.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/i18n/commands/templates/plural.djtpl | 2 | ||||
| -rw-r--r-- | tests/i18n/commands/templates/test.html | 14 | ||||
| -rwxr-xr-x | tests/i18n/sampleproject/update_catalogs.py | 9 | ||||
| -rw-r--r-- | tests/i18n/test_extraction.py | 12 | ||||
| -rw-r--r-- | tests/template_tests/syntax_tests/i18n/test_blocktranslate.py (renamed from tests/template_tests/syntax_tests/i18n/test_blocktrans.py) | 177 | ||||
| -rw-r--r-- | tests/template_tests/syntax_tests/i18n/test_translate.py (renamed from tests/template_tests/syntax_tests/i18n/test_trans.py) | 109 |
6 files changed, 237 insertions, 86 deletions
diff --git a/tests/i18n/commands/templates/plural.djtpl b/tests/i18n/commands/templates/plural.djtpl index 2d1566f7eb..fc37855d20 100644 --- a/tests/i18n/commands/templates/plural.djtpl +++ b/tests/i18n/commands/templates/plural.djtpl @@ -5,4 +5,4 @@ shouldn't create a .po file with duplicate `Plural-Forms` headers {% endcomment %} {% blocktrans count number=3 %}{{ number }} Bar{% plural %}{{ number }} Bars{% endblocktrans %} -{% trans 'First `trans`, then `blocktrans` with a plural' %} +{% translate 'First `translate`, then `blocktranslate` with a plural' %} diff --git a/tests/i18n/commands/templates/test.html b/tests/i18n/commands/templates/test.html index cac034e63e..0671a68860 100644 --- a/tests/i18n/commands/templates/test.html +++ b/tests/i18n/commands/templates/test.html @@ -85,9 +85,11 @@ continued here.{% endcomment %} {% blocktrans context 'Special blocktrans context wrapped in single quotes' %}Translatable literal with context wrapped in single quotes{% endblocktrans %} {% blocktrans context "Special blocktrans context wrapped in double quotes" %}Translatable literal with context wrapped in double quotes{% endblocktrans %} +{% blocktranslate %}blocktranslate text{% endblocktranslate %} +{% translate "translate text" %} -{# BasicExtractorTests.test_blocktrans_trimmed #} -{% blocktrans %} +{# BasicExtractorTests.test_blocktranslate_trimmed #} +{% blocktranslate %} Text with a few line breaks. {% endblocktrans %} @@ -98,10 +100,10 @@ continued here.{% endcomment %} {% endblocktrans %} {% trans "Get my line number" %} -{% blocktrans trimmed count counter=mylist|length %} -First `trans`, then `blocktrans` with a plural +{% blocktranslate trimmed count counter=mylist|length %} +First `translate`, then `blocktranslate` with a plural {% plural %} -Plural for a `trans` and `blocktrans` collision case -{% endblocktrans %} +Plural for a `translate` and `blocktranslate` collision case +{% endblocktranslate %} {% trans "Non-breaking space :" %} diff --git a/tests/i18n/sampleproject/update_catalogs.py b/tests/i18n/sampleproject/update_catalogs.py index 131d3e268b..8c57307f47 100755 --- a/tests/i18n/sampleproject/update_catalogs.py +++ b/tests/i18n/sampleproject/update_catalogs.py @@ -8,12 +8,13 @@ by using catalogs created from management commands. Example: -The string "Two %% Three %%%" renders differently using trans and blocktrans. -This issue is difficult to debug, it could be a problem with extraction, -interpolation, or both. +The string "Two %% Three %%%" renders differently using translate and +blocktranslate. This issue is difficult to debug, it could be a problem with +extraction, interpolation, or both. How this script helps: - * Add {% trans "Two %% Three %%%" %} and blocktrans equivalent to templates. + * Add {% translate "Two %% Three %%%" %} and blocktranslate equivalent to + templates. * Run this script. * Test extraction - verify the new msgid in sampleproject's django.po. * Add a translation to sampleproject's django.po. diff --git a/tests/i18n/test_extraction.py b/tests/i18n/test_extraction.py index e18a335135..db66ea1db5 100644 --- a/tests/i18n/test_extraction.py +++ b/tests/i18n/test_extraction.py @@ -182,7 +182,7 @@ class BasicExtractorTests(ExtractorTests): po_contents = fp.read() self.assertMsgId("Non-breaking space\u00a0:", po_contents) - def test_blocktrans_trimmed(self): + def test_blocktranslate_trimmed(self): management.call_command('makemessages', locale=[LOCALE], verbosity=0) self.assertTrue(os.path.exists(self.PO_FILE)) with open(self.PO_FILE) as fp: @@ -256,6 +256,10 @@ class BasicExtractorTests(ExtractorTests): self.assertIn('msgctxt "Special blocktrans context #4"', po_contents) self.assertMsgId("Translatable literal #8d %(a)s", po_contents) + # {% translate %} and {% blocktranslate %} + self.assertMsgId('translate text', po_contents) + self.assertMsgId('blocktranslate text', po_contents) + def test_context_in_single_quotes(self): management.call_command('makemessages', locale=[LOCALE], verbosity=0) self.assertTrue(os.path.exists(self.PO_FILE)) @@ -528,7 +532,7 @@ class CopyPluralFormsExtractorTests(ExtractorTests): found = re.findall(r'^(?P<value>"Plural-Forms.+?\\n")\s*$', po_contents, re.MULTILINE | re.DOTALL) self.assertEqual(1, len(found)) - def test_trans_and_plural_blocktrans_collision(self): + def test_translate_and_plural_blocktranslate_collision(self): """ Ensures a correct workaround for the gettext bug when handling a literal found inside a {% trans %} tag and also in another file inside a @@ -539,8 +543,8 @@ class CopyPluralFormsExtractorTests(ExtractorTests): with open(self.PO_FILE) as fp: po_contents = fp.read() self.assertNotIn("#-#-#-#-# django.pot (PACKAGE VERSION) #-#-#-#-#\\n", po_contents) - self.assertMsgId('First `trans`, then `blocktrans` with a plural', po_contents) - self.assertMsgIdPlural('Plural for a `trans` and `blocktrans` collision case', po_contents) + self.assertMsgId('First `translate`, then `blocktranslate` with a plural', po_contents) + self.assertMsgIdPlural('Plural for a `translate` and `blocktranslate` collision case', po_contents) class NoWrapExtractorTests(ExtractorTests): diff --git a/tests/template_tests/syntax_tests/i18n/test_blocktrans.py b/tests/template_tests/syntax_tests/i18n/test_blocktranslate.py index 744b410ea6..11bd456ea8 100644 --- a/tests/template_tests/syntax_tests/i18n/test_blocktrans.py +++ b/tests/template_tests/syntax_tests/i18n/test_blocktranslate.py @@ -1,4 +1,6 @@ +import inspect import os +from functools import partial, wraps from asgiref.local import Local @@ -8,10 +10,39 @@ from django.utils import translation from django.utils.safestring import mark_safe from django.utils.translation import trans_real -from ...utils import setup +from ...utils import setup as base_setup from .base import MultipleLocaleActivationTestCase, extended_locale_paths, here +def setup(templates, *args, **kwargs): + blocktrans_setup = base_setup(templates, *args, **kwargs) + blocktranslate_setup = base_setup({ + name: template.replace( + '{% blocktrans ', '{% blocktranslate ' + ).replace( + '{% endblocktrans %}', '{% endblocktranslate %}' + ) + 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) + return inner + return decorator + + class I18nBlockTransTagTests(SimpleTestCase): libraries = {'i18n': 'django.templatetags.i18n'} @@ -76,8 +107,8 @@ class I18nBlockTransTagTests(SimpleTestCase): '{% 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. + Escaping inside blocktranslate and translate works as if it was + directly in the template. """ output = self.engine.render_to_string('i18n17', {'anton': 'α & β'}) self.assertEqual(output, 'α & β') @@ -224,8 +255,8 @@ class I18nBlockTransTagTests(SimpleTestCase): self.assertEqual(output, '>Error: Seite nicht gefunden<') @setup({'template': '{% load i18n %}{% blocktrans asvar %}Yes{% endblocktrans %}'}) - def test_blocktrans_syntax_error_missing_assignment(self): - msg = "No argument provided to the 'blocktrans' tag for the asvar option." + def test_blocktrans_syntax_error_missing_assignment(self, tag_name): + msg = "No argument provided to the '{}' tag for the asvar option.".format(tag_name) with self.assertRaisesMessage(TemplateSyntaxError, msg): self.engine.render_to_string('template') @@ -235,14 +266,14 @@ class I18nBlockTransTagTests(SimpleTestCase): self.assertEqual(output, '%s') @setup({'template': '{% load i18n %}{% blocktrans %}{% block b %} {% endblock %}{% endblocktrans %}'}) - def test_with_block(self): - msg = "'blocktrans' doesn't allow other block tags (seen 'block b') inside it" + def test_with_block(self, tag_name): + msg = "'{}' doesn't allow other block tags (seen 'block b') inside it".format(tag_name) with self.assertRaisesMessage(TemplateSyntaxError, msg): self.engine.render_to_string('template') @setup({'template': '{% load i18n %}{% blocktrans %}{% for b in [1, 2, 3] %} {% endfor %}{% endblocktrans %}'}) - def test_with_for(self): - msg = "'blocktrans' doesn't allow other block tags (seen 'for b in [1, 2, 3]') inside it" + def test_with_for(self, tag_name): + msg = "'{}' doesn't allow other block tags (seen 'for b in [1, 2, 3]') inside it".format(tag_name) with self.assertRaisesMessage(TemplateSyntaxError, msg): self.engine.render_to_string('template') @@ -252,14 +283,14 @@ class I18nBlockTransTagTests(SimpleTestCase): self.engine.render_to_string('template', {'foo': 'bar'}) @setup({'template': '{% load i18n %}{% blocktrans with %}{% endblocktrans %}'}) - def test_no_args_with(self): - msg = '"with" in \'blocktrans\' tag needs at least one keyword argument.' + def test_no_args_with(self, tag_name): + msg = '"with" in \'{}\' tag needs at least one keyword argument.'.format(tag_name) with self.assertRaisesMessage(TemplateSyntaxError, msg): self.engine.render_to_string('template') @setup({'template': '{% load i18n %}{% blocktrans count a %}{% endblocktrans %}'}) - def test_count(self): - msg = '"count" in \'blocktrans\' tag expected exactly one keyword argument.' + def test_count(self, tag_name): + msg = '"count" in \'{}\' tag expected exactly one keyword argument.'.format(tag_name) with self.assertRaisesMessage(TemplateSyntaxError, msg): self.engine.render_to_string('template', {'a': [1, 2, 3]}) @@ -268,13 +299,25 @@ class I18nBlockTransTagTests(SimpleTestCase): 'There is {{ count }} object. {% block a %} {% endblock %}' '{% endblocktrans %}' )}) - def test_plural_bad_syntax(self): - msg = "'blocktrans' doesn't allow other block tags inside it" + def test_plural_bad_syntax(self, tag_name): + msg = "'{}' doesn't allow other block tags inside it".format(tag_name) with self.assertRaisesMessage(TemplateSyntaxError, msg): self.engine.render_to_string('template', {'var': [1, 2, 3]}) class TranslationBlockTransTagTests(SimpleTestCase): + tag_name = 'blocktrans' + + def get_template(self, template_string): + return Template( + template_string.replace( + '{{% blocktrans ', + '{{% {}'.format(self.tag_name) + ).replace( + '{{% endblocktrans %}}', + '{{% end{} %}}'.format(self.tag_name) + ) + ) @override_settings(LOCALE_PATHS=extended_locale_paths) def test_template_tags_pgettext(self): @@ -283,54 +326,58 @@ class TranslationBlockTransTagTests(SimpleTestCase): trans_real._translations = {} with translation.override('de'): # Nonexistent context - t = Template('{% load i18n %}{% blocktrans context "nonexistent" %}May{% endblocktrans %}') + t = self.get_template('{% load i18n %}{% blocktrans context "nonexistent" %}May{% endblocktrans %}') rendered = t.render(Context()) self.assertEqual(rendered, 'May') # Existing context... using a literal - t = Template('{% load i18n %}{% blocktrans context "month name" %}May{% endblocktrans %}') + t = self.get_template('{% load i18n %}{% blocktrans context "month name" %}May{% endblocktrans %}') rendered = t.render(Context()) self.assertEqual(rendered, 'Mai') - t = Template('{% load i18n %}{% blocktrans context "verb" %}May{% endblocktrans %}') + t = self.get_template('{% load i18n %}{% blocktrans context "verb" %}May{% endblocktrans %}') rendered = t.render(Context()) self.assertEqual(rendered, 'Kann') # Using a variable - t = Template('{% load i18n %}{% blocktrans context message_context %}May{% endblocktrans %}') + t = self.get_template('{% load i18n %}{% blocktrans context message_context %}May{% endblocktrans %}') rendered = t.render(Context({'message_context': 'month name'})) self.assertEqual(rendered, 'Mai') - t = Template('{% load i18n %}{% blocktrans context message_context %}May{% endblocktrans %}') + t = self.get_template('{% load i18n %}{% blocktrans context message_context %}May{% endblocktrans %}') rendered = t.render(Context({'message_context': 'verb'})) self.assertEqual(rendered, 'Kann') # Using a filter - t = Template('{% load i18n %}{% blocktrans context message_context|lower %}May{% endblocktrans %}') + t = self.get_template( + '{% load i18n %}{% blocktrans context message_context|lower %}May{% endblocktrans %}' + ) rendered = t.render(Context({'message_context': 'MONTH NAME'})) self.assertEqual(rendered, 'Mai') - t = Template('{% load i18n %}{% blocktrans context message_context|lower %}May{% endblocktrans %}') + t = self.get_template( + '{% load i18n %}{% blocktrans context message_context|lower %}May{% endblocktrans %}' + ) rendered = t.render(Context({'message_context': 'VERB'})) self.assertEqual(rendered, 'Kann') # Using 'count' - t = Template( + t = self.get_template( '{% load i18n %}{% blocktrans count number=1 context "super search" %}' '{{ number }} super result{% plural %}{{ number }} super results{% endblocktrans %}' ) rendered = t.render(Context()) self.assertEqual(rendered, '1 Super-Ergebnis') - t = Template( + t = self.get_template( '{% load i18n %}{% blocktrans count number=2 context "super search" %}{{ number }}' ' super result{% plural %}{{ number }} super results{% endblocktrans %}' ) rendered = t.render(Context()) self.assertEqual(rendered, '2 Super-Ergebnisse') - t = Template( + t = self.get_template( '{% load i18n %}{% blocktrans context "other super search" count number=1 %}' '{{ number }} super result{% plural %}{{ number }} super results{% endblocktrans %}' ) rendered = t.render(Context()) self.assertEqual(rendered, '1 anderen Super-Ergebnis') - t = Template( + t = self.get_template( '{% load i18n %}{% blocktrans context "other super search" count number=2 %}' '{{ number }} super result{% plural %}{{ number }} super results{% endblocktrans %}' ) @@ -338,13 +385,13 @@ class TranslationBlockTransTagTests(SimpleTestCase): self.assertEqual(rendered, '2 andere Super-Ergebnisse') # Using 'with' - t = Template( + t = self.get_template( '{% load i18n %}{% blocktrans with num_comments=5 context "comment count" %}' 'There are {{ num_comments }} comments{% endblocktrans %}' ) rendered = t.render(Context()) self.assertEqual(rendered, 'Es gibt 5 Kommentare') - t = Template( + t = self.get_template( '{% load i18n %}{% blocktrans with num_comments=5 context "other comment count" %}' 'There are {{ num_comments }} comments{% endblocktrans %}' ) @@ -352,19 +399,19 @@ class TranslationBlockTransTagTests(SimpleTestCase): self.assertEqual(rendered, 'Andere: Es gibt 5 Kommentare') # Using trimmed - t = Template( + t = self.get_template( '{% load i18n %}{% blocktrans trimmed %}\n\nThere\n\t are 5 ' '\n\n comments\n{% endblocktrans %}' ) rendered = t.render(Context()) self.assertEqual(rendered, 'There are 5 comments') - t = Template( + t = self.get_template( '{% load i18n %}{% blocktrans with num_comments=5 context "comment count" trimmed %}\n\n' 'There are \t\n \t {{ num_comments }} comments\n\n{% endblocktrans %}' ) rendered = t.render(Context()) self.assertEqual(rendered, 'Es gibt 5 Kommentare') - t = Template( + t = self.get_template( '{% load i18n %}{% blocktrans context "other super search" count number=2 trimmed %}\n' '{{ number }} super \n result{% plural %}{{ number }} super results{% endblocktrans %}' ) @@ -374,12 +421,14 @@ class TranslationBlockTransTagTests(SimpleTestCase): # Misuses msg = "Unknown argument for 'blocktrans' tag: %r." with self.assertRaisesMessage(TemplateSyntaxError, msg % 'month="May"'): - Template('{% load i18n %}{% blocktrans context with month="May" %}{{ month }}{% endblocktrans %}') + self.get_template( + '{% load i18n %}{% blocktrans context with month="May" %}{{ month }}{% endblocktrans %}' + ) msg = '"context" in %r tag expected exactly one argument.' % 'blocktrans' with self.assertRaisesMessage(TemplateSyntaxError, msg): - Template('{% load i18n %}{% blocktrans context %}{% endblocktrans %}') + self.get_template('{% load i18n %}{% blocktrans context %}{% endblocktrans %}') with self.assertRaisesMessage(TemplateSyntaxError, msg): - Template( + self.get_template( '{% load i18n %}{% blocktrans count number=2 context %}' '{{ number }} super result{% plural %}{{ number }}' ' super results{% endblocktrans %}' @@ -409,7 +458,23 @@ class TranslationBlockTransTagTests(SimpleTestCase): self.assertEqual(rendered, 'My other name is James.') +class TranslationBlockTranslationTagTests(TranslationBlockTransTagTests): + tag_name = 'blocktranslation' + + class MultipleLocaleActivationBlockTransTests(MultipleLocaleActivationTestCase): + tag_name = 'blocktrans' + + def get_template(self, template_string): + return Template( + template_string.replace( + '{{% blocktrans ', + '{{% {}'.format(self.tag_name) + ).replace( + '{{% endblocktrans %}}', + '{{% end{} %}}'.format(self.tag_name) + ) + ) def test_single_locale_activation(self): """ @@ -418,35 +483,51 @@ class MultipleLocaleActivationBlockTransTests(MultipleLocaleActivationTestCase): """ with translation.override('fr'): self.assertEqual( - Template("{% load i18n %}{% blocktrans %}Yes{% endblocktrans %}").render(Context({})), + self.get_template("{% load i18n %}{% blocktrans %}Yes{% endblocktrans %}").render(Context({})), 'Oui' ) def test_multiple_locale_btrans(self): with translation.override('de'): - t = Template("{% load i18n %}{% blocktrans %}No{% endblocktrans %}") + t = self.get_template("{% load i18n %}{% blocktrans %}No{% endblocktrans %}") 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): - t = Template("{% load i18n %}{% blocktrans %}No{% endblocktrans %}") + t = self.get_template("{% load i18n %}{% blocktrans %}No{% endblocktrans %}") with translation.override('nl'): self.assertEqual(t.render(Context({})), 'Nee') def test_multiple_locale_direct_switch_btrans(self): with translation.override('de'): - t = Template("{% load i18n %}{% blocktrans %}No{% endblocktrans %}") + t = self.get_template("{% load i18n %}{% blocktrans %}No{% endblocktrans %}") with translation.override('nl'): self.assertEqual(t.render(Context({})), 'Nee') +class MultipleLocaleActivationBlockTranslationTests(MultipleLocaleActivationBlockTransTests): + tag_name = 'blocktranslation' + + class MiscTests(SimpleTestCase): + tag_name = 'blocktranslate' + + def get_template(self, template_string): + return Template( + template_string.replace( + '{{% blocktrans ', + '{{% {}'.format(self.tag_name) + ).replace( + '{{% endblocktrans %}}', + '{{% end{} %}}'.format(self.tag_name) + ) + ) @override_settings(LOCALE_PATHS=extended_locale_paths) def test_percent_in_translatable_block(self): - t_sing = Template("{% load i18n %}{% blocktrans %}The result was {{ percent }}%{% endblocktrans %}") - t_plur = Template( + t_sing = self.get_template("{% load i18n %}{% blocktrans %}The result was {{ percent }}%{% endblocktrans %}") + t_plur = self.get_template( "{% load i18n %}{% blocktrans count num as number %}" "{{ percent }}% represents {{ num }} object{% plural %}" "{{ percent }}% represents {{ num }} objects{% endblocktrans %}" @@ -457,13 +538,15 @@ class MiscTests(SimpleTestCase): self.assertEqual(t_plur.render(Context({'percent': 42, 'num': 4})), '42% stellt 4 Objekte dar') @override_settings(LOCALE_PATHS=extended_locale_paths) - def test_percent_formatting_in_blocktrans(self): + def test_percent_formatting_in_blocktranslate(self): """ - Python's %-formatting is properly escaped in blocktrans, singular, or - plural. + Python's %-formatting is properly escaped in blocktranslate, singular, + or plural. """ - t_sing = Template("{% load i18n %}{% blocktrans %}There are %(num_comments)s comments{% endblocktrans %}") - t_plur = Template( + t_sing = self.get_template( + "{% load i18n %}{% blocktrans %}There are %(num_comments)s comments{% endblocktrans %}" + ) + t_plur = self.get_template( "{% load i18n %}{% blocktrans count num as number %}" "%(percent)s% represents {{ num }} object{% plural %}" "%(percent)s% represents {{ num }} objects{% endblocktrans %}" @@ -473,3 +556,7 @@ class MiscTests(SimpleTestCase): 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') + + +class MiscBlockTranslationTests(MiscTests): + tag_name = 'blocktrans' diff --git a/tests/template_tests/syntax_tests/i18n/test_trans.py b/tests/template_tests/syntax_tests/i18n/test_translate.py index 47a79ff74d..6011153a2d 100644 --- a/tests/template_tests/syntax_tests/i18n/test_trans.py +++ b/tests/template_tests/syntax_tests/i18n/test_translate.py @@ -1,3 +1,6 @@ +import inspect +from functools import partial, wraps + from asgiref.local import Local from django.template import Context, Template, TemplateSyntaxError @@ -7,10 +10,35 @@ from django.utils import translation from django.utils.safestring import mark_safe from django.utils.translation import trans_real -from ...utils import setup +from ...utils import setup as base_setup from .base import MultipleLocaleActivationTestCase, extended_locale_paths +def setup(templates, *args, **kwargs): + trans_setup = base_setup(templates, *args, **kwargs) + translate_setup = base_setup({ + name: template.replace('{% trans ', '{% translate ') + 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 + + class I18nTransTagTests(SimpleTestCase): libraries = {'i18n': 'django.templatetags.i18n'} @@ -84,38 +112,38 @@ class I18nTransTagTests(SimpleTestCase): self.assertEqual(output, 'Page not found') @setup({'template': '{% load i18n %}{% trans %}A}'}) - def test_syntax_error_no_arguments(self): - msg = "'trans' takes at least one argument" + def test_syntax_error_no_arguments(self, tag_name): + msg = "'{}' takes at least one argument".format(tag_name) with self.assertRaisesMessage(TemplateSyntaxError, msg): self.engine.render_to_string('template') @setup({'template': '{% load i18n %}{% trans "Yes" badoption %}'}) - def test_syntax_error_bad_option(self): - msg = "Unknown argument for 'trans' tag: 'badoption'" + def test_syntax_error_bad_option(self, tag_name): + msg = "Unknown argument for '{}' tag: 'badoption'".format(tag_name) with self.assertRaisesMessage(TemplateSyntaxError, msg): self.engine.render_to_string('template') @setup({'template': '{% load i18n %}{% trans "Yes" as %}'}) - def test_syntax_error_missing_assignment(self): - msg = "No argument provided to the 'trans' tag for the as option." + def test_syntax_error_missing_assignment(self, tag_name): + msg = "No argument provided to the '{}' tag for the as option.".format(tag_name) with self.assertRaisesMessage(TemplateSyntaxError, msg): self.engine.render_to_string('template') @setup({'template': '{% load i18n %}{% trans "Yes" as var context %}'}) - def test_syntax_error_missing_context(self): - msg = "No argument provided to the 'trans' tag for the context option." + def test_syntax_error_missing_context(self, tag_name): + msg = "No argument provided to the '{}' tag for the context option.".format(tag_name) with self.assertRaisesMessage(TemplateSyntaxError, msg): self.engine.render_to_string('template') @setup({'template': '{% load i18n %}{% trans "Yes" context as var %}'}) - def test_syntax_error_context_as(self): - msg = "Invalid argument 'as' provided to the 'trans' tag for the context option" + def test_syntax_error_context_as(self, tag_name): + msg = "Invalid argument 'as' provided to the '{}' tag for the context option".format(tag_name) with self.assertRaisesMessage(TemplateSyntaxError, msg): self.engine.render_to_string('template') @setup({'template': '{% load i18n %}{% trans "Yes" context noop %}'}) - def test_syntax_error_context_noop(self): - msg = "Invalid argument 'noop' provided to the 'trans' tag for the context option" + def test_syntax_error_context_noop(self, tag_name): + msg = "Invalid argument 'noop' provided to the '{}' tag for the context option".format(tag_name) with self.assertRaisesMessage(TemplateSyntaxError, msg): self.engine.render_to_string('template') @@ -132,6 +160,15 @@ class I18nTransTagTests(SimpleTestCase): class TranslationTransTagTests(SimpleTestCase): + tag_name = 'trans' + + def get_template(self, template_string): + return Template( + template_string.replace( + '{{% trans ', + '{{% {}'.format(self.tag_name) + ) + ) @override_settings(LOCALE_PATHS=extended_locale_paths) def test_template_tags_pgettext(self): @@ -140,44 +177,57 @@ class TranslationTransTagTests(SimpleTestCase): trans_real._translations = {} with translation.override('de'): # Nonexistent context... - t = Template('{% load i18n %}{% trans "May" context "nonexistent" %}') + t = self.get_template('{% load i18n %}{% trans "May" context "nonexistent" %}') rendered = t.render(Context()) self.assertEqual(rendered, 'May') # Existing context... using a literal - t = Template('{% load i18n %}{% trans "May" context "month name" %}') + t = self.get_template('{% load i18n %}{% trans "May" context "month name" %}') rendered = t.render(Context()) self.assertEqual(rendered, 'Mai') - t = Template('{% load i18n %}{% trans "May" context "verb" %}') + t = self.get_template('{% load i18n %}{% trans "May" context "verb" %}') rendered = t.render(Context()) self.assertEqual(rendered, 'Kann') # Using a variable - t = Template('{% load i18n %}{% trans "May" context message_context %}') + t = self.get_template('{% load i18n %}{% trans "May" context message_context %}') rendered = t.render(Context({'message_context': 'month name'})) self.assertEqual(rendered, 'Mai') - t = Template('{% load i18n %}{% trans "May" context message_context %}') + t = self.get_template('{% load i18n %}{% trans "May" context message_context %}') rendered = t.render(Context({'message_context': 'verb'})) self.assertEqual(rendered, 'Kann') # Using a filter - t = Template('{% load i18n %}{% trans "May" context message_context|lower %}') + t = self.get_template('{% load i18n %}{% trans "May" context message_context|lower %}') rendered = t.render(Context({'message_context': 'MONTH NAME'})) self.assertEqual(rendered, 'Mai') - t = Template('{% load i18n %}{% trans "May" context message_context|lower %}') + t = self.get_template('{% load i18n %}{% trans "May" context message_context|lower %}') rendered = t.render(Context({'message_context': 'VERB'})) self.assertEqual(rendered, 'Kann') # Using 'as' - t = Template('{% load i18n %}{% trans "May" context "month name" as var %}Value: {{ var }}') + t = self.get_template('{% load i18n %}{% trans "May" context "month name" as var %}Value: {{ var }}') rendered = t.render(Context()) self.assertEqual(rendered, 'Value: Mai') - t = Template('{% load i18n %}{% trans "May" as var context "verb" %}Value: {{ var }}') + t = self.get_template('{% load i18n %}{% trans "May" as var context "verb" %}Value: {{ var }}') rendered = t.render(Context()) self.assertEqual(rendered, 'Value: Kann') +class TranslationTranslateTagTests(TranslationTransTagTests): + tag_name = 'translate' + + class MultipleLocaleActivationTransTagTests(MultipleLocaleActivationTestCase): + tag_name = 'trans' + + def get_template(self, template_string): + return Template( + template_string.replace( + '{{% trans ', + '{{% {}'.format(self.tag_name) + ) + ) def test_single_locale_activation(self): """ @@ -185,27 +235,34 @@ class MultipleLocaleActivationTransTagTests(MultipleLocaleActivationTestCase): constructs. """ with translation.override('fr'): - self.assertEqual(Template("{% load i18n %}{% trans 'Yes' %}").render(Context({})), 'Oui') + self.assertEqual( + self.get_template("{% load i18n %}{% trans 'Yes' %}").render(Context({})), + 'Oui' + ) def test_multiple_locale_trans(self): with translation.override('de'): - t = Template("{% load i18n %}{% trans 'No' %}") + t = self.get_template("{% load i18n %}{% trans 'No' %}") 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): - t = Template("{% load i18n %}{% trans 'No' %}") + t = self.get_template("{% load i18n %}{% trans 'No' %}") with translation.override('nl'): self.assertEqual(t.render(Context({})), 'Nee') def test_multiple_locale_direct_switch_trans(self): with translation.override('de'): - t = Template("{% load i18n %}{% trans 'No' %}") + t = self.get_template("{% load i18n %}{% trans 'No' %}") with translation.override('nl'): self.assertEqual(t.render(Context({})), 'Nee') +class MultipleLocaleActivationTranslateTagTests(MultipleLocaleActivationTransTagTests): + tag_name = 'translate' + + class LocalizeNodeTests(SimpleTestCase): def test_repr(self): node = LocalizeNode(nodelist=[], use_l10n=True) |
