lordscales91
lordscales91

Reputation: 433

How to translate variables inside a for loop in a Django template?

I have the following code:

<ul>
    {% for item in array %}
        <li>{{ item }}</li>
    {% endfor %}
</ul>

I want to translate the item variable, I've tried using the trans tag like this:

<ul>
    {% for item in array %}
        <li>{% trans item %}</li>
    {% endfor %}
</ul>

But Django complains with a syntax error stating that it was expecting an empty or an endfor

Upvotes: 1

Views: 1279

Answers (1)

Rahul Gupta
Rahul Gupta

Reputation: 47846

You need to add {% load i18n %} at the top of your template to use the trans tag.

From the docs on internationalization:

To give your template access to these tags, put {% load i18n %} toward the top of your template.

Upvotes: 1

Related Questions