Ctrl<...">

Can Django Broadcast Tags Include HTML Tags?

Can Django tag 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?

+6
source share
2 answers

Can I include HTML tags inside trans template tags?

No, we should not include HTML tags inside the trans tag, as you do in your 1st approach {% trans "Hold <em><strong>Ctrl</strong></em>" %} . This is the wrong approach.

From docs:

The template tag {% trans %} translates to either a constant string (enclosed in single or double quotes) or variable content.

Cannot mix template variable inside string inside {% trans %} . If your translations require variable strings (placeholders) use {% blocktrans %} .

 <title>{% trans "This is the title." %}</title> # example 1 <title>{% trans "myvar" noop %}</title> # example 2 

Solution 1: use the trans tag

Instead of putting the HTML inside the trans tag, you can do something like below to get the desired result (although this is not the recommended approach).

 {% trans "Hold" %} <em><strong>{% trans "Ctrl" %}</strong></em> # using trans tag 

Solution 2: instead of blocktrans

It is better to use the blocktrans tag instead of the trans tag to include HTML tags.

The blocktrans tag allows blocktrans to mark complex sentences consisting of literals and variable content for translation using placeholders:

You can simply do:

 {% blocktrans %} Hold <em><strong>Ctrl</strong></em> {% endblocktrans %} 
+5
source

As Rahul said in his answer, you should not include HTML tags inside the trans template tag. However, according to Translating text blocks with Django .. what to do with HTML? (which I just found), you can put the HTML tags inside the blocktrans template tags, So I don’t need to do {% trans "Hold" %} <em><strong>{% trans "Ctrl" %}</strong></em>" . I could not find such instructions in the Django 1.8 docs .

+1
source

All Articles