问题描述
在我的一个 Python 项目中,我正在遍历存储在我的 sqlite 数据库中的国家/地区列表:
{% if europe %}
{% for country in europe %}
<figcaption>{{ country.Name_fr }}</figcaption>
{% endfor %}
{% endif %}
我正在使用本地化,所以我有一个包含 .po 文件的语言环境文件夹,我用它来处理不同的翻译。但是,我希望数据库字符串(来自 {{ country.name_fr }})显示在这些 .po 文件中。这意味着我必须包含一个翻译标签,但如果我尝试它,它会显示一个错误。这:
<figcaption>{% translate {{ country.Name_fr }} %}</figcaption>
导致:
TemplateSyntaxError at /
Could not parse the remainder: '{{' from '{{'
任何帮助将不胜感激
解决方法
如 translate
模板标签文档所述
不可能在 {% 内的字符串中混合模板变量 翻译 %}。如果您的翻译需要带变量的字符串 (占位符),请改用 {% blocktranslate %}。
所以你可以使用 blocktranslate
{% blocktranslate %}
{{ country.Name_fr}}
{% endblocktranslate %}
对于您的用例来说,更简单的方法是将变量传递给翻译
{% translate %} 模板标签翻译一个常量字符串 (用单引号或双引号括起来)或可变内容
{% translate country.Name_fr %}