模板中的 django blocktrans 和 i18n [英] django blocktrans and i18n in templates
问题描述
我在 django 中遇到了 i18n 问题:
I have an i18n problem in django:
这很好用:
{% trans cat.name %}
cat.name
将被翻译
但这不起作用:
{% blocktrans with cat.name|slugify as cat_slug %}{{ cat_slug }}{% endblocktrans %}
cat.name
未翻译
如果我改变过滤器:
{% blocktrans with cat.name|capfirst as cat_slug %}{{ cat_slug }}{% endblocktrans %}
我可以看到过滤器正在工作,但是没有翻译...
I can see that the filter is working, but there is no translation...
推荐答案
我才刚刚开始 Django 国际化,但我想你误解了 {% blocktrans %}
标签处理占位符.
I'm only just getting started with Django internationalization, but I think you're misunderstanding how the {% blocktrans %}
tag handles placeholders.
blocktrans
的意义在于允许围绕占位符的文本被翻译.它不会翻译 {{...}}
中的任何内容.
The point of blocktrans
is to allow the text around the placeholders to be translated. It won't translate anything inside {{...}}
.
如果您查看生成的 .po
文件,您会看到以下模板代码:
If you look at the generated .po
file, you'll see that the following template code:
{% blocktrans %}This is my variable: {{variable}}{% endblocktrans %}
将被转换成如下内容:
msgid:"This is my variable: %s"
我认为您不能翻译 blocktrans 标记中的变量.您可能可以使用 {% blocktrans with _("string") as x %}{{x}}{% endblocktrans %}
来执行常量字符串,但我想不出您为什么要这样做.
I don't think you can translate a variable within a blocktrans tag. You can probably do constant strings with {% blocktrans with _("string") as x %}{{x}}{% endblocktrans %}
but I can't think why you'd want to.
我认为你必须在视图或模型代码中做你想做的事情.
You'll have to do what you want in your view or model code I think.
这篇关于模板中的 django blocktrans 和 i18n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!