Can Django trans tags include HTML tags?
Asked Answered
F

2

12

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?

Forebode answered 28/9, 2015 at 19:10 Comment(0)
M
14

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 %}
Mucous answered 28/9, 2015 at 19:15 Comment(5)
This doesn't seem to accord with the DRY principle.Forebode
To some extent you can say.Mucous
Quite a bit in my case.Forebode
@RahulGupta I'm confused by this. What does the ability to mix variables and strings have to with including HTML tags (which are just constant strings)? Which is to say, you should be able to include them just fine, but you need to make sure they're translated correctly (i.e. not translated in most cases) ;)Tracery
Solution one isn't really correct, it doesn't make sense to translate the words independently if they are part of a sentence. E.g: some languages need them to be the other way around ("Ctrl" and then "Hold").Esmaria
F
2

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.

Forebode answered 29/9, 2015 at 17:46 Comment(1)
Yes, we can't use HTML tags inside a trans template tag but we can use blocktrans tag. The solution using trans template is not the best solution. We can use the blocktrans template tag instead for our case. I have updated the ans, please check.Mucous

© 2022 - 2024 — McMap. All rights reserved.