Fixes #2202 -- Added ability to customize output of pluralize filter to handle irregular cases (walrus/walruses, cherry/cherries). Thanks to gid for the suggestion and the initial patch
git-svn-id: http://code.djangoproject.com/svn/django/trunk@3272 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
ec4a143a40
commit
b9d9351e85
|
@ -430,20 +430,35 @@ def filesizeformat(bytes):
|
||||||
return "%.1f MB" % (bytes / (1024 * 1024))
|
return "%.1f MB" % (bytes / (1024 * 1024))
|
||||||
return "%.1f GB" % (bytes / (1024 * 1024 * 1024))
|
return "%.1f GB" % (bytes / (1024 * 1024 * 1024))
|
||||||
|
|
||||||
def pluralize(value):
|
def pluralize(value, arg='s'):
|
||||||
"Returns 's' if the value is not 1, for '1 vote' vs. '2 votes'"
|
"""
|
||||||
|
Returns a plural suffix if the value is not 1, for '1 vote' vs. '2 votes'
|
||||||
|
By default, 's' is used as a suffix; if an argument is provided, that string
|
||||||
|
is used instead. If the provided argument contains a comma, the text before
|
||||||
|
the comma is used for the singular case.
|
||||||
|
"""
|
||||||
|
bits = arg.split(',')
|
||||||
|
if len(bits) == 2:
|
||||||
|
singular_suffix = bits[0]
|
||||||
|
plural_suffix = bits[1]
|
||||||
|
elif len(bits) == 1:
|
||||||
|
singular_suffix = ''
|
||||||
|
plural_suffix = bits[0]
|
||||||
|
else:
|
||||||
|
return ''
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if int(value) != 1:
|
if int(value) != 1:
|
||||||
return 's'
|
return plural_suffix
|
||||||
except ValueError: # invalid string that's not a number
|
except ValueError: # invalid string that's not a number
|
||||||
pass
|
pass
|
||||||
except TypeError: # value isn't a string or a number; maybe it's a list?
|
except TypeError: # value isn't a string or a number; maybe it's a list?
|
||||||
try:
|
try:
|
||||||
if len(value) != 1:
|
if len(value) != 1:
|
||||||
return 's'
|
return plural_suffix
|
||||||
except TypeError: # len() of unsized object
|
except TypeError: # len() of unsized object
|
||||||
pass
|
pass
|
||||||
return ''
|
return singular_suffix
|
||||||
|
|
||||||
def phone2numeric(value):
|
def phone2numeric(value):
|
||||||
"Takes a phone number and converts it in to its numerical equivalent"
|
"Takes a phone number and converts it in to its numerical equivalent"
|
||||||
|
|
|
@ -951,12 +951,26 @@ any string.
|
||||||
pluralize
|
pluralize
|
||||||
~~~~~~~~~
|
~~~~~~~~~
|
||||||
|
|
||||||
Returns ``'s'`` if the value is not 1.
|
Returns a plural suffix if the value is not 1. By default, this suffix is ``'s'``.
|
||||||
|
|
||||||
Example::
|
Example::
|
||||||
|
|
||||||
You have {{ num_messages }} message{{ num_messages|pluralize }}.
|
You have {{ num_messages }} message{{ num_messages|pluralize }}.
|
||||||
|
|
||||||
|
For words that require a suffix other than ``'s'``, you can provide an alternate
|
||||||
|
suffix as a parameter to the filter.
|
||||||
|
|
||||||
|
Example::
|
||||||
|
|
||||||
|
You have {{ num_walruses }} walrus{{ num_walrus|pluralize:"es" }}.
|
||||||
|
|
||||||
|
For words that don't pluralize by simple suffix, you can specify both a
|
||||||
|
singular and plural suffix, separated by a comma.
|
||||||
|
|
||||||
|
Example::
|
||||||
|
|
||||||
|
You have {{ num_cherries }} cherr{{ num_cherries|pluralize:"y,ies" }}.
|
||||||
|
|
||||||
pprint
|
pprint
|
||||||
~~~~~~
|
~~~~~~
|
||||||
|
|
||||||
|
|
|
@ -313,6 +313,36 @@ False
|
||||||
>>> pluralize(2)
|
>>> pluralize(2)
|
||||||
's'
|
's'
|
||||||
|
|
||||||
|
>>> pluralize([1])
|
||||||
|
''
|
||||||
|
|
||||||
|
>>> pluralize([])
|
||||||
|
's'
|
||||||
|
|
||||||
|
>>> pluralize([1,2,3])
|
||||||
|
's'
|
||||||
|
|
||||||
|
>>> pluralize(1,'es')
|
||||||
|
''
|
||||||
|
|
||||||
|
>>> pluralize(0,'es')
|
||||||
|
'es'
|
||||||
|
|
||||||
|
>>> pluralize(2,'es')
|
||||||
|
'es'
|
||||||
|
|
||||||
|
>>> pluralize(1,'y,ies')
|
||||||
|
'y'
|
||||||
|
|
||||||
|
>>> pluralize(0,'y,ies')
|
||||||
|
'ies'
|
||||||
|
|
||||||
|
>>> pluralize(2,'y,ies')
|
||||||
|
'ies'
|
||||||
|
|
||||||
|
>>> pluralize(0,'y,ies,error')
|
||||||
|
''
|
||||||
|
|
||||||
>>> phone2numeric('0800 flowers')
|
>>> phone2numeric('0800 flowers')
|
||||||
'0800 3569377'
|
'0800 3569377'
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue