Ticket #6392: 6392.diff

File 6392.diff, 6.5 KB (added by Jannis Leidel, 15 years ago)

Updated patch which uses locale-aware formatting.

  • 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  
     1import re
     2from datetime import date
    13from django.utils.translation import ungettext, ugettext as _
    24from django.utils.encoding import force_unicode
     5from django.utils.formats import number_format
    36from django import template
    47from django.template import defaultfilters
    5 from datetime import date
    6 import re
     8from django.conf import settings
    79
    810register = template.Library()
    911
    def intcomma(value):  
    2830    Converts an integer to a string containing commas every three digits.
    2931    For example, 3000 becomes '3,000' and 45000 becomes '45,000'.
    3032    """
     33    if settings.USE_L10N:
     34        return number_format(value)
    3135    orig = force_unicode(value)
    3236    new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', orig)
    3337    if orig == new:
    def intword(value):  
    4650    value = int(value)
    4751    if value < 1000000:
    4852        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
    4962    if value < 1000000000:
    5063        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}
    5268    if value < 1000000000000:
    5369        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}
    5574    if value < 1000000000000000:
    5675        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}
    5880    return value
    5981intword.is_safe = False
    6082register.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:  
    4040    * ``450000`` becomes ``'450,000'``.
    4141    * ``4500000`` becomes ``'4,500,000'``.
    4242
     43:ref:`Format localization <format-localization>` will be respected if enabled,
     44e.g. with the ``'de'`` language:
     45
     46    * ``45000`` becomes ``'45.000'``.
     47    * ``450000`` becomes ``'450.000'``.
     48
    4349You can pass in either an integer or a string representation of an integer.
    4450
    4551intword
    Examples:  
    5662
    5763Values up to 1000000000000000 (one quadrillion) are supported.
    5864
     65:ref:`Format localization <format-localization>` will be respected if enabled,
     66e.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
    5972You can pass in either an integer or a string representation of an integer.
    6073
    6174ordinal
  • 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  
    22from datetime import timedelta, date
    33from django.template import Template, Context, add_to_builtins
    44from django.utils.dateformat import DateFormat
    5 from django.utils.translation import ugettext as _
     5from django.utils.translation import ugettext as _, activate, deactivate
    66from django.utils.html import escape
     7from django.conf import settings
    78
    89add_to_builtins('django.contrib.humanize.templatetags.humanize')
    910
    class HumanizeTests(unittest.TestCase):  
    3738
    3839        self.humanize_tester(test_list, result_list, 'intcomma')
    3940
     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
    4057    def test_intword(self):
    4158        test_list = ('100', '1000000', '1200000', '1290000',
    4259                     '1000000000','2000000000','6000000000000')
    class HumanizeTests(unittest.TestCase):  
    4562
    4663        self.humanize_tester(test_list, result_list, 'intword')
    4764
     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
    4882    def test_apnumber(self):
    4983        test_list = [str(x) for x in range(1, 11)]
    5084        result_list = (u'one', u'two', u'three', u'four', u'five', u'six',
Back to Top