2011-03-24 23 views
1

是否有可能使用{% blocktrans %}與「」和「計數」在同一時間?使用blocktrans與「同」和「算」關鍵字一起

的文檔描述只是單獨使用:

{% blocktrans with foo|filter as bar and baz|filter as boo %} 
{% blocktrans count var|length as count %} 


我需要打印一個變量的值,翻譯依賴於另一個變量。我嘗試下面的代碼:

{% blocktrans count cnt as count with cnt|make_text_from_count as text_count %} 
    and other {{ text_count }} city 
{% plural %} 
    and other {{ text_count }} cities 
{% endblocktrans %} 

它顯示text_count變量的值,但不翻譯文本。


Python 2.6.6,Django 1.3,django-templates。

+0

問題出在.m文件中的「模糊」標誌。 – Gregory 2011-03-24 13:07:27

回答

1

http://docs.djangoproject.com/en/dev/topics/i18n/internationalization/#blocktrans-template-tag

{% blocktrans with text_count=cnt|make_text_from_count count cnt=cnt %} 
    and another city 
{% plural %} 
    and other {{ text_count }} cities 
{% endblocktrans %} 
+0

TemplateSyntaxError:「u'blocktrans'標記中的'count'預期只有一個關鍵字參數。」如果我解決了這個問題,那麼我們可以獲得相當於我的代碼,但是語法不同。 – Gregory 2011-03-24 09:55:19

+0

單數情況下不需要{{text_count}},因爲text_count == 1。 – 2011-05-10 13:01:03

+0

我已經編輯了我的答案,在'count' django當然期待另一個關鍵字參數之後...... – 2011-05-10 17:40:22

2

是的,這是可能的。你只需要注意blocktrans參數的順序:with需要緊接在它後面的局部變量綁定,並且count及其各自的變量綁定在它之後。

documentation(至少1.5版本)有幾個多元化的例子。第二個例子(介紹爲「一個更復雜的例子」)同時顯示withcount使用時的順序:

{% blocktrans with amount=article.price count years=i.length %} 
That will cost $ {{ amount }} per year. 
{% plural %} 
That will cost $ {{ amount }} per {{ years }} years. 
{% endblocktrans %} 

如果你不需要一個比爲計數器任何其他變量,不要使用關鍵字with。這在更復雜的第一個示例中示出:

{% blocktrans count counter=list|length %} 
There is only one {{ name }} object. 
{% plural %} 
There are {{ counter }} {{ name }} objects. 
{% endblocktrans %} 
相關問題