[Django]-Django templates: Best practice for translating text block with HTML in it

23👍

From the docs:

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.

Then under blocktrans:

To translate a template expression — say, accessing object attributes or using template filters — you need to bind the expression to a local variable for use within the translation block. Examples:

{% blocktrans with article.price as amount %}
That will cost $ {{ amount }}.
{% endblocktrans %}

{% blocktrans with value|filter as myvar %}
This will have {{ myvar }} inside.
{% endblocktrans %}

This way your translated strings have the placeholders. In your case:

{% blocktrans with login_object.anchor as anchor %}
    Please {{ anchor|safe }}log in</a> in order to use MyApplicationName.
{% endblocktrans %}

You will need to generate the text that goes in anchor in your view function. This keeps it out of your translated string.

14👍

Not only does it make more sense to put the entire sentence in one translation string, it may be impossible for translators to get the sentence correct when it’s split into pieces. Remember that the different parts of the sentence can affect each other, with tenses, cases, gender, and so on. Not to mention that other languages behave differently than English. The word “please” for example could be different when making a request and making a demand, for example.

Always use complete sentences in your translation strings so that the translators can make a correct sentence in the target language.

Mike DeSimone makes the right recommendation, I would make just one tweak to it:

{% blocktrans with login_object.anchor_attr as anchor_attr %}
    Please <a {{ anchor_attr|safe }}>log in</a> in order to use MyApplicationName.
{% endblocktrans %}

This keeps the HTML in the translation string balanced. Without the opening tag in the string, it could easily look like an error in the string.

10👍

I can offer a convenient solution for only partial fragments which are constant for any translation.

In this case you can avoid to use any HTML or CSS inside of your .po file when using custom template tag such as the next:

@register.filter( name='safewrap' )
def safewrap( val, arg ):
    return val.format( arg )
safewrap.is_safe = True

...

{% blocktrans with sum2="<a href='mysite.com/offer'>{0}</a>"|safewrap:sum %}
    It costs {{sum2}} dollars.
{% endblocktrans %}

So, in your .po file you have:

It costs %(sum2)s dollars.

But it’s a difficult question – what to do with partial fragments which require translation (like in your case).

Leave a comment