Django模板翻译: zero is plural in fr

标签 django translation

我正在使用 Django,并且无法使用法语在我的模板中获得正确的复数形式,无论我在 blocktrans 标记中使用什么文本。例如:

{% blocktrans count counter=0 %}
    foo
{% plural %}
    bars
{% endblocktrans %}

给我bars而不是foo。事实上,0 在法语中是单数。

我在模板中使用以下内容检查了语言代码:

{% get_current_language as LANGUAGE_CODE %}
{{ LANGUAGE_CODE }}

它给了我fr

以下内容位于我的 django.po 法语翻译文件的标题中:

"Plural-Forms: nplurals=2; plural=(n > 1);\n"

你们有什么想法吗?

最佳答案

好吧,我终于知道这是怎么回事了。因为我输入 blocktrans 的方式,这是我在 django.po 中得到的内容由 python manage.py makemessages -l fr 生成的文件命令:

msgid ""
"\n"
"                                foo\n"
"                            "
msgid_plural ""
"\n"
"                                bars\n"
"                            "
msgstr[0] ""
msgstr[1] ""

我把类似的东西写为 msgstr[0]msgstr[1] :

msgstr[0] ""
"\n"
"                                foo\n"
"                            "
msgstr[1] ""
"\n"
"                                bars\n"
"                            "

所以我更新了模板:

{% blocktrans count counter=0 %}
    foo
{% plural %}
    bars
{% endblocktrans %}

致:

{% blocktrans count counter=0 %}foo{% plural %}bars{% endblocktrans %}

翻译如下:

msgid "foo"
msgid_plural "bars"
msgstr[0] "foo"
msgstr[1] "bars"

看起来您的 django.po 中是否有问题(翻译字符串丢失或为空)那么它会导致模板中出现错误的 0 解释。

我会把它留在那里......希望这有帮助!

关于Django模板翻译: zero is plural in fr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33220269/

相关文章:

django - 如何判断 Django session 是否是新 session ?

django - select_related 字段的调用管理器

wordpress - 帖子有多种语言吗?

python - django-admin makemessages : how does it work with txt, xml 和其他文件?

python - 寻找一个 Python 库来模拟数据库

mysql - 在 django ORM 中按 id 数组排序

java - 在将代码从 C 翻译成 Java 方面需要帮助

python - Django 切换,对于一段代码,切换语言以便以一种语言完成翻译

php - 无法转义翻译消息中的点字符

django - 多语言 django 网站上的搜索功能