From b9d9351e852bae532620f174a13a1cd966886b04 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Tue, 4 Jul 2006 06:18:39 +0000 Subject: [PATCH] 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 --- django/template/defaultfilters.py | 25 ++++++++++++++++++++----- docs/templates.txt | 16 +++++++++++++++- tests/othertests/defaultfilters.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 6 deletions(-) diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py index 9bd6e7cb3c..2b2a0d4568 100644 --- a/django/template/defaultfilters.py +++ b/django/template/defaultfilters.py @@ -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" diff --git a/docs/templates.txt b/docs/templates.txt index 42947510d1..4ba52b3263 100644 --- a/docs/templates.txt +++ b/docs/templates.txt @@ -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 ~~~~~~ diff --git a/tests/othertests/defaultfilters.py b/tests/othertests/defaultfilters.py index 46f2519285..1636b948d0 100644 --- a/tests/othertests/defaultfilters.py +++ b/tests/othertests/defaultfilters.py @@ -313,6 +313,36 @@ False >>> 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'