Ticket #2202: optional_arguments_to_pluralize.patch
File optional_arguments_to_pluralize.patch, 2.1 KB (added by , 18 years ago) |
---|
-
django/template/defaultfilters.py
430 430 return "%.1f MB" % (bytes / (1024 * 1024)) 431 431 return "%.1f GB" % (bytes / (1024 * 1024 * 1024)) 432 432 433 def pluralize(value ):433 def pluralize(value, arg='s'): 434 434 "Returns 's' if the value is not 1, for '1 vote' vs. '2 votes'" 435 if not ',' in arg: 436 arg = arg + ',' 437 plural, single = arg.split(',', 1) 435 438 try: 436 439 if int(value) != 1: 437 return 's'440 return plural 438 441 except ValueError: # invalid string that's not a number 439 442 pass 440 443 except TypeError: # value isn't a string or a number; maybe it's a list? 441 444 try: 442 445 if len(value) != 1: 443 return 's'446 return plural 444 447 except TypeError: # len() of unsized object 445 448 pass 446 return ''449 return single 447 450 448 451 def phone2numeric(value): 449 452 "Takes a phone number and converts it in to its numerical equivalent" -
docs/templates.txt
953 953 954 954 Returns ``'s'`` if the value is not 1. 955 955 956 Example:: 956 Use the optional argument to give an alternate plural. You can also add 957 the singlar alternative with a comma: ``"knives,knife"`` 958 959 Examples:: 957 960 958 961 You have {{ num_messages }} message{{ num_messages|pluralize }}. 962 963 Managed by {{ bosses }} boss{{ bosses|pluralize:"es" }}. 964 965 I know {{ female_count }} {{ female_count|pluralize:"women,woman" }}. 959 966 960 967 pprint 961 968 ~~~~~~ -
tests/othertests/defaultfilters.py
313 313 >>> pluralize(2) 314 314 's' 315 315 316 >>> pluralize(2, 'es') 317 'es' 318 319 >>> pluralize(1, 'men,man') 320 'man' 321 322 >>> pluralize(5, 'men,man') 323 'men' 324 316 325 >>> phone2numeric('0800 flowers') 317 326 '0800 3569377' 318 327