带有变量的 Django blocktrans

标签 django internationalization po django-i18n

我有一个模板,我想在其中翻译一个字符串。

{% blocktrans with "www.mywebsite.com" as website_name %}footer-slogan{{ website_name }}{% endblocktrans %}

我已经生成了我的 po 文件,其中我已经将字符串翻译如下:
msgid "footer-slogan %(website_name)s"
msgstr "This is a test %(website_name)s"

在我生成的 html 文件中,我得到了这个未翻译的元素:
footer-slogan www.mywebsite.com

如果我从翻译后的字符串中删除变量,它会起作用:
msgid "footer-slogan %(website_name)s"
msgstr "This is a test"

我什至尝试从源翻译中删除变量,但将变量保留在翻译后的字符串中,问题是相同的:
template.html
{% blocktrans with "www.mywebsite.com" as website_name %}footer-slogan{% endblocktrans %}

django.po
msgid "footer-slogan"
msgstr "This is a test %(website_name)s"

我希望能够仅在翻译后的字符串上设置变量。

我在翻译的字符串上做错了什么?

最佳答案

你可以这样使用 :

{% blocktrans %} 
    {% with website_name="www.mywebsite.com" %}
        {% trans 'footer-slogan{{ website_name }}' %}
    {% endwith %}   
{% endblocktrans %}

关于带有变量的 Django blocktrans,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46435085/

相关文章:

c++ - boost 翻译: po file not work

python - django 1.3 记录 timedrotatingfilehandler 在每次写入时截断文件

python - Django channel 不发送回复消息

python - 如何正确显示模板中递增的 Django 模型字段值?

python - 是否可以翻译Python中的函数、方法等?

unicode - 哪些编程语言从一开始就支持 Unicode?

python - Django 本地化消息合并错误

Django 在模板中获取派生形式 CHOICES

json - 使用 vue-i18n 和 nuxtjs 为每种语言提供多个翻译文件