Made intword/intcomma template filters independent of USE_L10N.

This commit is contained in:
Claude Paroz 2020-05-22 20:39:47 +02:00 committed by Mariusz Felisiak
parent 7514852767
commit bd4e409695
2 changed files with 20 additions and 60 deletions

View File

@ -3,7 +3,6 @@ from datetime import date, datetime
from decimal import Decimal from decimal import Decimal
from django import template from django import template
from django.conf import settings
from django.template import defaultfilters from django.template import defaultfilters
from django.utils.formats import number_format from django.utils.formats import number_format
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
@ -63,14 +62,14 @@ def intcomma(value, use_l10n=True):
Convert an integer to a string containing commas every three digits. Convert an integer to a string containing commas every three digits.
For example, 3000 becomes '3,000' and 45000 becomes '45,000'. For example, 3000 becomes '3,000' and 45000 becomes '45,000'.
""" """
if settings.USE_L10N and use_l10n: if use_l10n:
try: try:
if not isinstance(value, (float, Decimal)): if not isinstance(value, (float, Decimal)):
value = int(value) value = int(value)
except (TypeError, ValueError): except (TypeError, ValueError):
return intcomma(value, False) return intcomma(value, False)
else: else:
return number_format(value, force_grouping=True) return number_format(value, use_l10n=True, force_grouping=True)
orig = str(value) orig = str(value)
new = re.sub(r"^(-?\d+)(\d{3})", r'\g<1>,\g<2>', orig) new = re.sub(r"^(-?\d+)(\d{3})", r'\g<1>,\g<2>', orig)
if orig == new: if orig == new:
@ -81,50 +80,17 @@ def intcomma(value, use_l10n=True):
# A tuple of standard large number to their converters # A tuple of standard large number to their converters
intword_converters = ( intword_converters = (
(6, lambda number: ( (6, lambda number: ngettext('%(value)s million', '%(value)s million', number)),
ngettext('%(value).1f million', '%(value).1f million', number), (9, lambda number: ngettext('%(value)s billion', '%(value)s billion', number)),
ngettext('%(value)s million', '%(value)s million', number), (12, lambda number: ngettext('%(value)s trillion', '%(value)s trillion', number)),
)), (15, lambda number: ngettext('%(value)s quadrillion', '%(value)s quadrillion', number)),
(9, lambda number: ( (18, lambda number: ngettext('%(value)s quintillion', '%(value)s quintillion', number)),
ngettext('%(value).1f billion', '%(value).1f billion', number), (21, lambda number: ngettext('%(value)s sextillion', '%(value)s sextillion', number)),
ngettext('%(value)s billion', '%(value)s billion', number), (24, lambda number: ngettext('%(value)s septillion', '%(value)s septillion', number)),
)), (27, lambda number: ngettext('%(value)s octillion', '%(value)s octillion', number)),
(12, lambda number: ( (30, lambda number: ngettext('%(value)s nonillion', '%(value)s nonillion', number)),
ngettext('%(value).1f trillion', '%(value).1f trillion', number), (33, lambda number: ngettext('%(value)s decillion', '%(value)s decillion', number)),
ngettext('%(value)s trillion', '%(value)s trillion', number), (100, lambda number: ngettext('%(value)s googol', '%(value)s googol', number)),
)),
(15, lambda number: (
ngettext('%(value).1f quadrillion', '%(value).1f quadrillion', number),
ngettext('%(value)s quadrillion', '%(value)s quadrillion', number),
)),
(18, lambda number: (
ngettext('%(value).1f quintillion', '%(value).1f quintillion', number),
ngettext('%(value)s quintillion', '%(value)s quintillion', number),
)),
(21, lambda number: (
ngettext('%(value).1f sextillion', '%(value).1f sextillion', number),
ngettext('%(value)s sextillion', '%(value)s sextillion', number),
)),
(24, lambda number: (
ngettext('%(value).1f septillion', '%(value).1f septillion', number),
ngettext('%(value)s septillion', '%(value)s septillion', number),
)),
(27, lambda number: (
ngettext('%(value).1f octillion', '%(value).1f octillion', number),
ngettext('%(value)s octillion', '%(value)s octillion', number),
)),
(30, lambda number: (
ngettext('%(value).1f nonillion', '%(value).1f nonillion', number),
ngettext('%(value)s nonillion', '%(value)s nonillion', number),
)),
(33, lambda number: (
ngettext('%(value).1f decillion', '%(value).1f decillion', number),
ngettext('%(value)s decillion', '%(value)s decillion', number),
)),
(100, lambda number: (
ngettext('%(value).1f googol', '%(value).1f googol', number),
ngettext('%(value)s googol', '%(value)s googol', number),
)),
) )
@ -144,23 +110,14 @@ def intword(value):
if abs_value < 1000000: if abs_value < 1000000:
return value return value
def _check_for_i18n(value, float_formatted, string_formatted): for exponent, converter in intword_converters:
"""
Use the i18n enabled defaultfilters.floatformat if possible
"""
if settings.USE_L10N:
value = defaultfilters.floatformat(value, 1)
template = string_formatted
else:
template = float_formatted
return template % {'value': value}
for exponent, converters in intword_converters:
large_number = 10 ** exponent large_number = 10 ** exponent
if abs_value < large_number * 1000: if abs_value < large_number * 1000:
new_value = value / large_number new_value = value / large_number
rounded_value = round_away_from_one(new_value) rounded_value = round_away_from_one(new_value)
return _check_for_i18n(new_value, *converters(abs(rounded_value))) return converter(abs(rounded_value)) % {
'value': defaultfilters.floatformat(new_value, 1),
}
return value return value

View File

@ -283,6 +283,9 @@ Miscellaneous
* :func:`~django.utils.text.slugify` now removes leading and trailing dashes * :func:`~django.utils.text.slugify` now removes leading and trailing dashes
and underscores. and underscores.
* The :tfilter:`intcomma` and :tfilter:`intword` template filters no longer
depend on the :setting:`USE_L10N` setting.
.. _deprecated-features-3.2: .. _deprecated-features-3.2:
Features deprecated in 3.2 Features deprecated in 3.2