如何使数据库中的字符串显示在Django中的本地化.po文件中?
在我的一个python项目中,我循环浏览存储在SQLite数据库中的国家列表:如何使数据库中的字符串显示在Django中的本地化.po文件中?,django,django-templates,internationalization,django-i18n,Django,Django Templates,Internationalization,Django I18n,在我的一个python项目中,我循环浏览存储在SQLite数据库中的国家列表: {% if europe %} {% for country in europe %} <figcaption>{{ country.Name_fr }}</figcaption> {% endfor %} {% endif %
{% if europe %}
{% for country in europe %}
<figcaption>{{ country.Name_fr }}</figcaption>
{% endfor %}
{% endif %}
如有任何帮助,将不胜感激。模板标记文档说明
无法在{%内的字符串中混合模板变量
翻译%}。如果翻译需要带变量的字符串
(占位符),改为使用{%blocktranslate%}
所以你可以用
对于您的用例来说,更简单的方法是只传递变量进行转换 {%translate%}模板标记转换常量字符串或 (用单引号或双引号括起来)或可变内容
但这实际上并没有转换变量。如果我在模板中设置了此选项,“python managy.py makemessages”无法识别该字符串,不会将其写入po文件,因此也不会对其进行翻译。我可能需要在模型中翻译它吗?如果您有其他需要翻译的字符串,您必须手动添加它们,因为makemessages将找不到任何与数据库相关的内容,这正是我想要的答案。我认为有必要通过makemessages将字符串添加到po文件中,但是手动添加数据库中的字符串并在模板中包含translate标记效果很好。非常感谢。
<figcaption>{% translate {{ country.Name_fr }} %}</figcaption>
TemplateSyntaxError at /
Could not parse the remainder: '{{' from '{{'
{% blocktranslate %}
{{ country.Name_fr}}
{% endblocktranslate %}
{% translate country.Name_fr %}