Skip to content

Commit

Permalink
Fixed #33631 -- Marked {% blocktranslate asvar %} result as HTML safe.
Browse files Browse the repository at this point in the history
  • Loading branch information
chengyuan authored and felixxm committed Jul 14, 2022
1 parent 7faf25d commit d4c5d2b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions django/templatetags/i18n.py
Expand Up @@ -5,7 +5,7 @@
from django.template.base import TokenType, render_value_in_context
from django.template.defaulttags import token_kwargs
from django.utils import translation
from django.utils.safestring import SafeData, mark_safe
from django.utils.safestring import SafeData, SafeString, mark_safe

register = Library()

Expand Down Expand Up @@ -198,7 +198,7 @@ def render_value(key):
with translation.override(None):
result = self.render(context, nested=True)
if self.asvar:
context[self.asvar] = result
context[self.asvar] = SafeString(result)
return ""
else:
return result
Expand Down
3 changes: 3 additions & 0 deletions docs/releases/4.2.txt
Expand Up @@ -270,6 +270,9 @@ Miscellaneous
* The undocumented ``django.http.multipartparser.parse_header()`` function is
removed. Use ``django.utils.http.parse_header_parameters()`` instead.

* :ttag:`{% blocktranslate asvar … %}<blocktranslate>` result is now marked as
safe for (HTML) output purposes.

.. _deprecated-features-4.2:

Features deprecated in 4.2
Expand Down
5 changes: 5 additions & 0 deletions docs/topics/i18n/translation.txt
Expand Up @@ -707,6 +707,11 @@ 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.

.. versionchanged:: 4.2

In older versions, ``asvar`` instances weren't marked as safe for (HTML)
output purposes.

``{% blocktranslate %}`` also supports :ref:`contextual
markers<contextual-markers>` using the ``context`` keyword:

Expand Down
16 changes: 16 additions & 0 deletions tests/template_tests/syntax_tests/i18n/test_blocktranslate.py
Expand Up @@ -416,6 +416,22 @@ def test_i18n41(self):
output = self.engine.render_to_string("i18n41")
self.assertEqual(output, ">Error: Seite nicht gefunden<")

@setup(
{
"i18n_asvar_safestring": (
"{% load i18n %}"
"{% blocktranslate asvar the_title %}"
"{{title}}other text"
"{% endblocktranslate %}"
"{{ the_title }}"
)
}
)
def test_i18n_asvar_safestring(self):
context = {"title": "<Main Title>"}
output = self.engine.render_to_string("i18n_asvar_safestring", context=context)
self.assertEqual(output, "&lt;Main Title&gt;other text")

@setup(
{
"template": (
Expand Down

0 comments on commit d4c5d2b

Please sign in to comment.