我正在呈现一些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后,这是我的相关代码片段,例如en的django.po文件
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,它编译得很好。
然而,我的问题是如何保持第一个模板语法,并且仍然能够编译消息,因为它极大地提高了模板文件中代码的可读性。
发布于 2018-09-16 20:27:40
文档中提到了blocktrans的"trimmed“关键字,引用如下:
例如,下面的{% BLOCKTRAN%}标记:
{% First %}第一句话。第二段。{% endblocktrans trans%}
将导致条目“第一句话.第二段.”PO文件中的与"\n第一句.\n第二句.\n“,如果尚未指定修剪选项。
发布于 2014-01-17 00:02:31
你能做的最简单的事情就是匹配你的输入字符串的格式。在您的示例中,.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确实有一个补丁来处理这个问题(参见https://github.com/mbi/django-rosetta/pull/34)。
发布于 2017-03-07 19:09:29
我相信使用{% spaceless %} tag应该可以解决这个问题。它所做的是删除开头和结尾之间的所有空格(和跳行)。我只在没有使用复数的情况下对此进行了测试,但它应该可以工作。
{% spaceless %}{% blocktrans count choice_count=choice_count %}
You have {{ choice_count }} choice:
{% plural %}
You have {{ choice_count }} choices:
{% endblocktrans %}{% endspaceless %}https://stackoverflow.com/questions/19013886
复制相似问题