With Jinja2 and Babel, how do I translate sentences containing HTML tags?
You can do that:
{% trans url=url_for('our_site') %}<p>The <em>best</em> way of using the Internet is to use <a href="{{ url }}">our site</a>.</p>{% endtrans %}
same with object nested variables (obj.site_name
):
{% trans url=url_for('our_site'), site_name=obj.site_name %}<p>The <em>best</em> way of using the Internet isto use <a href="{{ url }}">our site</a>{{ site_name }}.</p>{% endtrans %}
so you have to declare variables as the trans
function params, otherwise trans
won't work. To read more, please visit docs.
You can use the gettext() and the safe filter
{{ gettext('The <em>best</em> solution') | safe }}
http://jinja.pocoo.org/docs/2.9/templates/#list-of-builtin-filters
Your translator would then be able to arrange the tags.
If you want to keep things a little simpler for the translator you could add a custom markdown filter and use that that add simple formatting within phrases, see here for an example https://gist.github.com/glombard/7554134