[Django]-How to mark string for translation in jinja2 using trans blocks

0👍

To translate a block like that you need to use the {% blocktrans %} template tag,

{% blocktrans with myurl=request.url('start') %}
A list of qualifying devices is available once you start your trade-in estimate. <a href= {{myurl}}>Click here</a> to learn what your old device is worth.</p>
{% endblocktrans %}

However, I’m not sure you can call a function like that anywhere in a template like you are attempting.

Consider instead passing myurl in as a template variable, and then using smaller chunks of text. This increases reuse of the translations – especially for small common blocks like “Click here”.

{% blocktrans %}
    "A list of qualifying devices is available once you start your trade-in estimate."
{% endblocktrans %}

<a href= {{myurl}}>{% trans "Click here" %}</a>
    {% trans "to learn what your old device is worth." %}

Also, when using HTML and template tags, try and keep them correctly nested. For example, in your code you would have:

<p>... {% blocktrans %}... </p>{% endblocktrans %}

Instead try to nest them like so:

<p>... {% blocktrans %}... {% endblocktrans %}</p>

This is especially useful for IDEs that can auto indent and fold content and helps readability.

Leave a comment