summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcheng <chengyuan.china@gmail.com>2022-07-13 23:11:17 -0700
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-07-14 11:09:19 +0200
commitd4c5d2b52c897ccc07f04482d3f42f976a79223c (patch)
tree0e5d71b862c42826ab1f8ac61e16018d011b84df
parent7faf25d682b8e8f4fd2006eb7dfc71ed2a2193b7 (diff)
Fixed #33631 -- Marked {% blocktranslate asvar %} result as HTML safe.
-rw-r--r--django/templatetags/i18n.py4
-rw-r--r--docs/releases/4.2.txt3
-rw-r--r--docs/topics/i18n/translation.txt5
-rw-r--r--tests/template_tests/syntax_tests/i18n/test_blocktranslate.py16
4 files changed, 26 insertions, 2 deletions
diff --git a/django/templatetags/i18n.py b/django/templatetags/i18n.py
index 3582df1829..8b4824fbbf 100644
--- a/django/templatetags/i18n.py
+++ b/django/templatetags/i18n.py
@@ -5,7 +5,7 @@ from django.template import Library, Node, TemplateSyntaxError, Variable
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()
@@ -198,7 +198,7 @@ class BlockTranslateNode(Node):
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
diff --git a/docs/releases/4.2.txt b/docs/releases/4.2.txt
index a1d2bb96bb..413e9a9471 100644
--- a/docs/releases/4.2.txt
+++ b/docs/releases/4.2.txt
@@ -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
diff --git a/docs/topics/i18n/translation.txt b/docs/topics/i18n/translation.txt
index 650852c50c..5e29b1acb0 100644
--- a/docs/topics/i18n/translation.txt
+++ b/docs/topics/i18n/translation.txt
@@ -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:
diff --git a/tests/template_tests/syntax_tests/i18n/test_blocktranslate.py b/tests/template_tests/syntax_tests/i18n/test_blocktranslate.py
index 4a162362c6..4c9036ba76 100644
--- a/tests/template_tests/syntax_tests/i18n/test_blocktranslate.py
+++ b/tests/template_tests/syntax_tests/i18n/test_blocktranslate.py
@@ -418,6 +418,22 @@ class I18nBlockTransTagTests(SimpleTestCase):
@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": (
"{% load i18n %}{% blocktranslate asvar %}Yes{% endblocktranslate %}"
)