3

I am using django and jinja2 and have something like this in one of my html pages

<p><strong>Q. {{ _("What products will you accept?") }}</strong></p>

<p class="style3"><strong>A: </strong>{% trans 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>{% endtrans %}

When I run django-admin.py makemessages, "What products will you accept?" is the only string that gets processed. I thought that wrapping a string with the {% trans %} block also marks that string or is this a wrong statement?

What is the best technique to mark that second string (it's tricky because of the request.url variable)

I've tried {{ _("A list of qualifying devices is available once you start your trade-in estimate. <a href= {{ request.url('start') }}>Click here</a> to learn what your old device is worth.")|safe }} but then the link doesn't work properly.

1 Answers1

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.