模板中的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
的目的是允许文本 around 持有人要翻译。它不会翻译 {{...}}
内部的任何内容。
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和_( 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屋!