Ticket #6392: 6392.2.diff

File 6392.2.diff, 6.5 KB (added by dmclain, 4 years ago)

Slight tweak to get patch to apply cleanly

  • 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  
    11from django.utils.translation import ungettext, ugettext as _
    22from django.utils.encoding import force_unicode
     3from django.utils.formats import number_format
    34from django import template
    45from django.template import defaultfilters
     6from django.conf import settings
    57from datetime import date, datetime
    68import re
    79
    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): 
    4953        return value
    5054    if value < 1000000:
    5155        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
    5265    if value < 1000000000:
    5366        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}
    5571    if value < 1000000000000:
    5672        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}
    5877    if value < 1000000000000000:
    5978        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}
    6183    return value
    6284intword.is_safe = False
    6385register.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: 
    4343    * ``450000`` becomes ``450,000``.
    4444    * ``4500000`` becomes ``4,500,000``.
    4545
     46:ref:`Format localization <format-localization>` will be respected if enabled,
     47e.g. with the ``'de'`` language:
     48
     49    * ``45000`` becomes ``'45.000'``.
     50    * ``450000`` becomes ``'450.000'``.
     51
    4652You can pass in either an integer or a string representation of an integer.
    4753
    4854.. templatefilter:: intword
    Examples: 
    6167
    6268Values up to 1000000000000000 (one quadrillion) are supported.
    6369
     70:ref:`Format localization <format-localization>` will be respected if enabled,
     71e.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
    6477You can pass in either an integer or a string representation of an integer.
    6578
    6679.. 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 
    33from django.template import Template, Context, add_to_builtins
    44from django.utils import unittest
    55from django.utils.dateformat import DateFormat
    6 from django.utils.translation import ugettext as _
     6from django.utils.translation import ugettext as _, activate, deactivate
    77from django.utils.html import escape
     8from django.conf import settings
    89
    910add_to_builtins('django.contrib.humanize.templatetags.humanize')
    1011
    class HumanizeTests(unittest.TestCase): 
    5859
    5960        self.humanize_tester(test_list, result_list, 'intcomma')
    6061
     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
    6178    def test_intword(self):
    6279        test_list = ('100', '1000000', '1200000', '1290000',
    6380                     '1000000000','2000000000','6000000000000',
    class HumanizeTests(unittest.TestCase): 
    6885
    6986        self.humanize_tester(test_list, result_list, 'intword')
    7087
     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
    71105    def test_apnumber(self):
    72106        test_list = [str(x) for x in range(1, 11)]
    73107        test_list.append(None)
Back to Top