# -*- coding: utf-8 -*-
"""
Usage: {% plural quantity name_singular name_plural %}
This simple version only works with template variable since we will use blocktrans for strings.
"""
from django import template
register = template.Library()
class PluralNode(template.Node):
def __init__(self, quantity, single, plural):
self.quantity = template.Variable(quantity)
self.single = template.Variable(single)
self.plural = template.Variable(plural)
def render(self, context):
if self.quantity.resolve(context) > 1:
return u'%s' % self.plural.resolve(context)
else:
return u'%s' % self.single.resolve(context)
@register.tag(name="plural")
def do_plural(parser, token):
try:
# split_contents() knows not to split quoted strings.
tag_name, quantity, single, plural = token.split_contents()
except ValueError:
raise template.TemplateSyntaxError, "%r tag requires exactly three arguments" % token.contents.split()[0]
return PluralNode(quantity, single, plural)
Comments
What does this do that the pluralize filter doesn't?
#