如何翻译 Django 模板中 for 循环内的变量?

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

我有以下代码:

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

我想翻译 item 变量,我试过像这样使用 trans 标签:

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

但是 Django 抱怨语法错误,指出它期待 emptyendfor

您需要在模板顶部添加 {% load i18n %} 才能使用 trans 标签。

来自 internationalization:

上的文档

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