开发者

(How) Can I use string substitution for working w/ Django’s i18n {% trans %} tag?

I'm looking for something like this:

{% trans "There are %{flowers}n fl开发者_运维问答owers in the vase" < flowers:3 %}

Now obviously syntax is fake, but it should be sufficient to demonstrate what I'm looking for.

Should I cook something of my own? It looks like a common usecase, so I was quite surprised that quick web search didn't return anything helpful.

I'm actually starting to loathe working with Django templating system... While I understand that it's designed to enforce separation of application logic from view, it's doing it by being intrusive on my workflow. I should be able to quickly prototype and only when I need to work with designer, and only then, should I have to be more strict about something like this.


I'm not absolutely sure what you're trying to do (what's < flowers:3 supposed to do?), but have you looked at blocktrans?

{% blocktrans count flowers|length as counter %}
    There is one flower in the vase.
{% plural %}
    There are {{ counter }} flowers in the vase.
{% endblocktrans %}


Use {% blocktrans %} instead of {% trans %}.


You may find the module inflect.py useful, although it would mean departing from the templating system.

import inflect
p = inflect.engine()
p.num(numflowers, show=False)
return 'There %s %s %s in the vase.' % (
              p.pl('is'),
              p.numwords(numflowers),
              p.pl('flower'))

with numflowers = 1

'There is one flower in the vase.'

with numflowers = 2

'There are two flowers in the vase.'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜