Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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 authored July 04, 2006
25  django/template/defaultfilters.py
@@ -430,20 +430,35 @@ def filesizeformat(bytes):
430 430
         return "%.1f MB" % (bytes / (1024 * 1024))
431 431
     return "%.1f GB" % (bytes / (1024 * 1024 * 1024))
432 432
 
433  
-def pluralize(value):
434  
-    "Returns 's' if the value is not 1, for '1 vote' vs. '2 votes'"
  433
+def pluralize(value, arg='s'):
  434
+    """
  435
+    Returns a plural suffix if the value is not 1, for '1 vote' vs. '2 votes'
  436
+    By default, 's' is used as a suffix; if an argument is provided, that string
  437
+    is used instead. If the provided argument contains a comma, the text before
  438
+    the comma is used for the singular case.
  439
+    """
  440
+    bits = arg.split(',')
  441
+    if len(bits) == 2:
  442
+        singular_suffix = bits[0]
  443
+        plural_suffix = bits[1]
  444
+    elif len(bits) == 1:
  445
+        singular_suffix = ''
  446
+        plural_suffix = bits[0]
  447
+    else:
  448
+        return ''
  449
+    
435 450
     try:
436 451
         if int(value) != 1:
437  
-            return 's'
  452
+            return plural_suffix
438 453
     except ValueError: # invalid string that's not a number
439 454
         pass
440 455
     except TypeError: # value isn't a string or a number; maybe it's a list?
441 456
         try:
442 457
             if len(value) != 1:
443  
-                return 's'
  458
+                return plural_suffix
444 459
         except TypeError: # len() of unsized object
445 460
             pass
446  
-    return ''
  461
+    return singular_suffix
447 462
 
448 463
 def phone2numeric(value):
449 464
     "Takes a phone number and converts it in to its numerical equivalent"
16  docs/templates.txt
@@ -951,12 +951,26 @@ any string.
951 951
 pluralize
952 952
 ~~~~~~~~~
953 953
 
954  
-Returns ``'s'`` if the value is not 1.
  954
+Returns a plural suffix if the value is not 1. By default, this suffix is ``'s'``. 
955 955
 
956 956
 Example::
957 957
 
958 958
     You have {{ num_messages }} message{{ num_messages|pluralize }}.
959 959
 
  960
+For words that require a suffix other than ``'s'``, you can provide an alternate 
  961
+suffix as a parameter to the filter.
  962
+
  963
+Example::
  964
+
  965
+    You have {{ num_walruses }} walrus{{ num_walrus|pluralize:"es" }}.
  966
+
  967
+For words that don't pluralize by simple suffix, you can specify both a
  968
+singular and plural suffix, separated by a comma.
  969
+
  970
+Example::
  971
+
  972
+    You have {{ num_cherries }} cherr{{ num_cherries|pluralize:"y,ies" }}.
  973
+
960 974
 pprint
961 975
 ~~~~~~
962 976
 
30  tests/othertests/defaultfilters.py
@@ -313,6 +313,36 @@
313 313
 >>> pluralize(2)
314 314
 's'
315 315
 
  316
+>>> pluralize([1])
  317
+''
  318
+
  319
+>>> pluralize([])
  320
+'s'
  321
+
  322
+>>> pluralize([1,2,3])
  323
+'s'
  324
+
  325
+>>> pluralize(1,'es')
  326
+''
  327
+
  328
+>>> pluralize(0,'es')
  329
+'es'
  330
+
  331
+>>> pluralize(2,'es')
  332
+'es'
  333
+
  334
+>>> pluralize(1,'y,ies')
  335
+'y'
  336
+
  337
+>>> pluralize(0,'y,ies')
  338
+'ies'
  339
+
  340
+>>> pluralize(2,'y,ies')
  341
+'ies'
  342
+
  343
+>>> pluralize(0,'y,ies,error')
  344
+''
  345
+
316 346
 >>> phone2numeric('0800 flowers')
317 347
 '0800 3569377'
318 348
 

0 notes on commit b9d9351

Please sign in to comment.
Something went wrong with that request. Please try again.