2013-09-25 9 views
5

私は一部をレンダリングしていますpluralization using the blocktrans tag;ここでは関係スニペットは、テンプレートファイルにあります:msgfmtエラーを引き起こさずに{%blocktrans%}タグと{%plural%}タグの間に空白を入れたいのですが?

{% 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を再実行しても問題ありません。

しかし、私の質問は、テンプレートファイルのコードの可読性を大幅に向上させるため、最初のテンプレート構文を維持してメッセージをコンパイルする方法です。

+0

['{%spaceless%}'タグを使用してみましたか(https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#spaceless)? – guival

答えて

2

最も簡単なことは、入力文字列のフォーマットと一致することです。あなたが見るように、それは退屈で、

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" 

このファイルには、エラーなしでコンパイル、しかし:あなたの例では、.poファイルは次のようになります。

私の知る限り、現在のところ、この問題に対するその他の回避策はありません。 django-rosettaには、この正確なことを処理するパッチがあります(https://github.com/mbi/django-rosetta/pull/34参照)。

0

私は{% spaceless %} tagを使用して問題を解決すると信じています。それは、開始点と終了点の間のスペース(およびラインジャンプ)を取り除くことです。私は複数形を使わずにこれをテストしましたが、うまくいくはずです。

{% spaceless %}{% blocktrans count choice_count=choice_count %} 
    You have {{ choice_count }} choice: 
{% plural %} 
    You have {{ choice_count }} choices: 
{% endblocktrans %}{% endspaceless %} 
関連する問題