Ticket #6392: humanize-numbers-locale.patch

File humanize-numbers-locale.patch, 5.3 KB (added by hlian, 7 years ago)

An initial patch

  • django/contrib/humanize/templatetags/humanize.py

     
     1import re
     2import locale
    13from django.utils.translation import ungettext, ugettext as _
    24from django.utils.encoding import force_unicode
    35from django import template
     
    35from django.template import defaultfilters
    46from datetime import date
    5 import re
    67
    78register = template.Library()
     
    2930    For example, 3000 becomes '3,000' and 45000 becomes '45,000'.
    3031    """
    3132    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)
    3335    if orig == new:
    3436        return new
    3537    else:
     
    3739intcomma.is_safe = True
    3840register.filter(intcomma)
    3941
     42def _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
    4050def intword(value):
    4151    """
    4252    Converts a large integer to a friendly text representation. Works best for
     
    4858        return value
    4959    if value < 1000000000:
    5060        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)
    5262    if value < 1000000000000:
    5363        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)
    5565    if value < 1000000000000000:
    5666        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)
    5868    return value
    5969intword.is_safe = False
    6070register.filter(intword)
     
    8090    present day returns representing string. Otherwise, returns a string
    8191    formatted according to settings.DATE_FORMAT.
    8292    """
    83     try: 
     93    try:
    8494        value = date(value.year, value.month, value.day)
    8595    except AttributeError:
    8696        # Passed value wasn't a date object
  • tests/regressiontests/humanize/tests.py

     
     1import locale
    12import unittest
    23from datetime import timedelta, date
    34from django.template import Template, Context, add_to_builtins
     
    3738
    3839        self.humanize_tester(test_list, result_list, 'intcomma')
    3940
     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
    4052    def test_intword(self):
    4153        test_list = ('100', '1000000', '1200000', '1290000',
    4254                     '1000000000','2000000000','6000000000000')
     
    4557
    4658        self.humanize_tester(test_list, result_list, 'intword')
    4759
     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
    4871    def test_apnumber(self):
    4972        test_list = [str(x) for x in range(1, 11)]
    5073        result_list = (u'one', u'two', u'three', u'four', u'five', u'six',
  • docs/ref/contrib/humanize.txt

     
    4040    * ``450000`` becomes ``'450,000'``.
    4141    * ``4500000`` becomes ``'4,500,000'``.
    4242
     43If you have set a locale through ``'locale.setlocale()'``, it respects your
     44settings.
     45
     46Examples for the ``'de_DE'`` locale:
     47
     48    * ``45000`` becomes ``'45,000'``.
     49    * ``450000`` becomes ``'450,000'``.
     50
    4351You can pass in either an integer or a string representation of an integer.
    4452
    4553intword
     
    5664
    5765Values up to 1000000000000000 (one quadrillion) are supported.
    5866
     67If you have set a locale through ``'locale.setlocale()'``, it respects your
     68settings.
     69
     70Examples 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
    5976You can pass in either an integer or a string representation of an integer.
    6077
    6178ordinal
Back to Top