2010-05-28 79 views
10

根據Django文檔中的this section,我應該使用{% blocktrans %}來處理需要翻譯複數的情況。然而,像下面這樣的例子,我能做些什麼更方便嗎?Django模板{%trans%}多元化

{% blocktrans count video.views.count as views %} 
The video has been viewed <span>{{ views }}</span> time 
{% plural %} 
The video has been viewed <span>{{ views }}</span> times 
{% endblocktrans %} 

我試着做到以下幾點:

{% blocktrans %}time{% plural %}times{% endblocktrans %} 

但它扔TemplateSyntaxError: 'blocktrans' doesn't allow other block tags (seen u'plural') inside it

+3

這使得它非常難翻譯,因爲你是強迫的話排序;如果翻譯需要在「{{views}}」前面有複數形式,該怎麼辦?詳細的表達式真的是要走的路。 – bouke 2013-01-18 08:03:04

回答

12

你忘了在blocktrans標籤count variable as variable_name

該變量的值將被用來檢測它是否是複數。

{% blocktrans count variable as variable_name %} 
    time 
    {% plural %} 
    {{ variable_name }} times 
{% endblocktrans %} 
+0

啊現在我看到:)我仍然覺得應該有一個比這更方便的方式,雖然: {%trans「該視頻已被瀏覽」%} {{video.views.count}} {% blocktrans將video.views.count計爲觀看次數%}時間{%複數%}次數{%endblocktrans%} – jmagnusson 2010-05-28 14:45:25

+1

文檔鏈接https://docs.djangoproject.com/zh/dev/topics/i18n/translation/#std: templatetag-blocktrans – Gesias 2014-08-28 06:31:50

-1

您可以使用:

{% blocktrans with video.views.count|pluralize as foo and video.views.count as views %} 
The video has been viewed <span>{{ views }}</span> time{{ foo }} 
{% endblocktrans %} 
+2

...這是擊敗使用blocktrans。它創造更多的文字謎題。最好選擇'{%plural%}'解決方案。 – vdboor 2012-06-07 22:39:25