With Jinja2 and Babel, how do I translate sentences containing HTML tags?
<p>Suppose I have a Jinja2 template, and I'm using Flask-Babel to translate my project. For example:</p>

<pre><code>&lt;p&gt;The &lt;em&gt;best&lt;/em&gt; way of using the Internet is
to use &lt;a href="{{ url_for('our_site') }}"&gt;our site&lt;/a&gt;.&lt;/p&gt;
</code></pre>

<p>So I have a sentence with a link and an emphasis. Suppose I then want to translate my sentence. The obvious way would be to use <code>gettext()</code> or the <code>{% trans %}</code> tag:</p>

<pre><code>&lt;p&gt;{% trans %}The {% endtrans %} &lt;em&gt;{% trans %}best{% endtrans %}&lt;/em&gt;
{% trans %}way of using the Internet is to use{% endtrans %}
&lt;a href="{{ url_for('our_site') }}"&gt;{% trans %}our site{% endtrans %}&lt;/a&gt;{% trans %}.{% endtrans %}&lt;/p&gt;
</code></pre>

<p>Obviously the problem is that this breaks up the sentence into multiple fragments that don't translate well. This would result in the translator considering the string "The", "best", "way of using the Internet is to use", and "our site" as all separate strings, plus the punctuation. Of course, the translator will want to restructure the sentence, and choose what words to link and emphasize separately.</p>

<p>So in light of that, what's the solution? How do I have a sentence with tags in it that translates as one unit?</p>