Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Django trans tags include HTML tags?

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?

like image 389
Daniel Avatar asked Sep 28 '15 19:09

Daniel


People also ask

What is trans tag in Django?

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" %}

How do you use include in Django HTML?

(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.

Can you use Django template tags in JavaScript?

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.

What is {% include %} in Django?

{% 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.


2 Answers

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 %}
like image 167
Rahul Gupta Avatar answered Oct 26 '22 21:10

Rahul Gupta


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.

like image 21
Daniel Avatar answered Oct 26 '22 21:10

Daniel