diff options
author | Russell Keith-Magee <russell@keith-magee.com> | 2006-07-04 06:18:39 +0000 |
---|---|---|
committer | Russell Keith-Magee <russell@keith-magee.com> | 2006-07-04 06:18:39 +0000 |
commit | b9d9351e852bae532620f174a13a1cd966886b04 (patch) | |
tree | a6ae9c112e899c545dd5da36ef96c2f430afdde1 /django | |
parent | ec4a143a407169f6a7b6d79695db6a641b242f9b (diff) | |
download | django-b9d9351e852bae532620f174a13a1cd966886b04.tar.gz |
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
Diffstat (limited to 'django')
-rw-r--r-- | django/template/defaultfilters.py | 25 |
1 files changed, 20 insertions, 5 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" |