我正在渲染一些pluralization using the blocktrans tag;这是模板文件中的相关代码段:

{% blocktrans count choice_count=choice_count %}
  You have {{ choice_count }} choice:
{% plural %}
  You have {{ choice_count }} choices:
{% endblocktrans %}

运行python manage.py makemessages --all后,这是我的相关代码段,例如django.poen文件:
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标记,然后重新运行compilemessages,这样编译就可以了。

但是,我的问题是如何保留第一个模板语法并仍然能够编译消息,因为它大大提高了模板文件中代码的可读性。

最佳答案

该文档提到blocktrans的“trimmed”关键字,并引用:

09-20 00:13