Repository: zestedesavoir/zds-site

Issue:
[v20] Pluriel incorrect pour le nombre d'abonnés sur la page de profil
Via https://zestedesavoir.com/forums/sujet/6789/la-v20-mesdames-messieurs-attention-les-velos/?page=1#p121796

S'inspirer des autres compteurs du genre (sur le forum par exemple) pour corriger ça.



Discussion:

Resolving patch:
diff --git a/zds/utils/templatetags/pluralize_fr.py b/zds/utils/templatetags/pluralize_fr.py
new file mode 100644
--- /dev/null
+++ b/zds/utils/templatetags/pluralize_fr.py
@@ -0,0 +1,32 @@
+# coding: utf-8
+
+from django import template
+
+register = template.Library()
+
+
+@register.filter(is_safe=False)
+def pluralize_fr(value, arg='s'):
+    """
+    Based on default pluralize filter : https://github.com/django/django/blob/master/django/template/defaultfilters.py
+    Support french (-1, 0 and 1 are singular).
+    """
+    if ',' not in arg:
+        arg = ',' + arg
+    bits = arg.split(',')
+    if len(bits) > 2:
+        return ''
+    singular_suffix, plural_suffix = bits[:2]
+
+    try:
+        if not (-2 < float(value) < 2):
+            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 not (-2 < len(value) < 2):
+                return plural_suffix
+        except TypeError:  # len() of unsized object.
+            pass
+    return singular_suffix


Test patch:
diff --git a/zds/utils/templatetags/tests/tests_pluralize_fr.py b/zds/utils/templatetags/tests/tests_pluralize_fr.py
new file mode 100644
--- /dev/null
+++ b/zds/utils/templatetags/tests/tests_pluralize_fr.py
@@ -0,0 +1,42 @@
+# coding: utf-8
+
+# Based on default pluralize filter tests :
+# https://github.com/django/django/blob/master/tests/template_tests/filter_tests/test_pluralize.py
+
+from decimal import Decimal
+
+from django.test import SimpleTestCase
+
+from zds.utils.templatetags.pluralize_fr import pluralize_fr
+
+
+class FunctionTests(SimpleTestCase):
+
+    def test_integers(self):
+        self.assertEqual(pluralize_fr(1), '')
+        self.assertEqual(pluralize_fr(0), '')
+        self.assertEqual(pluralize_fr(2), 's')
+
+    def test_floats(self):
+        self.assertEqual(pluralize_fr(0.5), '')
+        self.assertEqual(pluralize_fr(1.5), '')
+        self.assertEqual(pluralize_fr(2.5), 's')
+
+    def test_decimals(self):
+        self.assertEqual(pluralize_fr(Decimal(1)), '')
+        self.assertEqual(pluralize_fr(Decimal(0)), '')
+        self.assertEqual(pluralize_fr(Decimal(2)), 's')
+
+    def test_lists(self):
+        self.assertEqual(pluralize_fr([1]), '')
+        self.assertEqual(pluralize_fr([]), '')
+        self.assertEqual(pluralize_fr([1, 2, 3]), 's')
+
+    def test_suffixes(self):
+        self.assertEqual(pluralize_fr(1, 'es'), '')
+        self.assertEqual(pluralize_fr(0, 'es'), '')
+        self.assertEqual(pluralize_fr(2, 'es'), 'es')
+        self.assertEqual(pluralize_fr(1, 'y,ies'), 'y')
+        self.assertEqual(pluralize_fr(0, 'y,ies'), 'y')
+        self.assertEqual(pluralize_fr(2, 'y,ies'), 'ies')
+        self.assertEqual(pluralize_fr(0, 'y,ies,error'), '')