Can Django trans tags include HTML tags? For example can I do {% trans "Hold <em><strong>Ctrl</strong></em>" %}
? Or would I have to do {% trans "Hold" %} <em><strong>{% trans "Ctrl" %}</strong></em>"
instead?
The {% trans %} template tag allows you to mark a string, a constant, or variable content for translation. Internally, Django executes gettext() on the given text. This is how to mark a string for translation in a template: {% trans "Text to be translated" %}
(1) First, create the Django template you want to be included in other templates. Create a new folder called includes. This folder will hold all of the HTML files we want to include within other Django templates. Within this folder make a new file named navbar.
Django templates are often used to pass data to JavaScript code. Unfortunately, if implemented incorrectly, this opens up the possibility of HTML injection, and thus XSS (Cross-Site Scripting) attacks.
{% block %}{% endblock %}: This is used to define sections in your templates, so that if another template extends this one, it'll be able to replace whatever html code has been written inside of it. Blocks are identified by their name.
Can we include HTML tags inside the trans
template tags?
No, we should not include HTML tags inside a trans
template tag as you are doing in your 1st approach {% trans "Hold <em><strong>Ctrl</strong></em>" %}
. This is a wrong approach
From the docs:
The
{% trans %}
template tag translates either a constant string (enclosed in single or double quotes) or variable content.It’s not possible to mix a template variable inside a string within
{% trans %}
. If your translations require strings with variables (placeholders), use{% blocktrans %}
instead.
<title>{% trans "This is the title." %}</title> # example 1
<title>{% trans "myvar" noop %}</title> # example 2
Solution-1: Using trans
template tag
Instead of putting HTML code inside the trans
tag, you can do something like below to get the desired result(though this is not the recommended approach).
{% trans "Hold" %} <em><strong>{% trans "Ctrl" %}</strong></em> # using trans tag
Solution-2: Using blocktrans
tag instead
Better option is to use blocktrans
template tag instead of trans
tag to include the HTML tags.
the
blocktrans
tag allows you to mark complex sentences consisting of literals and variable content for translation by making use of placeholders:
You can then just do:
{% blocktrans %}
Hold <em><strong>Ctrl</strong></em>
{% endblocktrans %}
As Rahul said in his answer, one should not include HTML tags inside a trans template tag. However, according to Translating text blocks with Django .. what to do with the HTML? (which I just found), one can put HTML tags inside of blocktrans template tags instead. Thus I don't have to do {% trans "Hold" %} <em><strong>{% trans "Ctrl" %}</strong></em>"
. I was unable to find such instructions in the Django 1.8 docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With