Ticket #6392: 6392.diff
File 6392.diff, 6.5 KB (added by , 15 years ago) |
---|
-
django/contrib/humanize/templatetags/humanize.py
diff --git a/django/contrib/humanize/templatetags/humanize.py b/django/contrib/humanize/templatetags/humanize.py index 05d3223..5baf003 100644
a b 1 import re 2 from datetime import date 1 3 from django.utils.translation import ungettext, ugettext as _ 2 4 from django.utils.encoding import force_unicode 5 from django.utils.formats import number_format 3 6 from django import template 4 7 from django.template import defaultfilters 5 from datetime import date 6 import re 8 from django.conf import settings 7 9 8 10 register = template.Library() 9 11 … … def intcomma(value): 28 30 Converts an integer to a string containing commas every three digits. 29 31 For example, 3000 becomes '3,000' and 45000 becomes '45,000'. 30 32 """ 33 if settings.USE_L10N: 34 return number_format(value) 31 35 orig = force_unicode(value) 32 36 new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', orig) 33 37 if orig == new: … … def intword(value): 46 50 value = int(value) 47 51 if value < 1000000: 48 52 return value 53 54 def _check_for_i18n(value, float_formatted, string_formatted): 55 """ 56 Use the i18n enabled defaultfilters.floatformat if possible 57 """ 58 if settings.USE_L10N: 59 return defaultfilters.floatformat(value, 1), string_formatted 60 return value, float_formatted 61 49 62 if value < 1000000000: 50 63 new_value = value / 1000000.0 51 return ungettext('%(value).1f million', '%(value).1f million', new_value) % {'value': new_value} 64 new_value, value_string = _check_for_i18n(new_value, 65 ungettext('%(value).1f million', '%(value).1f million', new_value), 66 ungettext('%(value)s million', '%(value)s million', new_value)) 67 return value_string % {'value': new_value} 52 68 if value < 1000000000000: 53 69 new_value = value / 1000000000.0 54 return ungettext('%(value).1f billion', '%(value).1f billion', new_value) % {'value': new_value} 70 new_value, value_string = _check_for_i18n(new_value, 71 ungettext('%(value).1f billion', '%(value).1f billion', new_value), 72 ungettext('%(value)s billion', '%(value)s billion', new_value)) 73 return value_string % {'value': new_value} 55 74 if value < 1000000000000000: 56 75 new_value = value / 1000000000000.0 57 return ungettext('%(value).1f trillion', '%(value).1f trillion', new_value) % {'value': new_value} 76 new_value, value_string = _check_for_i18n(new_value, 77 ungettext('%(value).1f trillion', '%(value).1f trillion', new_value), 78 ungettext('%(value)s trillion', '%(value)s trillion', new_value)) 79 return value_string % {'value': new_value} 58 80 return value 59 81 intword.is_safe = False 60 82 register.filter(intword) -
docs/ref/contrib/humanize.txt
diff --git a/docs/ref/contrib/humanize.txt b/docs/ref/contrib/humanize.txt index 07a62a7..fe90150 100644
a b Examples: 40 40 * ``450000`` becomes ``'450,000'``. 41 41 * ``4500000`` becomes ``'4,500,000'``. 42 42 43 :ref:`Format localization <format-localization>` will be respected if enabled, 44 e.g. with the ``'de'`` language: 45 46 * ``45000`` becomes ``'45.000'``. 47 * ``450000`` becomes ``'450.000'``. 48 43 49 You can pass in either an integer or a string representation of an integer. 44 50 45 51 intword … … Examples: 56 62 57 63 Values up to 1000000000000000 (one quadrillion) are supported. 58 64 65 :ref:`Format localization <format-localization>` will be respected if enabled, 66 e.g. with the ``'de'`` language: 67 68 * ``1000000`` becomes ``'1,0 Million'``. 69 * ``1200000`` becomes ``'1,2 Million'``. 70 * ``1200000000`` becomes ``'1,2 Milliarde'``. 71 59 72 You can pass in either an integer or a string representation of an integer. 60 73 61 74 ordinal -
tests/regressiontests/humanize/tests.py
diff --git a/tests/regressiontests/humanize/tests.py b/tests/regressiontests/humanize/tests.py index 6f60c6d..097bcb7 100644
a b import unittest 2 2 from datetime import timedelta, date 3 3 from django.template import Template, Context, add_to_builtins 4 4 from django.utils.dateformat import DateFormat 5 from django.utils.translation import ugettext as _ 5 from django.utils.translation import ugettext as _, activate, deactivate 6 6 from django.utils.html import escape 7 from django.conf import settings 7 8 8 9 add_to_builtins('django.contrib.humanize.templatetags.humanize') 9 10 … … class HumanizeTests(unittest.TestCase): 37 38 38 39 self.humanize_tester(test_list, result_list, 'intcomma') 39 40 41 def test_i18n_intcomma(self): 42 test_list = (100, 1000, 10123, 10311, 1000000, 1234567.25, 43 '100', '1000', '10123', '10311', '1000000', '1234567.1234567') 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 _use_l10n = settings.USE_L10N 47 _use_thousand_separator = settings.USE_THOUSAND_SEPARATOR 48 settings.USE_L10N = True 49 settings.USE_THOUSAND_SEPARATOR = True 50 try: 51 activate('de') 52 self.humanize_tester(test_list, result_list, 'intcomma') 53 finally: 54 settings.USE_L10N = _use_l10n 55 deactivate() 56 40 57 def test_intword(self): 41 58 test_list = ('100', '1000000', '1200000', '1290000', 42 59 '1000000000','2000000000','6000000000000') … … class HumanizeTests(unittest.TestCase): 45 62 46 63 self.humanize_tester(test_list, result_list, 'intword') 47 64 65 def test_i18n_intword(self): 66 test_list = ('100', '1000000', '1200000', '1290000', 67 '1000000000','2000000000','6000000000000') 68 result_list = ('100', '1,0 million', '1,2 million', '1,3 million', 69 '1,0 billion', '2,0 billion', '6,0 trillion') 70 _use_l10n = settings.USE_L10N 71 _use_thousand_separator = settings.USE_THOUSAND_SEPARATOR 72 settings.USE_L10N = True 73 settings.USE_THOUSAND_SEPARATOR = True 74 try: 75 activate('de') 76 self.humanize_tester(test_list, result_list, 'intword') 77 finally: 78 settings.USE_L10N = _use_l10n 79 settings.USE_THOUSAND_SEPARATOR = _use_thousand_separator 80 deactivate() 81 48 82 def test_apnumber(self): 49 83 test_list = [str(x) for x in range(1, 11)] 50 84 result_list = (u'one', u'two', u'three', u'four', u'five', u'six',