Ticket #6392: humanize-numbers-locale.patch
File humanize-numbers-locale.patch, 5.3 KB (added by , 16 years ago) |
---|
-
django/contrib/humanize/templatetags/humanize.py
1 import re 2 import locale 1 3 from django.utils.translation import ungettext, ugettext as _ 2 4 from django.utils.encoding import force_unicode 3 5 from django import template … … 3 5 from django.template import defaultfilters 4 6 from datetime import date 5 import re6 7 7 8 register = template.Library() … … 29 30 For example, 3000 becomes '3,000' and 45000 becomes '45,000'. 30 31 """ 31 32 orig = force_unicode(value) 32 new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', orig) 33 sep = locale.localeconv()['thousands_sep'] or ',' 34 new = re.sub("^(-?\d+)(\d{3})", '\g<1>' + sep + '\g<2>', orig) 33 35 if orig == new: 34 36 return new 35 37 else: … … 37 39 intcomma.is_safe = True 38 40 register.filter(intcomma) 39 41 42 def _intword_format(format, value): 43 """ 44 Localizes the format string and takes care of placing the correct 45 decimal separator using locale. 46 """ 47 format = ungettext(format, format, value) 48 return locale.format(format.replace('%(value)', '%'), value) 49 40 50 def intword(value): 41 51 """ 42 52 Converts a large integer to a friendly text representation. Works best for … … 48 58 return value 49 59 if value < 1000000000: 50 60 new_value = value / 1000000.0 51 return ungettext('%(value).1f million', '%(value).1f million', new_value) % {'value': new_value}61 return _intword_format('%(value).1f million', new_value) 52 62 if value < 1000000000000: 53 63 new_value = value / 1000000000.0 54 return ungettext('%(value).1f billion', '%(value).1f billion', new_value) % {'value': new_value}64 return _intword_format('%(value).1f billion', new_value) 55 65 if value < 1000000000000000: 56 66 new_value = value / 1000000000000.0 57 return ungettext('%(value).1f trillion', '%(value).1f trillion', new_value) % {'value': new_value}67 return _intword_format('%(value).1f trillion', new_value) 58 68 return value 59 69 intword.is_safe = False 60 70 register.filter(intword) … … 80 90 present day returns representing string. Otherwise, returns a string 81 91 formatted according to settings.DATE_FORMAT. 82 92 """ 83 try: 93 try: 84 94 value = date(value.year, value.month, value.day) 85 95 except AttributeError: 86 96 # Passed value wasn't a date object -
tests/regressiontests/humanize/tests.py
1 import locale 1 2 import unittest 2 3 from datetime import timedelta, date 3 4 from django.template import Template, Context, add_to_builtins … … 37 38 38 39 self.humanize_tester(test_list, result_list, 'intcomma') 39 40 41 try: 42 loc = locale.getlocale() 43 locale.setlocale(locale.LC_ALL, 'de_DE') 44 result_list = ('100', '1.000', '10.123', '10.311', '1.000.000', '1.234.567,25', 45 '100', '1.000', '10.123', '10.311', '1.000.000', '1.234.567,1234567') 46 47 self.humanize_tester(test_list, result_list, 'intcomma') 48 locale.setlocale(locale.LC_ALL, loc) 49 except locale.Error: 50 pass 51 40 52 def test_intword(self): 41 53 test_list = ('100', '1000000', '1200000', '1290000', 42 54 '1000000000','2000000000','6000000000000') … … 45 57 46 58 self.humanize_tester(test_list, result_list, 'intword') 47 59 60 try: 61 loc = locale.getlocale() 62 locale.setlocale(locale.LC_ALL, 'de_DE') 63 result_list = ('100', '1,0 million', '1,2 million', 64 '1,3 million', '2,0 billion', '6,0 trillion') 65 66 self.humanize_tester(test_list, result_list, 'intword') 67 locale.setlocale(locale.LC_ALL, loc) 68 except locale.Error: 69 pass 70 48 71 def test_apnumber(self): 49 72 test_list = [str(x) for x in range(1, 11)] 50 73 result_list = (u'one', u'two', u'three', u'four', u'five', u'six', -
docs/ref/contrib/humanize.txt
40 40 * ``450000`` becomes ``'450,000'``. 41 41 * ``4500000`` becomes ``'4,500,000'``. 42 42 43 If you have set a locale through ``'locale.setlocale()'``, it respects your 44 settings. 45 46 Examples for the ``'de_DE'`` locale: 47 48 * ``45000`` becomes ``'45,000'``. 49 * ``450000`` becomes ``'450,000'``. 50 43 51 You can pass in either an integer or a string representation of an integer. 44 52 45 53 intword … … 56 64 57 65 Values up to 1000000000000000 (one quadrillion) are supported. 58 66 67 If you have set a locale through ``'locale.setlocale()'``, it respects your 68 settings. 69 70 Examples for the ``'de_DE'`` locale: 71 72 * ``1000000`` becomes ``'1,0 million'``. 73 * ``1200000`` becomes ``'1,2 million'``. 74 * ``1200000000`` becomes ``'1,2 billion'``. 75 59 76 You can pass in either an integer or a string representation of an integer. 60 77 61 78 ordinal