处理 Django blocktrans 标签中的百分号 (%)
我目前正在本地化我的 Django 应用程序。除了 blocktrans 标签内的百分号之外,所有其他翻译都可以正常工作。
在我的模板中,我有 {% blocktrans %}Original % blocktrans{endblocktrans %}
。
django-admin makemessages
在 django.po 中生成此内容:
#: templates/index.html:78
#, python-format
msgid "Original %% blocktrans"
msgstr ""
我将其更新为 msgstr "Translated %% blocktrans"
,运行 django-admincompilemessages
,重新启动开发服务器并刷新页面,但我仍然在输出中看到 Original % blocktrans
。其他翻译均正确显示。
作为参考,{% trans "Original % trans" %}
也可以正常工作。在 makemessages 和翻译之后,我得到:
#: templates/index.html:72
msgid "Original % trans"
msgstr "Translated % trans"
这按预期工作 - 显示翻译版本。
我必须使用 blocktrans,因为我还需要将变量嵌入到字符串中。我正在使用 Django 1.2.5。
如何使 blocktrans 与百分号一起工作?
I'm currently localizing my Django app. All other translations work fine except percent-sign inside blocktrans tags.
In my template I have {% blocktrans %}Original % blocktrans{endblocktrans %}
.
django-admin makemessages
produces this in django.po:
#: templates/index.html:78
#, python-format
msgid "Original %% blocktrans"
msgstr ""
I update that to msgstr "Translated %% blocktrans"
, run django-admin compilemessages
, restart dev server and refresh the page, but I still see Original % blocktrans
in the output. Other translations are shown properly.
For reference, {% trans "Original % trans" %}
also works ok. After makemessages and translation I have:
#: templates/index.html:72
msgid "Original % trans"
msgstr "Translated % trans"
This works as expected - translated version is shown.
I must use blocktrans because I also need to embed variables to the strings. I'm using Django 1.2.5.
How can I make blocktrans work with percent-signs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看这张票 - 它不是一个解决方案,但它揭示了正在发生的事情
Check out this ticket - it's not a solution, but it sheds light on what's going on
无法找到问题的真正解决方案,因此我使用了一种解决方法:创建一个常量 PERCENT_SIGN = u'%' 并将其用作 blocktrans-blocks 内的 {{ PERCENT_SIGN }} 。
Couldn't find a real solution to the problem, so I used a workaround: create a constant PERCENT_SIGN = u'%' and use that as {{ PERCENT_SIGN }} inside blocktrans-blocks.
已针对此特定问题打开了另一张票证,并提供了修复该问题的补丁。希望 Django 1.4 能够修复这个问题。
https://code.djangoproject.com/ticket/16721
Another ticket has been opened for this particular issue, with a patch that fixes it. Hopefully it will be fixed for Django 1.4.
https://code.djangoproject.com/ticket/16721
2023 年,doc 提到了这个问题。看起来翻译后的短语中也必须有
%%
。因此# xgettext:no-python-format
有点无用,因为翻译者无论如何都必须意识到这一点。在任何地方使用%%
都有效。In 2023 the doc mention this problem. It looks like you must have
%%
also in the translated phrase. Hence# xgettext:no-python-format
is a bit useless, since the translators must be aware of that anyway. Using%%
everywhere works.