Ticket #6392: 6392.2.diff
File 6392.2.diff, 6.5 KB (added by , 14 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 eee2b4d..94815c3 100644
a b 1 1 from django.utils.translation import ungettext, ugettext as _ 2 2 from django.utils.encoding import force_unicode 3 from django.utils.formats import number_format 3 4 from django import template 4 5 from django.template import defaultfilters 6 from django.conf import settings 5 7 from datetime import date, datetime 6 8 import re 7 9 … … 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): 49 53 return value 50 54 if value < 1000000: 51 55 return value 56 57 def _check_for_i18n(value, float_formatted, string_formatted): 58 """ 59 Use the i18n enabled defaultfilters.floatformat if possible 60 """ 61 if settings.USE_L10N: 62 return defaultfilters.floatformat(value, 1), string_formatted 63 return value, float_formatted 64 52 65 if value < 1000000000: 53 66 new_value = value / 1000000.0 54 return ungettext('%(value).1f million', '%(value).1f million', new_value) % {'value': new_value} 67 new_value, value_string = _check_for_i18n(new_value, 68 ungettext('%(value).1f million', '%(value).1f million', new_value), 69 ungettext('%(value)s million', '%(value)s million', new_value)) 70 return value_string % {'value': new_value} 55 71 if value < 1000000000000: 56 72 new_value = value / 1000000000.0 57 return ungettext('%(value).1f billion', '%(value).1f billion', new_value) % {'value': new_value} 73 new_value, value_string = _check_for_i18n(new_value, 74 ungettext('%(value).1f billion', '%(value).1f billion', new_value), 75 ungettext('%(value)s billion', '%(value)s billion', new_value)) 76 return value_string % {'value': new_value} 58 77 if value < 1000000000000000: 59 78 new_value = value / 1000000000000.0 60 return ungettext('%(value).1f trillion', '%(value).1f trillion', new_value) % {'value': new_value} 79 new_value, value_string = _check_for_i18n(new_value, 80 ungettext('%(value).1f trillion', '%(value).1f trillion', new_value), 81 ungettext('%(value)s trillion', '%(value)s trillion', new_value)) 82 return value_string % {'value': new_value} 61 83 return value 62 84 intword.is_safe = False 63 85 register.filter(intword) -
docs/ref/contrib/humanize.txt
diff --git a/docs/ref/contrib/humanize.txt b/docs/ref/contrib/humanize.txt index 23b48dd..ad64885 100644
a b Examples: 43 43 * ``450000`` becomes ``450,000``. 44 44 * ``4500000`` becomes ``4,500,000``. 45 45 46 :ref:`Format localization <format-localization>` will be respected if enabled, 47 e.g. with the ``'de'`` language: 48 49 * ``45000`` becomes ``'45.000'``. 50 * ``450000`` becomes ``'450.000'``. 51 46 52 You can pass in either an integer or a string representation of an integer. 47 53 48 54 .. templatefilter:: intword … … Examples: 61 67 62 68 Values up to 1000000000000000 (one quadrillion) are supported. 63 69 70 :ref:`Format localization <format-localization>` will be respected if enabled, 71 e.g. with the ``'de'`` language: 72 73 * ``1000000`` becomes ``'1,0 Million'``. 74 * ``1200000`` becomes ``'1,2 Million'``. 75 * ``1200000000`` becomes ``'1,2 Milliarde'``. 76 64 77 You can pass in either an integer or a string representation of an integer. 65 78 66 79 .. templatefilter:: naturalday -
tests/regressiontests/humanize/tests.py
diff --git a/tests/regressiontests/humanize/tests.py b/tests/regressiontests/humanize/tests.py index ade0d1a..220fff6 100644
a b from datetime import timedelta, date, datetime, tzinfo, timedelta 3 3 from django.template import Template, Context, add_to_builtins 4 4 from django.utils import unittest 5 5 from django.utils.dateformat import DateFormat 6 from django.utils.translation import ugettext as _ 6 from django.utils.translation import ugettext as _, activate, deactivate 7 7 from django.utils.html import escape 8 from django.conf import settings 8 9 9 10 add_to_builtins('django.contrib.humanize.templatetags.humanize') 10 11 … … class HumanizeTests(unittest.TestCase): 58 59 59 60 self.humanize_tester(test_list, result_list, 'intcomma') 60 61 62 def test_i18n_intcomma(self): 63 test_list = (100, 1000, 10123, 10311, 1000000, 1234567.25, 64 '100', '1000', '10123', '10311', '1000000', '1234567.1234567') 65 result_list = ('100', '1.000', '10.123', '10.311', '1.000.000', '1.234.567,25', 66 '100', '1.000', '10.123', '10.311', '1.000.000', '1.234.567,1234567') 67 _use_l10n = settings.USE_L10N 68 _use_thousand_separator = settings.USE_THOUSAND_SEPARATOR 69 settings.USE_L10N = True 70 settings.USE_THOUSAND_SEPARATOR = True 71 try: 72 activate('de') 73 self.humanize_tester(test_list, result_list, 'intcomma') 74 finally: 75 settings.USE_L10N = _use_l10n 76 deactivate() 77 61 78 def test_intword(self): 62 79 test_list = ('100', '1000000', '1200000', '1290000', 63 80 '1000000000','2000000000','6000000000000', … … class HumanizeTests(unittest.TestCase): 68 85 69 86 self.humanize_tester(test_list, result_list, 'intword') 70 87 88 def test_i18n_intword(self): 89 test_list = ('100', '1000000', '1200000', '1290000', 90 '1000000000','2000000000','6000000000000') 91 result_list = ('100', '1,0 million', '1,2 million', '1,3 million', 92 '1,0 billion', '2,0 billion', '6,0 trillion') 93 _use_l10n = settings.USE_L10N 94 _use_thousand_separator = settings.USE_THOUSAND_SEPARATOR 95 settings.USE_L10N = True 96 settings.USE_THOUSAND_SEPARATOR = True 97 try: 98 activate('de') 99 self.humanize_tester(test_list, result_list, 'intword') 100 finally: 101 settings.USE_L10N = _use_l10n 102 settings.USE_THOUSAND_SEPARATOR = _use_thousand_separator 103 deactivate() 104 71 105 def test_apnumber(self): 72 106 test_list = [str(x) for x in range(1, 11)] 73 107 test_list.append(None)