Django

Code

Changeset 3272

Show
Ignore:
Timestamp:
07/04/06 01:18:39 (3 years ago)
Author:
russellm
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/template/defaultfilters.py

    r3185 r3272  
    431431    return "%.1f GB" % (bytes / (1024 * 1024 * 1024)) 
    432432 
    433 def pluralize(value): 
    434     "Returns 's' if the value is not 1, for '1 vote' vs. '2 votes'" 
     433def 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     
    435450    try: 
    436451        if int(value) != 1: 
    437             return 's' 
     452            return plural_suffix 
    438453    except ValueError: # invalid string that's not a number 
    439454        pass 
     
    441456        try: 
    442457            if len(value) != 1: 
    443                 return 's' 
     458                return plural_suffix 
    444459        except TypeError: # len() of unsized object 
    445460            pass 
    446     return '' 
     461    return singular_suffix 
    447462 
    448463def phone2numeric(value): 
  • django/trunk/docs/templates.txt

    r3185 r3272  
    952952~~~~~~~~~ 
    953953 
    954 Returns ``'s'`` if the value is not 1. 
     954Returns a plural suffix if the value is not 1. By default, this suffix is ``'s'``.  
    955955 
    956956Example:: 
    957957 
    958958    You have {{ num_messages }} message{{ num_messages|pluralize }}. 
     959 
     960For words that require a suffix other than ``'s'``, you can provide an alternate  
     961suffix as a parameter to the filter. 
     962 
     963Example:: 
     964 
     965    You have {{ num_walruses }} walrus{{ num_walrus|pluralize:"es" }}. 
     966 
     967For words that don't pluralize by simple suffix, you can specify both a 
     968singular and plural suffix, separated by a comma. 
     969 
     970Example:: 
     971 
     972    You have {{ num_cherries }} cherr{{ num_cherries|pluralize:"y,ies" }}. 
    959973 
    960974pprint 
  • django/trunk/tests/othertests/defaultfilters.py

    r2809 r3272  
    314314's' 
    315315 
     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 
    316346>>> phone2numeric('0800 flowers') 
    317347'0800 3569377'