Opened 11 years ago

Closed 11 years ago

Last modified 10 years ago

#794 closed defect (wontfix)

A template filter that lets the user choose pluralization method

Reported by: ozamosi@… Owned by: Adrian Holovaty
Component: Template system Version:
Severity: minor Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


The current pluralization in the default filter adds an s if it should pluralize. While this is often good for english, it is not always, and it is generally not usefull for any other language. Therefore, I wrote a custom pluralization filter that lets the template author specify what the filter should return if it should return anything at all.

Here is the diff:

Index: django/core/template/
--- django/core/template/      (revision 1124)
+++ django/core/template/      (arbetskopia)
@@ -408,6 +408,22 @@
     return ''

+def custompluralize(value, arg):
+    """Returns the arg if the value is not 1. Like above, but with
+    customizable pluralization. Good for i18n"""
+    try:
+        if int(value) != 1:
+            return arg
+    except ValueError: # invalid string that's not a number
+        pass
+    except TypeError: # value isn't a string or a number, maybe it is a list?
+        try:
+            if len(value) != 1:
+                return arg
+        except TypeError: # len() of unsized object
+            pass
+    return ''
 def phone2numeric(value, _):
     "Takes a phone number and converts it in to its numerical equivalent"
     from django.utils.text import phone2numeric
@@ -447,6 +463,7 @@
 register_filter('make_list', make_list, False)
 register_filter('phone2numeric', phone2numeric, False)
 register_filter('pluralize', pluralize, False)
+register_filter('custompluralize', custompluralize, True)
 register_filter('pprint', pprint, False)
 register_filter('removetags', removetags, True)
 register_filter('random', random, False) 

Change History (2)

comment:1 Changed 11 years ago by hugo

Actually this is not really usefull for i18n - languages have different handling of pluralization that goes far beyoned simple "attach string" situations. The pluralization is handled by the {% blocktrasn %} {% plural %} {% endblocktrans %} tag and by the power of gettext, which handles the actual different plural algorithms.

comment:2 Changed 11 years ago by Adrian Holovaty

Resolution: wontfix
Status: newclosed

Feel free to use this in your own projects, but it's not going into the Django distribution. See Hugo's comment above.

Note: See TracTickets for help on using tickets.
Back to Top