Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
  • Loading branch information...
commit b9d9351e852bae532620f174a13a1cd966886b04 1 parent ec4a143
Russell Keith-Magee freakboy3742 authored
25 django/template/defaultfilters.py
View
@@ -430,20 +430,35 @@ def filesizeformat(bytes):
return "%.1f MB" % (bytes / (1024 * 1024))
return "%.1f GB" % (bytes / (1024 * 1024 * 1024))
-def pluralize(value):
- "Returns 's' if the value is not 1, for '1 vote' vs. '2 votes'"
+def pluralize(value, arg='s'):
+ """
+ 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:
if int(value) != 1:
- return 's'
+ return plural_suffix
except ValueError: # invalid string that's not a number
pass
except TypeError: # value isn't a string or a number; maybe it's a list?
try:
if len(value) != 1:
- return 's'
+ return plural_suffix
except TypeError: # len() of unsized object
pass
- return ''
+ return singular_suffix
def phone2numeric(value):
"Takes a phone number and converts it in to its numerical equivalent"
16 docs/templates.txt
View
@@ -951,12 +951,26 @@ any string.
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::
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
~~~~~~
30 tests/othertests/defaultfilters.py
View
@@ -313,6 +313,36 @@
>>> pluralize(2)
'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')
'0800 3569377'
Please sign in to comment.
Something went wrong with that request. Please try again.