Ticket #20122: defaultfilters_fix_pluralize.diff

File defaultfilters_fix_pluralize.diff, 1.8 KB (added by Jorge C. Leitão, 6 years ago)
  • django/template/defaultfilters.py

    diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py
    index 4201cfe..526593d 100644
    a b def filesizeformat(bytes): 
    839839@register.filter(is_safe=False)
    840840def pluralize(value, arg='s'):
    841841    """
    842     Returns a plural suffix if the value is not 1. By default, 's' is used as
    843     the suffix:
     842    Returns a singular suffix if the value is 1, '1' or an object of len 1,
     843    plural suffix for different than 1, '1' or len != 1 and ''
     844    if one cannot quantify the value in either singular or plural.
     845    By default, 's' is used as the suffix:
    844846
    845847    * If value is 0, vote{{ value|pluralize }} displays "0 votes".
    846848    * If value is 1, vote{{ value|pluralize }} displays "1 vote".
    def pluralize(value, arg='s'): 
    867869        return ''
    868870    singular_suffix, plural_suffix = bits[:2]
    869871
     872    if value == 1 or value == '1':
     873        return singular_suffix
    870874    try:
    871         if int(value) != 1:
    872             return plural_suffix
    873     except ValueError: # Invalid string that's not a number.
     875        float(value)
     876        return plural_suffix
     877    except ValueError: # String that cannot be converted to a numerical value
    874878        pass
    875     except TypeError: # Value isn't a string or a number; maybe it's a list?
     879    except TypeError: # Value isn't a string nor a number; maybe it's a list?
    876880        try:
    877881            if len(value) != 1:
    878882                return plural_suffix
    879         except TypeError: # len() of unsized object.
     883            else:
     884                return singular_suffix
     885        except TypeError: # Value is an unsized object
    880886            pass
    881     return singular_suffix
     887    return ''
    882888
    883889@register.filter("phone2numeric", is_safe=True)
    884890def phone2numeric_filter(value):
Back to Top