gpt4 book ai didi

django - 如何在 {% blocktrans %} 和 {%复数 %} 标签之间保留空格而不导致 msgfmt 错误?

转载 作者:行者123 更新时间:2023-12-04 15:58:49 24 4
gpt4 key购买 nike

我正在渲染一些 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.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消息中的标记,然后重新运行 compilemessages ,它编译得很好。

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

最佳答案

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

For instance, the following {% blocktrans %} tag:

{% blocktrans trimmed %}
First sentence.
Second paragraph.
{% endblocktrans %}

will result in the entry "First sentence. Second paragraph." in the PO file, compared to "\n First sentence.\n Second sentence.\n", if the trimmed option had not been specified.

关于django - 如何在 {% blocktrans %} 和 {%复数 %} 标签之间保留空格而不导致 msgfmt 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19013886/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com