Django USE_L10N not working

I have already set USE_L10N = True in settings.py

But in the following form:

from django.contrib.humanize.templatetags.humanize import intcomma

dev view_name(request):
     output = intcomma(123456)

      

The output is always "123,456" for all locales.

+2


a source to share


4 answers


Intcomma only respects localization settings in Django 1.4 and higher.

Delete intcomma

and enable at the same time USE_THOUSAND_SEPARATOR

.



Note that this allows thousands of separators for all integers.

+2


a source


I think intcomma () does the same for all locales:

def intcomma(value):
    """
    Converts an integer to a string containing commas every three digits.
    For example, 3000 becomes '3,000' and 45000 becomes '45,000'.
    """
    orig = force_unicode(value)
    new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', orig)
    if orig == new:
        return new
    else:
        return intcomma(new)

intcomma.is_safe = True
register.filter(intcomma)

      



You can change this function and pass the delimiter as an argument.

+1


a source


If you are typing this in a template you can set in settings.py:

USE_THOUSAND_SEPARATOR=True
THOUSAND_SEPARATOR='.'
NUMBER_GROUPING=3

      

after making 3 changes above, DECIMAL_SEPARATOR will automatically become ','. But you can also install it:

DECIMAL_SEPARATOR=','

      

this way you don't need to humanize, but I think it will affect all your apps in this settings.py file.

+1


a source


import locale
locale.format("%d", 123456, True)

      

-1


a source







All Articles