Django 如何保持{%blocktrans%}和{%plural%}标记之间的空白而不导致msgfmt错误?
我正在渲染一些;以下是模板文件中的相关代码段:Django 如何保持{%blocktrans%}和{%plural%}标记之间的空白而不导致msgfmt错误?,django,plural,django-i18n,msgfmt,Django,Plural,Django I18n,Msgfmt,我正在渲染一些;以下是模板文件中的相关代码段: {% blocktrans count choice_count=choice_count %} You have {{ choice_count }} choice: {% plural %} You have {{ choice_count }} choices: {% endblocktrans %} 运行python manage.py makemessages--all后,这是my中的相关代码段,例如django.po文件中的e
{% blocktrans count choice_count=choice_count %}
You have {{ choice_count }} choice:
{% plural %}
You have {{ choice_count }} choices:
{% endblocktrans %}
运行python manage.py makemessages--all
后,这是my中的相关代码段,例如django.po
文件中的en
:
msgid ""
"\n"
" You have %(choice_count)s choice:\n"
msgid_plural ""
"\n"
" You have %(choice_count)s choices:\n"
msgstr[0] "You have one choices:"
msgstr[1] "You have %(choice_count)s choice(s):"
但当我运行python manage.py compilemessages
时,我得到的错误消息如下:
$ ./manage.py compilemessages
processing file django.po in /home/yiqing/repos/training/site/training/locale/en/LC_MESSAGES
/home/yiqing/repos/training/site/training/locale/en/LC_MESSAGES/django.po:60: `msgid' and `msgstr[0]' entries do not both begin with '\n'
msgfmt: found 4 fatal errors
我知道这是因为模板文件中有换行符/空格,并且我知道如何“绕过”它--当我将模板片段更改为时,例如:
{% blocktrans count choice_count=choice_count %}You have {{ choice_count }} choice:{% plural %}You have {{ choice_count }} choices:{% endblocktrans %}
然后重新运行makemessages
,从消息中删除fuzzy
标记,然后重新运行compilemesessages
,它编译得很好
然而,我的问题是如何保持第一个模板语法,并且仍然能够编译消息,因为它极大地提高了模板文件中代码的可读性 最简单的方法就是匹配输入字符串的格式。在您的示例中,
.po
文件如下所示:
msgid ""
"\n"
" You have %(choice_count)s choice:\n"
msgid_plural ""
"\n"
" You have %(choice_count)s choices:\n"
msgstr[0] "\nYou have one choices:\n"
msgstr[1] "\nYou have %(choice_count)s choice(s):\n"
这个文件编译时没有错误,但是,正如您所看到的,它很乏味
据我所知,目前还没有解决这个问题的其他办法。看来django rosetta确实有一个补丁来处理这个问题(请参阅)。我相信使用应该可以解决这个问题。它所做的是删除其起点和终点之间的任何空格(和换行)。我只测试了这个,没有使用复数,但它应该可以工作
{% spaceless %}{% blocktrans count choice_count=choice_count %}
You have {{ choice_count }} choice:
{% plural %}
You have {{ choice_count }} choices:
{% endblocktrans %}{% endspaceless %}
文档中提到了“修剪”关键字,引用: 例如,以下{%blocktrans%}标记:
{% blocktrans trimmed %}
First sentence.
Second paragraph.
{% endblocktrans %}
将导致输入“第一句。第二段。”
PO文件,与“\n第一句相比。\n第二句
句子。\n“,如果未指定修剪的选项
您是否尝试过使用(至少)Django 1.11下的工作和文档