From d291c72bf24387e4abe8d9883ebe9c69abdd26d0 Mon Sep 17 00:00:00 2001 From: Mike Hansen Date: Fri, 21 Jun 2019 09:41:01 -0700 Subject: Fixed #30585 -- Added {% translate %} and {% blocktranslate %} template tags. --- django/templatetags/i18n.py | 19 +- django/utils/translation/template.py | 8 +- docs/ref/contrib/admin/index.txt | 4 +- docs/ref/templates/builtins.txt | 10 +- docs/ref/templates/language.txt | 2 +- docs/releases/3.1.txt | 5 +- docs/topics/cache.txt | 2 +- docs/topics/i18n/translation.txt | 148 +++--- tests/i18n/commands/templates/plural.djtpl | 2 +- tests/i18n/commands/templates/test.html | 14 +- tests/i18n/sampleproject/update_catalogs.py | 9 +- tests/i18n/test_extraction.py | 12 +- .../syntax_tests/i18n/test_blocktrans.py | 475 ----------------- .../syntax_tests/i18n/test_blocktranslate.py | 562 +++++++++++++++++++++ .../template_tests/syntax_tests/i18n/test_trans.py | 212 -------- .../syntax_tests/i18n/test_translate.py | 269 ++++++++++ 16 files changed, 962 insertions(+), 791 deletions(-) delete mode 100644 tests/template_tests/syntax_tests/i18n/test_blocktrans.py create mode 100644 tests/template_tests/syntax_tests/i18n/test_blocktranslate.py delete mode 100644 tests/template_tests/syntax_tests/i18n/test_trans.py create mode 100644 tests/template_tests/syntax_tests/i18n/test_translate.py diff --git a/django/templatetags/i18n.py b/django/templatetags/i18n.py index d83c8c525d..5618c88bd4 100644 --- a/django/templatetags/i18n.py +++ b/django/templatetags/i18n.py @@ -98,7 +98,8 @@ class TranslateNode(Node): class BlockTranslateNode(Node): def __init__(self, extra_context, singular, plural=None, countervar=None, - counter=None, message_context=None, trimmed=False, asvar=None): + counter=None, message_context=None, trimmed=False, asvar=None, + tag_name='blocktranslate'): self.extra_context = extra_context self.singular = singular self.plural = plural @@ -107,6 +108,7 @@ class BlockTranslateNode(Node): self.message_context = message_context self.trimmed = trimmed self.asvar = asvar + self.tag_name = tag_name def render_token_list(self, tokens): result = [] @@ -163,8 +165,8 @@ class BlockTranslateNode(Node): if nested: # Either string is malformed, or it's a bug raise TemplateSyntaxError( - "'blocktrans' is unable to format string returned by gettext: %r using %r" - % (result, data) + '%r is unable to format string returned by gettext: %r ' + 'using %r' % (self.tag_name, result, data) ) with translation.override(None): result = self.render(context, nested=True) @@ -313,6 +315,7 @@ def do_get_current_language_bidi(parser, token): return GetCurrentLanguageBidiNode(args[2]) +@register.tag("translate") @register.tag("trans") def do_translate(parser, token): """ @@ -406,6 +409,7 @@ def do_translate(parser, token): return TranslateNode(message_string, noop, asvar, message_context) +@register.tag("blocktranslate") @register.tag("blocktrans") def do_block_translate(parser, token): """ @@ -513,19 +517,20 @@ def do_block_translate(parser, token): break if countervar and counter: if token.contents.strip() != 'plural': - raise TemplateSyntaxError("'blocktrans' doesn't allow other block tags inside it") + raise TemplateSyntaxError("%r doesn't allow other block tags inside it" % bits[0]) while parser.tokens: token = parser.next_token() if token.token_type in (TokenType.VAR, TokenType.TEXT): plural.append(token) else: break - if token.contents.strip() != 'endblocktrans': - raise TemplateSyntaxError("'blocktrans' doesn't allow other block tags (seen %r) inside it" % token.contents) + end_tag_name = 'end%s' % bits[0] + if token.contents.strip() != end_tag_name: + raise TemplateSyntaxError("%r doesn't allow other block tags (seen %r) inside it" % (bits[0], token.contents)) return BlockTranslateNode(extra_context, singular, plural, countervar, counter, message_context, trimmed=trimmed, - asvar=asvar) + asvar=asvar, tag_name=bits[0]) @register.tag diff --git a/django/utils/translation/template.py b/django/utils/translation/template.py index 979ae1ade6..fee66dac9a 100644 --- a/django/utils/translation/template.py +++ b/django/utils/translation/template.py @@ -19,15 +19,15 @@ def blankout(src, char): context_re = _lazy_re_compile(r"""^\s+.*context\s+((?:"[^"]*?")|(?:'[^']*?'))\s*""") inline_re = _lazy_re_compile( - # Match the trans 'some text' part - r"""^\s*trans\s+((?:"[^"]*?")|(?:'[^']*?'))""" + # Match the trans/translate 'some text' part. + r"""^\s*trans(?:late)?\s+((?:"[^"]*?")|(?:'[^']*?'))""" # Match and ignore optional filters r"""(?:\s*\|\s*[^\s:]+(?::(?:[^\s'":]+|(?:"[^"]*?")|(?:'[^']*?')))?)*""" # Match the optional context part r"""(\s+.*context\s+((?:"[^"]*?")|(?:'[^']*?')))?\s*""" ) -block_re = _lazy_re_compile(r"""^\s*blocktrans(\s+.*context\s+((?:"[^"]*?")|(?:'[^']*?')))?(?:\s+|$)""") -endblock_re = _lazy_re_compile(r"""^\s*endblocktrans$""") +block_re = _lazy_re_compile(r"""^\s*blocktrans(?:late)?(\s+.*context\s+((?:"[^"]*?")|(?:'[^']*?')))?(?:\s+|$)""") +endblock_re = _lazy_re_compile(r"""^\s*endblocktrans(?:late)?$""") plural_re = _lazy_re_compile(r"""^\s*plural$""") constant_re = _lazy_re_compile(r"""_\(((?:".*?")|(?:'.*?'))\)""") diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt index 75d8245499..669fc185a4 100644 --- a/docs/ref/contrib/admin/index.txt +++ b/docs/ref/contrib/admin/index.txt @@ -2695,14 +2695,14 @@ Therefore here is our new ``change_form.html`` : {% load i18n admin_urls %} {% block object-tools-items %}
  • - {% trans "History" %} + {% translate "History" %}
  • My Link
  • {% if has_absolute_url %}
  • - {% trans "View on site" %} + {% translate "View on site" %}
  • {% endif %} {% endblock %} diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt index 2e55b66d04..4d65dc82f7 100644 --- a/docs/ref/templates/builtins.txt +++ b/docs/ref/templates/builtins.txt @@ -814,10 +814,10 @@ This would display as "It is the 4th of September". You can also use the syntax ``{% now "Y" as current_year %}`` to store the output (as a string) inside a variable. This is useful if you want to use -``{% now %}`` inside a template tag like :ttag:`blocktrans` for example:: +``{% now %}`` inside a template tag like :ttag:`blocktranslate` for example:: {% now "Y" as current_year %} - {% blocktrans %}Copyright {{ current_year }}{% endblocktrans %} + {% blocktranslate %}Copyright {{ current_year }}{% endblocktranslate %} .. templatetag:: regroup @@ -1200,10 +1200,10 @@ image in the above example will be 88 pixels wide (because 175/200 = .875; .875 * 100 = 87.5 which is rounded up to 88). In some cases you might want to capture the result of ``widthratio`` in a -variable. It can be useful, for instance, in a :ttag:`blocktrans` like this:: +variable. It can be useful, for instance, in a :ttag:`blocktranslate` like this:: {% widthratio this_value max_value max_width as width %} - {% blocktrans %}The width is: {{ width }}{% endblocktrans %} + {% blocktranslate %}The width is: {{ width }}{% endblocktranslate %} .. templatetag:: with @@ -2023,7 +2023,7 @@ Example:: You have {{ num_cherries }} cherr{{ num_cherries|pluralize:"y,ies" }}. -.. note:: Use :ttag:`blocktrans` to pluralize translated strings. +.. note:: Use :ttag:`blocktranslate` to pluralize translated strings. .. templatefilter:: pprint diff --git a/docs/ref/templates/language.txt b/docs/ref/templates/language.txt index 62f866bbc0..d5221ad60b 100644 --- a/docs/ref/templates/language.txt +++ b/docs/ref/templates/language.txt @@ -414,7 +414,7 @@ Here are some tips for working with inheritance: tag ``as`` syntax can't be used inside the block. For example, this template doesn't render anything:: - {% trans "Title" as title %} + {% translate "Title" as title %} {% block content %}{{ title }}{% endblock %} * For extra readability, you can optionally give a *name* to your diff --git a/docs/releases/3.1.txt b/docs/releases/3.1.txt index 136f2900b8..8a4d9f5326 100644 --- a/docs/releases/3.1.txt +++ b/docs/releases/3.1.txt @@ -260,7 +260,10 @@ Signals Templates ~~~~~~~~~ -* ... +* The renamed :ttag:`translate` and :ttag:`blocktranslate` template tags are + introduced for internationalization in template code. The older :ttag:`trans` + and :ttag:`blocktrans` template tags aliases continue to work, and will be + retained for the foreseeable future. Tests ~~~~~ diff --git a/docs/topics/cache.txt b/docs/topics/cache.txt index a038183d0d..29ea8fb001 100644 --- a/docs/topics/cache.txt +++ b/docs/topics/cache.txt @@ -682,7 +682,7 @@ templates to achieve the same result: {% get_current_language as LANGUAGE_CODE %} {% cache 600 welcome LANGUAGE_CODE %} - {% trans "Welcome to example.com" %} + {% translate "Welcome to example.com" %} {% endcache %} The cache timeout can be a template variable, as long as the template variable diff --git a/docs/topics/i18n/translation.txt b/docs/topics/i18n/translation.txt index 7a8be1eb4a..241dcea660 100644 --- a/docs/topics/i18n/translation.txt +++ b/docs/topics/i18n/translation.txt @@ -328,8 +328,8 @@ will appear in the ``.po`` file as: msgid "May" msgstr "" -Contextual markers are also supported by the :ttag:`trans` and -:ttag:`blocktrans` template tags. +Contextual markers are also supported by the :ttag:`translate` and +:ttag:`blocktranslate` template tags. .. _lazy-translations: @@ -575,21 +575,22 @@ have already loaded the ``i18n`` tag. unchanged. .. templatetag:: trans +.. templatetag:: translate -``trans`` template tag ----------------------- +``translate`` template tag +-------------------------- -The ``{% trans %}`` template tag translates either a constant string +The ``{% translate %}`` template tag translates either a constant string (enclosed in single or double quotes) or variable content:: - {% trans "This is the title." %} - {% trans myvar %} + {% translate "This is the title." %} + {% translate myvar %} If the ``noop`` option is present, variable lookup still takes place but the translation is skipped. This is useful when "stubbing out" content that will require translation in the future:: - {% trans "myvar" noop %} + {% translate "myvar" noop %} Internally, inline translations use an :func:`~django.utils.translation.gettext` call. @@ -598,15 +599,14 @@ In case a template var (``myvar`` above) is passed to the tag, the tag will first resolve such variable to a string at run-time and then look up that string in the message catalogs. -It's not possible to mix a template variable inside a string within ``{% trans -%}``. If your translations require strings with variables (placeholders), use -:ttag:`{% blocktrans %}` instead. - +It's not possible to mix a template variable inside a string within +``{% translate %}``. If your translations require strings with variables +(placeholders), use :ttag:`{% blocktranslate %}` instead. If you'd like to retrieve a translated string without displaying it, you can use the following syntax:: - {% trans "This is the title" as the_title %} + {% translate "This is the title" as the_title %} {{ the_title }} @@ -615,12 +615,12 @@ In practice you'll use this to get a string you can use in multiple places in a template or so you can use the output as an argument for other template tags or filters:: - {% trans "starting point" as start %} - {% trans "end point" as end %} - {% trans "La Grande Boucle" as race %} + {% translate "starting point" as start %} + {% translate "end point" as end %} + {% translate "La Grande Boucle" as race %}

    - {{ race }} + {{ race }}

    {% for stage in tour_stages %} @@ -628,50 +628,56 @@ filters:: {% endfor %}

    -``{% trans %}`` also supports :ref:`contextual markers` +``{% translate %}`` also supports :ref:`contextual markers` using the ``context`` keyword: .. code-block:: html+django - {% trans "May" context "month name" %} + {% translate "May" context "month name" %} + +.. versionchanged:: 3.1 + + The ``trans`` tag was renamed to ``translate``. The ``trans`` + tag is still supported as an alias for backwards compatibility. .. templatetag:: blocktrans +.. templatetag:: blocktranslate -``blocktrans`` template tag ---------------------------- +``blocktranslate`` template tag +------------------------------- -Contrarily to the :ttag:`trans` tag, the ``blocktrans`` tag allows you to mark -complex sentences consisting of literals and variable content for translation -by making use of placeholders:: +Contrarily to the :ttag:`translate` tag, the ``blocktranslate`` tag allows you +to mark complex sentences consisting of literals and variable content for +translation by making use of placeholders:: - {% blocktrans %}This string will have {{ value }} inside.{% endblocktrans %} + {% blocktranslate %}This string will have {{ value }} inside.{% endblocktranslate %} To translate a template expression -- say, accessing object attributes or using template filters -- you need to bind the expression to a local variable for use within the translation block. Examples:: - {% blocktrans with amount=article.price %} + {% blocktranslate with amount=article.price %} That will cost $ {{ amount }}. - {% endblocktrans %} + {% endblocktranslate %} - {% blocktrans with myvar=value|filter %} + {% blocktranslate with myvar=value|filter %} This will have {{ myvar }} inside. - {% endblocktrans %} + {% endblocktranslate %} -You can use multiple expressions inside a single ``blocktrans`` tag:: +You can use multiple expressions inside a single ``blocktranslate`` tag:: - {% blocktrans with book_t=book|title author_t=author|title %} + {% blocktranslate with book_t=book|title author_t=author|title %} This is {{ book_t }} by {{ author_t }} - {% endblocktrans %} + {% endblocktranslate %} .. note:: The previous more verbose format is still supported: - ``{% blocktrans with book|title as book_t and author|title as author_t %}`` + ``{% blocktranslate with book|title as book_t and author|title as author_t %}`` Other block tags (for example ``{% for %}`` or ``{% if %}``) are not allowed -inside a ``blocktrans`` tag. +inside a ``blocktranslate`` tag. -If resolving one of the block arguments fails, ``blocktrans`` will fall back to -the default language by deactivating the currently active language +If resolving one of the block arguments fails, ``blocktranslate`` will fall +back to the default language by deactivating the currently active language temporarily with the :func:`~django.utils.translation.deactivate_all` function. @@ -681,43 +687,43 @@ This tag also provides for pluralization. To use it: be the one used to select the right plural form. * Specify both the singular and plural forms separating them with the - ``{% plural %}`` tag within the ``{% blocktrans %}`` and - ``{% endblocktrans %}`` tags. + ``{% plural %}`` tag within the ``{% blocktranslate %}`` and + ``{% endblocktranslate %}`` tags. An example:: - {% blocktrans count counter=list|length %} + {% blocktranslate count counter=list|length %} There is only one {{ name }} object. {% plural %} There are {{ counter }} {{ name }} objects. - {% endblocktrans %} + {% endblocktranslate %} A more complex example:: - {% blocktrans with amount=article.price count years=i.length %} + {% blocktranslate with amount=article.price count years=i.length %} That will cost $ {{ amount }} per year. {% plural %} That will cost $ {{ amount }} per {{ years }} years. - {% endblocktrans %} + {% endblocktranslate %} When you use both the pluralization feature and bind values to local variables -in addition to the counter value, keep in mind that the ``blocktrans`` +in addition to the counter value, keep in mind that the ``blocktranslate`` construct is internally converted to an ``ngettext`` call. This means the same :ref:`notes regarding ngettext variables ` apply. -Reverse URL lookups cannot be carried out within the ``blocktrans`` and should -be retrieved (and stored) beforehand:: +Reverse URL lookups cannot be carried out within the ``blocktranslate`` and +should be retrieved (and stored) beforehand:: {% url 'path.to.view' arg arg2 as the_url %} - {% blocktrans %} + {% blocktranslate %} This is a URL: {{ the_url }} - {% endblocktrans %} + {% endblocktranslate %} If you'd like to retrieve a translated string without displaying it, you can use the following syntax:: - {% blocktrans asvar the_title %}The title is {{ title }}.{% endblocktrans %} + {% blocktranslate asvar the_title %}The title is {{ title }}.{% endblocktranslate %} {{ the_title }} @@ -725,32 +731,38 @@ In practice you'll use this to get a string you can use in multiple places in a template or so you can use the output as an argument for other template tags or filters. -``{% blocktrans %}`` also supports :ref:`contextual +``{% blocktranslate %}`` also supports :ref:`contextual markers` using the ``context`` keyword: .. code-block:: html+django - {% blocktrans with name=user.username context "greeting" %}Hi {{ name }}{% endblocktrans %} + {% blocktranslate with name=user.username context "greeting" %}Hi {{ name }}{% endblocktranslate %} -Another feature ``{% blocktrans %}`` supports is the ``trimmed`` option. This -option will remove newline characters from the beginning and the end of the -content of the ``{% blocktrans %}`` tag, replace any whitespace at the beginning -and end of a line and merge all lines into one using a space character to -separate them. This is quite useful for indenting the content of a ``{% -blocktrans %}`` tag without having the indentation characters end up in the -corresponding entry in the PO file, which makes the translation process easier. +Another feature ``{% blocktranslate %}`` supports is the ``trimmed`` option. +This option will remove newline characters from the beginning and the end of +the content of the ``{% blocktranslate %}`` tag, replace any whitespace at the +beginning and end of a line and merge all lines into one using a space +character to separate them. This is quite useful for indenting the content of a +``{% blocktranslate %}`` tag without having the indentation characters end up +in the corresponding entry in the PO file, which makes the translation process +easier. -For instance, the following ``{% blocktrans %}`` tag:: +For instance, the following ``{% blocktranslate %}`` tag:: - {% blocktrans trimmed %} + {% blocktranslate trimmed %} First sentence. Second paragraph. - {% endblocktrans %} + {% endblocktranslate %} will result in the entry ``"First sentence. Second paragraph."`` in the PO file, compared to ``"\n First sentence.\n Second sentence.\n"``, if the ``trimmed`` option had not been specified. +.. versionchanged:: 3.1 + + The ``blocktrans`` tag was renamed to ``blocktranslate``. The ``blocktrans`` + tag is still supported as an alias for backwards compatibility. + String literals passed to tags and filters ------------------------------------------ @@ -782,21 +794,21 @@ tag: .. code-block:: html+django {% comment %}Translators: View verb{% endcomment %} - {% trans "View" %} + {% translate "View" %} {% comment %}Translators: Short intro blurb{% endcomment %} -

    {% blocktrans %}A multiline translatable - literal.{% endblocktrans %}

    +

    {% blocktranslate %}A multiline translatable + literal.{% endblocktranslate %}

    or with the ``{#`` ... ``#}`` :ref:`one-line comment constructs `: .. code-block:: html+django {# Translators: Label of a button that triggers search #} - + {# Translators: This is a text of the base template #} - {% blocktrans %}Ambiguous translatable block of text{% endblocktrans %} + {% blocktranslate %}Ambiguous translatable block of text{% endblocktranslate %} .. note:: Just for completeness, these are the corresponding fragments of the resulting ``.po`` file: @@ -841,12 +853,12 @@ If you want to select a language within a template, you can use the {% get_current_language as LANGUAGE_CODE %} -

    {% trans "Welcome to our page" %}

    +

    {% translate "Welcome to our page" %}

    {% language 'en' %} {% get_current_language as LANGUAGE_CODE %} -

    {% trans "Welcome to our page" %}

    +

    {% translate "Welcome to our page" %}

    {% endlanguage %} While the first occurrence of "Welcome to our page" uses the current language, @@ -1450,7 +1462,7 @@ template tag. It enables the given language in the enclosed template section: {% get_available_languages as languages %} - {% trans "View this category in:" %} + {% translate "View this category in:" %} {% for lang_code, lang_name in languages %} {% language lang_code %} {{ lang_name }} 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"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_blocktrans.py deleted file mode 100644 index 744b410ea6..0000000000 --- a/tests/template_tests/syntax_tests/i18n/test_blocktrans.py +++ /dev/null @@ -1,475 +0,0 @@ -import os - -from asgiref.local import Local - -from django.template import Context, Template, TemplateSyntaxError -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 - -from ...utils import setup -from .base import MultipleLocaleActivationTestCase, extended_locale_paths, here - - -class I18nBlockTransTagTests(SimpleTestCase): - libraries = {'i18n': 'django.templatetags.i18n'} - - @setup({'i18n03': '{% load i18n %}{% blocktrans %}{{ anton }}{% endblocktrans %}'}) - def test_i18n03(self): - """simple translation of a variable""" - output = self.engine.render_to_string('i18n03', {'anton': 'Å'}) - 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 = self.engine.render_to_string('i18n04', {'anton': 'Å'}) - 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 = self.engine.render_to_string('legacyi18n04', {'anton': 'Å'}) - self.assertEqual(output, 'å') - - @setup({'i18n05': '{% load i18n %}{% blocktrans %}xxx{{ anton }}xxx{% endblocktrans %}'}) - def test_i18n05(self): - """simple translation of a string with interpolation""" - output = self.engine.render_to_string('i18n05', {'anton': 'yyy'}) - self.assertEqual(output, 'xxxyyyxxx') - - @setup({'i18n07': '{% load i18n %}' - '{% blocktrans count counter=number %}singular{% plural %}' - '{{ counter }} plural{% endblocktrans %}'}) - def test_i18n07(self): - """translation of singular form""" - output = self.engine.render_to_string('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 = self.engine.render_to_string('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 = self.engine.render_to_string('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 = self.engine.render_to_string('legacyi18n08', {'number': 2}) - self.assertEqual(output, '2 plural') - - @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 = self.engine.render_to_string('i18n17', {'anton': 'α & β'}) - self.assertEqual(output, 'α & β') - - @setup({'i18n18': '{% load i18n %}' - '{% blocktrans with berta=anton|force_escape %}{{ berta }}{% endblocktrans %}'}) - def test_i18n18(self): - output = self.engine.render_to_string('i18n18', {'anton': 'α & β'}) - self.assertEqual(output, 'α & β') - - @setup({'i18n19': '{% load i18n %}{% blocktrans %}{{ andrew }}{% endblocktrans %}'}) - def test_i18n19(self): - output = self.engine.render_to_string('i18n19', {'andrew': 'a & b'}) - self.assertEqual(output, 'a & b') - - @setup({'i18n21': '{% load i18n %}{% blocktrans %}{{ andrew }}{% endblocktrans %}'}) - def test_i18n21(self): - output = self.engine.render_to_string('i18n21', {'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 = self.engine.render_to_string('legacyi18n17', {'anton': 'α & β'}) - self.assertEqual(output, 'α & β') - - @setup({'legacyi18n18': '{% load i18n %}' - '{% blocktrans with anton|force_escape as berta %}' - '{{ berta }}{% endblocktrans %}'}) - def test_legacyi18n18(self): - output = self.engine.render_to_string('legacyi18n18', {'anton': 'α & β'}) - self.assertEqual(output, 'α & β') - - @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 = self.engine.render_to_string('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 = self.engine.render_to_string('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)""" - 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({'legacyi18n27': '{% load i18n %}' - '{% blocktrans count number as counter %}{{ counter }} result' - '{% plural %}{{ counter }} results{% endblocktrans %}'}) - 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({'i18n28': '{% load i18n %}' - '{% blocktrans with a=anton b=berta %}{{ a }} + {{ b }}{% endblocktrans %}'}) - def test_i18n28(self): - """simple translation of multiple variables""" - output = self.engine.render_to_string('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 = self.engine.render_to_string('legacyi18n28', {'anton': 'α', 'berta': 'β'}) - self.assertEqual(output, 'α + β') - - # 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 = self.engine.render_to_string('i18n34') - if self.engine.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 = self.engine.render_to_string('i18n34_2') - if self.engine.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 = self.engine.render_to_string( - 'i18n34_3', {'anton': '\xce\xb1'}) - if self.engine.string_if_invalid: - self.assertEqual(output, 'INVALID') - else: - self.assertEqual(output, '') - - @setup({'i18n37': '{% load i18n %}' - '{% trans "Page not found" as page_not_found %}' - '{% blocktrans %}Error: {{ page_not_found }}{% endblocktrans %}'}) - def test_i18n37(self): - with translation.override('de'): - output = self.engine.render_to_string('i18n37') - self.assertEqual(output, 'Error: Seite nicht gefunden') - - # blocktrans tag with asvar - @setup({'i18n39': '{% load i18n %}' - '{% blocktrans asvar page_not_found %}Page not found{% endblocktrans %}' - '>{{ page_not_found }}<'}) - def test_i18n39(self): - with translation.override('de'): - output = self.engine.render_to_string('i18n39') - self.assertEqual(output, '>Seite nicht gefunden<') - - @setup({'i18n40': '{% load i18n %}' - '{% trans "Page not found" as pg_404 %}' - '{% blocktrans with page_not_found=pg_404 asvar output %}' - 'Error: {{ page_not_found }}' - '{% endblocktrans %}'}) - def test_i18n40(self): - output = self.engine.render_to_string('i18n40') - self.assertEqual(output, '') - - @setup({'i18n41': '{% load i18n %}' - '{% trans "Page not found" as pg_404 %}' - '{% blocktrans with page_not_found=pg_404 asvar output %}' - 'Error: {{ page_not_found }}' - '{% endblocktrans %}' - '>{{ output }}<'}) - def test_i18n41(self): - with translation.override('de'): - output = self.engine.render_to_string('i18n41') - 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." - with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('template') - - @setup({'template': '{% load i18n %}{% blocktrans %}%s{% endblocktrans %}'}) - def test_blocktrans_tag_using_a_string_that_looks_like_str_fmt(self): - output = self.engine.render_to_string('template') - 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" - 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" - with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('template') - - @setup({'template': '{% load i18n %}{% blocktrans with foo=bar with %}{{ foo }}{% endblocktrans %}'}) - 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'}) - - @setup({'template': '{% load i18n %}{% blocktrans with %}{% endblocktrans %}'}) - def test_no_args_with(self): - msg = '"with" in \'blocktrans\' tag needs at least one keyword argument.' - 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.' - with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('template', {'a': [1, 2, 3]}) - - @setup({'template': ( - '{% load i18n %}{% blocktrans count count=var|length %}' - 'There is {{ count }} object. {% block a %} {% endblock %}' - '{% endblocktrans %}' - )}) - def test_plural_bad_syntax(self): - msg = "'blocktrans' doesn't allow other block tags inside it" - with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('template', {'var': [1, 2, 3]}) - - -class TranslationBlockTransTagTests(SimpleTestCase): - - @override_settings(LOCALE_PATHS=extended_locale_paths) - def test_template_tags_pgettext(self): - """{% blocktrans %} takes message contexts into account (#14806).""" - trans_real._active = Local() - trans_real._translations = {} - with translation.override('de'): - # Nonexistent context - t = 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 %}') - rendered = t.render(Context()) - self.assertEqual(rendered, 'Mai') - t = 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 %}') - rendered = t.render(Context({'message_context': 'month name'})) - self.assertEqual(rendered, 'Mai') - t = 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 %}') - rendered = t.render(Context({'message_context': 'MONTH NAME'})) - self.assertEqual(rendered, 'Mai') - t = 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( - '{% 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( - '{% 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( - '{% 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( - '{% load i18n %}{% blocktrans context "other super search" count number=2 %}' - '{{ number }} super result{% plural %}{{ number }} super results{% endblocktrans %}' - ) - rendered = t.render(Context()) - self.assertEqual(rendered, '2 andere Super-Ergebnisse') - - # Using 'with' - t = 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( - '{% load i18n %}{% blocktrans with num_comments=5 context "other comment count" %}' - 'There are {{ num_comments }} comments{% endblocktrans %}' - ) - rendered = t.render(Context()) - self.assertEqual(rendered, 'Andere: Es gibt 5 Kommentare') - - # Using trimmed - t = 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( - '{% 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( - '{% load i18n %}{% blocktrans context "other super search" count number=2 trimmed %}\n' - '{{ number }} super \n result{% plural %}{{ number }} super results{% endblocktrans %}' - ) - rendered = t.render(Context()) - self.assertEqual(rendered, '2 andere Super-Ergebnisse') - - # 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 %}') - msg = '"context" in %r tag expected exactly one argument.' % 'blocktrans' - with self.assertRaisesMessage(TemplateSyntaxError, msg): - Template('{% load i18n %}{% blocktrans context %}{% endblocktrans %}') - with self.assertRaisesMessage(TemplateSyntaxError, msg): - Template( - '{% load i18n %}{% blocktrans count number=2 context %}' - '{{ number }} super result{% plural %}{{ number }}' - ' super results{% endblocktrans %}' - ) - - @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'): - t = Template('{% load i18n %}{% blocktrans %}My name is {{ person }}.{% endblocktrans %}') - 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'): - t = Template('{% load i18n %}{% blocktrans %}My other name is {{ person }}.{% endblocktrans %}') - rendered = t.render(Context({'person': 'James'})) - self.assertEqual(rendered, 'My other name is James.') - - -class MultipleLocaleActivationBlockTransTests(MultipleLocaleActivationTestCase): - - def test_single_locale_activation(self): - """ - Simple baseline behavior with one locale for all the supported i18n - constructs. - """ - with translation.override('fr'): - self.assertEqual( - 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 %}") - 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 %}") - 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 %}") - with translation.override('nl'): - self.assertEqual(t.render(Context({})), 'Nee') - - -class MiscTests(SimpleTestCase): - - @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( - "{% load i18n %}{% blocktrans count num as number %}" - "{{ percent }}% represents {{ num }} object{% plural %}" - "{{ percent }}% represents {{ num }} objects{% endblocktrans %}" - ) - 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) - def test_percent_formatting_in_blocktrans(self): - """ - Python's %-formatting is properly escaped in blocktrans, singular, or - plural. - """ - t_sing = Template("{% load i18n %}{% blocktrans %}There are %(num_comments)s comments{% endblocktrans %}") - t_plur = Template( - "{% load i18n %}{% blocktrans count num as number %}" - "%(percent)s% represents {{ num }} object{% plural %}" - "%(percent)s% represents {{ num }} objects{% endblocktrans %}" - ) - 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') diff --git a/tests/template_tests/syntax_tests/i18n/test_blocktranslate.py b/tests/template_tests/syntax_tests/i18n/test_blocktranslate.py new file mode 100644 index 0000000000..11bd456ea8 --- /dev/null +++ b/tests/template_tests/syntax_tests/i18n/test_blocktranslate.py @@ -0,0 +1,562 @@ +import inspect +import os +from functools import partial, wraps + +from asgiref.local import Local + +from django.template import Context, Template, TemplateSyntaxError +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 + +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'} + + @setup({'i18n03': '{% load i18n %}{% blocktrans %}{{ anton }}{% endblocktrans %}'}) + def test_i18n03(self): + """simple translation of a variable""" + output = self.engine.render_to_string('i18n03', {'anton': 'Å'}) + 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 = self.engine.render_to_string('i18n04', {'anton': 'Å'}) + 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 = self.engine.render_to_string('legacyi18n04', {'anton': 'Å'}) + self.assertEqual(output, 'å') + + @setup({'i18n05': '{% load i18n %}{% blocktrans %}xxx{{ anton }}xxx{% endblocktrans %}'}) + def test_i18n05(self): + """simple translation of a string with interpolation""" + output = self.engine.render_to_string('i18n05', {'anton': 'yyy'}) + self.assertEqual(output, 'xxxyyyxxx') + + @setup({'i18n07': '{% load i18n %}' + '{% blocktrans count counter=number %}singular{% plural %}' + '{{ counter }} plural{% endblocktrans %}'}) + def test_i18n07(self): + """translation of singular form""" + output = self.engine.render_to_string('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 = self.engine.render_to_string('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 = self.engine.render_to_string('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 = self.engine.render_to_string('legacyi18n08', {'number': 2}) + self.assertEqual(output, '2 plural') + + @setup({'i18n17': '{% load i18n %}' + '{% blocktrans with berta=anton|escape %}{{ berta }}{% endblocktrans %}'}) + def test_i18n17(self): + """ + 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, 'α & β') + + @setup({'i18n18': '{% load i18n %}' + '{% blocktrans with berta=anton|force_escape %}{{ berta }}{% endblocktrans %}'}) + def test_i18n18(self): + output = self.engine.render_to_string('i18n18', {'anton': 'α & β'}) + self.assertEqual(output, 'α & β') + + @setup({'i18n19': '{% load i18n %}{% blocktrans %}{{ andrew }}{% endblocktrans %}'}) + def test_i18n19(self): + output = self.engine.render_to_string('i18n19', {'andrew': 'a & b'}) + self.assertEqual(output, 'a & b') + + @setup({'i18n21': '{% load i18n %}{% blocktrans %}{{ andrew }}{% endblocktrans %}'}) + def test_i18n21(self): + output = self.engine.render_to_string('i18n21', {'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 = self.engine.render_to_string('legacyi18n17', {'anton': 'α & β'}) + self.assertEqual(output, 'α & β') + + @setup({'legacyi18n18': '{% load i18n %}' + '{% blocktrans with anton|force_escape as berta %}' + '{{ berta }}{% endblocktrans %}'}) + def test_legacyi18n18(self): + output = self.engine.render_to_string('legacyi18n18', {'anton': 'α & β'}) + self.assertEqual(output, 'α & β') + + @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 = self.engine.render_to_string('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 = self.engine.render_to_string('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)""" + 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({'legacyi18n27': '{% load i18n %}' + '{% blocktrans count number as counter %}{{ counter }} result' + '{% plural %}{{ counter }} results{% endblocktrans %}'}) + 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({'i18n28': '{% load i18n %}' + '{% blocktrans with a=anton b=berta %}{{ a }} + {{ b }}{% endblocktrans %}'}) + def test_i18n28(self): + """simple translation of multiple variables""" + output = self.engine.render_to_string('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 = self.engine.render_to_string('legacyi18n28', {'anton': 'α', 'berta': 'β'}) + self.assertEqual(output, 'α + β') + + # 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 = self.engine.render_to_string('i18n34') + if self.engine.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 = self.engine.render_to_string('i18n34_2') + if self.engine.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 = self.engine.render_to_string( + 'i18n34_3', {'anton': '\xce\xb1'}) + if self.engine.string_if_invalid: + self.assertEqual(output, 'INVALID') + else: + self.assertEqual(output, '') + + @setup({'i18n37': '{% load i18n %}' + '{% trans "Page not found" as page_not_found %}' + '{% blocktrans %}Error: {{ page_not_found }}{% endblocktrans %}'}) + def test_i18n37(self): + with translation.override('de'): + output = self.engine.render_to_string('i18n37') + self.assertEqual(output, 'Error: Seite nicht gefunden') + + # blocktrans tag with asvar + @setup({'i18n39': '{% load i18n %}' + '{% blocktrans asvar page_not_found %}Page not found{% endblocktrans %}' + '>{{ page_not_found }}<'}) + def test_i18n39(self): + with translation.override('de'): + output = self.engine.render_to_string('i18n39') + self.assertEqual(output, '>Seite nicht gefunden<') + + @setup({'i18n40': '{% load i18n %}' + '{% trans "Page not found" as pg_404 %}' + '{% blocktrans with page_not_found=pg_404 asvar output %}' + 'Error: {{ page_not_found }}' + '{% endblocktrans %}'}) + def test_i18n40(self): + output = self.engine.render_to_string('i18n40') + self.assertEqual(output, '') + + @setup({'i18n41': '{% load i18n %}' + '{% trans "Page not found" as pg_404 %}' + '{% blocktrans with page_not_found=pg_404 asvar output %}' + 'Error: {{ page_not_found }}' + '{% endblocktrans %}' + '>{{ output }}<'}) + def test_i18n41(self): + with translation.override('de'): + output = self.engine.render_to_string('i18n41') + self.assertEqual(output, '>Error: Seite nicht gefunden<') + + @setup({'template': '{% load i18n %}{% blocktrans asvar %}Yes{% endblocktrans %}'}) + 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') + + @setup({'template': '{% load i18n %}{% blocktrans %}%s{% endblocktrans %}'}) + def test_blocktrans_tag_using_a_string_that_looks_like_str_fmt(self): + output = self.engine.render_to_string('template') + self.assertEqual(output, '%s') + + @setup({'template': '{% load i18n %}{% blocktrans %}{% block b %} {% endblock %}{% endblocktrans %}'}) + 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, 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') + + @setup({'template': '{% load i18n %}{% blocktrans with foo=bar with %}{{ foo }}{% endblocktrans %}'}) + 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'}) + + @setup({'template': '{% load i18n %}{% blocktrans with %}{% endblocktrans %}'}) + 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, 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]}) + + @setup({'template': ( + '{% load i18n %}{% blocktrans count count=var|length %}' + 'There is {{ count }} object. {% block a %} {% endblock %}' + '{% endblocktrans %}' + )}) + 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): + """{% blocktrans %} takes message contexts into account (#14806).""" + trans_real._active = Local() + trans_real._translations = {} + with translation.override('de'): + # Nonexistent context + 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 = self.get_template('{% load i18n %}{% blocktrans context "month name" %}May{% endblocktrans %}') + rendered = t.render(Context()) + self.assertEqual(rendered, 'Mai') + t = self.get_template('{% load i18n %}{% blocktrans context "verb" %}May{% endblocktrans %}') + rendered = t.render(Context()) + self.assertEqual(rendered, 'Kann') + + # Using a variable + 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 = 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 = 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 = 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 = 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 = 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 = 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 = self.get_template( + '{% load i18n %}{% blocktrans context "other super search" count number=2 %}' + '{{ number }} super result{% plural %}{{ number }} super results{% endblocktrans %}' + ) + rendered = t.render(Context()) + self.assertEqual(rendered, '2 andere Super-Ergebnisse') + + # Using 'with' + 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 = self.get_template( + '{% load i18n %}{% blocktrans with num_comments=5 context "other comment count" %}' + 'There are {{ num_comments }} comments{% endblocktrans %}' + ) + rendered = t.render(Context()) + self.assertEqual(rendered, 'Andere: Es gibt 5 Kommentare') + + # Using trimmed + 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 = 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 = self.get_template( + '{% load i18n %}{% blocktrans context "other super search" count number=2 trimmed %}\n' + '{{ number }} super \n result{% plural %}{{ number }} super results{% endblocktrans %}' + ) + rendered = t.render(Context()) + self.assertEqual(rendered, '2 andere Super-Ergebnisse') + + # Misuses + msg = "Unknown argument for 'blocktrans' tag: %r." + with self.assertRaisesMessage(TemplateSyntaxError, msg % 'month="May"'): + 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): + self.get_template('{% load i18n %}{% blocktrans context %}{% endblocktrans %}') + with self.assertRaisesMessage(TemplateSyntaxError, msg): + self.get_template( + '{% load i18n %}{% blocktrans count number=2 context %}' + '{{ number }} super result{% plural %}{{ number }}' + ' super results{% endblocktrans %}' + ) + + @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'): + t = Template('{% load i18n %}{% blocktrans %}My name is {{ person }}.{% endblocktrans %}') + 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'): + t = Template('{% load i18n %}{% blocktrans %}My other name is {{ person }}.{% endblocktrans %}') + rendered = t.render(Context({'person': 'James'})) + 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): + """ + Simple baseline behavior with one locale for all the supported i18n + constructs. + """ + with translation.override('fr'): + self.assertEqual( + self.get_template("{% load i18n %}{% blocktrans %}Yes{% endblocktrans %}").render(Context({})), + 'Oui' + ) + + def test_multiple_locale_btrans(self): + with translation.override('de'): + 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 = 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 = 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 = 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 %}" + ) + 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) + def test_percent_formatting_in_blocktranslate(self): + """ + Python's %-formatting is properly escaped in blocktranslate, singular, + or plural. + """ + 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 %}" + ) + 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') + + +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_trans.py deleted file mode 100644 index 47a79ff74d..0000000000 --- a/tests/template_tests/syntax_tests/i18n/test_trans.py +++ /dev/null @@ -1,212 +0,0 @@ -from asgiref.local import Local - -from django.template import Context, Template, TemplateSyntaxError -from django.templatetags.l10n import LocalizeNode -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 - -from ...utils import setup -from .base import MultipleLocaleActivationTestCase, extended_locale_paths - - -class I18nTransTagTests(SimpleTestCase): - libraries = {'i18n': 'django.templatetags.i18n'} - - @setup({'i18n01': '{% load i18n %}{% trans \'xxxyyyxxx\' %}'}) - def test_i18n01(self): - """simple translation of a string delimited by '.""" - output = self.engine.render_to_string('i18n01') - self.assertEqual(output, 'xxxyyyxxx') - - @setup({'i18n02': '{% load i18n %}{% trans "xxxyyyxxx" %}'}) - def test_i18n02(self): - """simple translation of a string delimited by ".""" - output = self.engine.render_to_string('i18n02') - self.assertEqual(output, 'xxxyyyxxx') - - @setup({'i18n06': '{% load i18n %}{% trans "Page not found" %}'}) - 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') - - @setup({'i18n09': '{% load i18n %}{% trans "Page not found" noop %}'}) - 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') - - @setup({'i18n20': '{% load i18n %}{% trans andrew %}'}) - def test_i18n20(self): - output = self.engine.render_to_string('i18n20', {'andrew': 'a & b'}) - self.assertEqual(output, 'a & b') - - @setup({'i18n22': '{% load i18n %}{% trans andrew %}'}) - def test_i18n22(self): - output = self.engine.render_to_string('i18n22', {'andrew': mark_safe('a & b')}) - self.assertEqual(output, 'a & b') - - @setup({'i18n23': '{% load i18n %}{% trans "Page not found"|capfirst|slice:"6:" %}'}) - def test_i18n23(self): - """Using filters with the {% trans %} tag (#5972).""" - with translation.override('de'): - output = self.engine.render_to_string('i18n23') - self.assertEqual(output, 'nicht gefunden') - - @setup({'i18n24': '{% load i18n %}{% trans \'Page not found\'|upper %}'}) - def test_i18n24(self): - with translation.override('de'): - output = self.engine.render_to_string('i18n24') - self.assertEqual(output, 'SEITE NICHT GEFUNDEN') - - @setup({'i18n25': '{% load i18n %}{% trans somevar|upper %}'}) - 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 - @setup({'i18n35': '{% load i18n %}{% trans "Page not found" as page_not_found %}{{ page_not_found }}'}) - 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 %}' - '{% trans "Page not found" noop as page_not_found %}{{ page_not_found }}'}) - def test_i18n36(self): - with translation.override('de'): - output = self.engine.render_to_string('i18n36') - 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" - 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'" - 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." - 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." - 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" - 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" - with self.assertRaisesMessage(TemplateSyntaxError, msg): - self.engine.render_to_string('template') - - @setup({'template': '{% load i18n %}{% trans "Yes" noop noop %}'}) - 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') - - @setup({'template': '{% load i18n %}{% trans "%s" %}'}) - 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): - - @override_settings(LOCALE_PATHS=extended_locale_paths) - def test_template_tags_pgettext(self): - """{% trans %} takes message contexts into account (#14806).""" - trans_real._active = Local() - trans_real._translations = {} - with translation.override('de'): - # Nonexistent context... - t = 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" %}') - rendered = t.render(Context()) - self.assertEqual(rendered, 'Mai') - t = 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 %}') - rendered = t.render(Context({'message_context': 'month name'})) - self.assertEqual(rendered, 'Mai') - t = 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 %}') - rendered = t.render(Context({'message_context': 'MONTH NAME'})) - self.assertEqual(rendered, 'Mai') - t = 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 }}') - rendered = t.render(Context()) - self.assertEqual(rendered, 'Value: Mai') - t = Template('{% load i18n %}{% trans "May" as var context "verb" %}Value: {{ var }}') - rendered = t.render(Context()) - self.assertEqual(rendered, 'Value: Kann') - - -class MultipleLocaleActivationTransTagTests(MultipleLocaleActivationTestCase): - - def test_single_locale_activation(self): - """ - Simple baseline behavior with one locale for all the supported i18n - constructs. - """ - with translation.override('fr'): - self.assertEqual(Template("{% load i18n %}{% trans 'Yes' %}").render(Context({})), 'Oui') - - def test_multiple_locale_trans(self): - with translation.override('de'): - t = 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' %}") - 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' %}") - with translation.override('nl'): - self.assertEqual(t.render(Context({})), 'Nee') - - -class LocalizeNodeTests(SimpleTestCase): - def test_repr(self): - node = LocalizeNode(nodelist=[], use_l10n=True) - self.assertEqual(repr(node), '') diff --git a/tests/template_tests/syntax_tests/i18n/test_translate.py b/tests/template_tests/syntax_tests/i18n/test_translate.py new file mode 100644 index 0000000000..6011153a2d --- /dev/null +++ b/tests/template_tests/syntax_tests/i18n/test_translate.py @@ -0,0 +1,269 @@ +import inspect +from functools import partial, wraps + +from asgiref.local import Local + +from django.template import Context, Template, TemplateSyntaxError +from django.templatetags.l10n import LocalizeNode +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 + +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'} + + @setup({'i18n01': '{% load i18n %}{% trans \'xxxyyyxxx\' %}'}) + def test_i18n01(self): + """simple translation of a string delimited by '.""" + output = self.engine.render_to_string('i18n01') + self.assertEqual(output, 'xxxyyyxxx') + + @setup({'i18n02': '{% load i18n %}{% trans "xxxyyyxxx" %}'}) + def test_i18n02(self): + """simple translation of a string delimited by ".""" + output = self.engine.render_to_string('i18n02') + self.assertEqual(output, 'xxxyyyxxx') + + @setup({'i18n06': '{% load i18n %}{% trans "Page not found" %}'}) + 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') + + @setup({'i18n09': '{% load i18n %}{% trans "Page not found" noop %}'}) + 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') + + @setup({'i18n20': '{% load i18n %}{% trans andrew %}'}) + def test_i18n20(self): + output = self.engine.render_to_string('i18n20', {'andrew': 'a & b'}) + self.assertEqual(output, 'a & b') + + @setup({'i18n22': '{% load i18n %}{% trans andrew %}'}) + def test_i18n22(self): + output = self.engine.render_to_string('i18n22', {'andrew': mark_safe('a & b')}) + self.assertEqual(output, 'a & b') + + @setup({'i18n23': '{% load i18n %}{% trans "Page not found"|capfirst|slice:"6:" %}'}) + def test_i18n23(self): + """Using filters with the {% trans %} tag (#5972).""" + with translation.override('de'): + output = self.engine.render_to_string('i18n23') + self.assertEqual(output, 'nicht gefunden') + + @setup({'i18n24': '{% load i18n %}{% trans \'Page not found\'|upper %}'}) + def test_i18n24(self): + with translation.override('de'): + output = self.engine.render_to_string('i18n24') + self.assertEqual(output, 'SEITE NICHT GEFUNDEN') + + @setup({'i18n25': '{% load i18n %}{% trans somevar|upper %}'}) + 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 + @setup({'i18n35': '{% load i18n %}{% trans "Page not found" as page_not_found %}{{ page_not_found }}'}) + 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 %}' + '{% trans "Page not found" noop as page_not_found %}{{ page_not_found }}'}) + def test_i18n36(self): + with translation.override('de'): + output = self.engine.render_to_string('i18n36') + self.assertEqual(output, 'Page not found') + + @setup({'template': '{% load i18n %}{% trans %}A}'}) + 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, 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, 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, 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, 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, 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') + + @setup({'template': '{% load i18n %}{% trans "Yes" noop noop %}'}) + 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') + + @setup({'template': '{% load i18n %}{% trans "%s" %}'}) + 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): + 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): + """{% trans %} takes message contexts into account (#14806).""" + trans_real._active = Local() + trans_real._translations = {} + with translation.override('de'): + # Nonexistent context... + t = self.get_template('{% load i18n %}{% trans "May" context "nonexistent" %}') + rendered = t.render(Context()) + self.assertEqual(rendered, 'May') + + # Existing context... using a literal + t = self.get_template('{% load i18n %}{% trans "May" context "month name" %}') + rendered = t.render(Context()) + self.assertEqual(rendered, 'Mai') + t = self.get_template('{% load i18n %}{% trans "May" context "verb" %}') + rendered = t.render(Context()) + self.assertEqual(rendered, 'Kann') + + # Using a variable + t = self.get_template('{% load i18n %}{% trans "May" context message_context %}') + rendered = t.render(Context({'message_context': 'month name'})) + self.assertEqual(rendered, 'Mai') + 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 = self.get_template('{% load i18n %}{% trans "May" context message_context|lower %}') + rendered = t.render(Context({'message_context': 'MONTH NAME'})) + self.assertEqual(rendered, 'Mai') + 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 = self.get_template('{% load i18n %}{% trans "May" context "month name" as var %}Value: {{ var }}') + rendered = t.render(Context()) + self.assertEqual(rendered, 'Value: Mai') + 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): + """ + Simple baseline behavior with one locale for all the supported i18n + constructs. + """ + with translation.override('fr'): + self.assertEqual( + self.get_template("{% load i18n %}{% trans 'Yes' %}").render(Context({})), + 'Oui' + ) + + def test_multiple_locale_trans(self): + with translation.override('de'): + 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 = 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 = 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) + self.assertEqual(repr(node), '') -- cgit v1.3