Changeset 3272
- Timestamp:
- 07/04/06 01:18:39 (2 years ago)
- Files:
-
- django/trunk/django/template/defaultfilters.py (modified) (2 diffs)
- django/trunk/docs/templates.txt (modified) (1 diff)
- django/trunk/tests/othertests/defaultfilters.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/template/defaultfilters.py
r3185 r3272 431 431 return "%.1f GB" % (bytes / (1024 * 1024 * 1024)) 432 432 433 def pluralize(value): 434 "Returns 's' if the value is not 1, for '1 vote' vs. '2 votes'" 433 def 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 435 450 try: 436 451 if int(value) != 1: 437 return 's'452 return plural_suffix 438 453 except ValueError: # invalid string that's not a number 439 454 pass … … 441 456 try: 442 457 if len(value) != 1: 443 return 's'458 return plural_suffix 444 459 except TypeError: # len() of unsized object 445 460 pass 446 return ''461 return singular_suffix 447 462 448 463 def phone2numeric(value): django/trunk/docs/templates.txt
r3185 r3272 952 952 ~~~~~~~~~ 953 953 954 Returns ``'s'`` if the value is not 1.954 Returns a plural suffix if the value is not 1. By default, this suffix is ``'s'``. 955 955 956 956 Example:: 957 957 958 958 You have {{ num_messages }} message{{ num_messages|pluralize }}. 959 960 For words that require a suffix other than ``'s'``, you can provide an alternate 961 suffix as a parameter to the filter. 962 963 Example:: 964 965 You have {{ num_walruses }} walrus{{ num_walrus|pluralize:"es" }}. 966 967 For words that don't pluralize by simple suffix, you can specify both a 968 singular and plural suffix, separated by a comma. 969 970 Example:: 971 972 You have {{ num_cherries }} cherr{{ num_cherries|pluralize:"y,ies" }}. 959 973 960 974 pprint django/trunk/tests/othertests/defaultfilters.py
r2809 r3272 314 314 's' 315 315 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 316 346 >>> phone2numeric('0800 flowers') 317 347 '0800 3569377'
