如何解决Django中翻译的复数问题
您好,我正在尝试将以下文本复数化
<p>{% trans "How much apples do I have? I have " %}
{{ nb_apples }}
{% blocktrans count nb_apples=1 %}apple{% plural %}apples{% endblocktrans %}</p>
我的 .po 文件是这样定义的
#: .\templates\pages\home.html:13
msgid "apple"
msgid_plural "apples"
msgstr[0] "pomme"
msgstr[1] "pommes"
但是上面的方法不起作用。
与 count nb_apples=1 或 count nb_apples=0 它永远不会复数
与 count nb_apples>1 它会产生语法错误
与 count nb_apples=1|length 它总是复数,但当我只有一个苹果时,单数不受尊重。请注意, nb_apples 变量是从视图中随机传递的
def get_context_data(self,**kwargs):
posts = Post.objects.all()
context = super().get_context_data(**kwargs)
context['title'] = _('Home')
context['comment'] = _('Welcome to my site')
context['posts'] = posts
context['nb_apples'] = random.randint(1,10)
return context
然后,课后
def home(request):
nb_apples = random.randint(1,10)
return render(request,'pages/home.html',{
'title': _('Home'),'comment': _('Welcome to my site'),'nb_apples': nb_apples
})
我想在 nb_apples 变量等于 1 时看到“apple”或“pomme”,当 nb_apples 变量大于 1 时看到“apples”或“pommes”。
怎么了?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。