Django: L10N and en-IN

Question:

I googled a lot to find solution for my problem related to Django’s L10N settings for en-IN and found nothing satisfying which works. So, finally get back here to friends.

I’m struggling to format currency as Indian number formatting standard. Which follows NUMBER_GROUPING = (3, 2, 0) and LANGUAGE_CODE = 'en-IN'.

My current configurations of settings.py file is:

LANGUAGE_CODE = 'en-IN'
USE_I18N = False
USE_L10N = True
USE_THOUSAND_SEPARATOR = True
NUMBER_GROUPING = (3, 2, 0)

Also tried setting:

USE_I18N = True

In template file I use:

{% load humanize %}
{# where object.price value is 524300 #}
<p>
{{ object.price }}
<!-- and -->
{{ object.price|intcomma }}
</p>

However this outputs:

524,300 instead 5,24,300

enter image description here

What am I doing wrong, which is stopping Django for follow settings of LANGUAGE_CODE, USE_L10N and NUMBER_GROUPING

It works for hi-IN

If I changes LANGUAGE_CODE = 'hi-IN', I get expected output of formatted currency as 5,24,300. But the problem is our site is not in Hindi language but in Indian English with Hindi localization.
What a mess 🙁

Django Docs even showing example for language en_IN. Which clearly telling it will format numbers as I expected. But it is not working for me.

Setting LANGUAGE_CODE = 'en_IN' throws error. Do not accept underscore but hyphen.

My current system locale settings are as below:

LANG=en_US.UTF-8
LANGUAGE=en_US:en
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC=en_IN.UTF-8
LC_TIME=en_IN.UTF-8
LC_COLLATE="en_US.UTF-8"
LC_MONETARY=en_IN.UTF-8
LC_MESSAGES="en_US.UTF-8"
LC_PAPER=en_IN.UTF-8
LC_NAME=en_IN.UTF-8
LC_ADDRESS=en_IN.UTF-8
LC_TELEPHONE=en_IN.UTF-8
LC_MEASUREMENT=en_IN.UTF-8
LC_IDENTIFICATION=en_IN.UTF-8
LC_ALL=

Any idea or suggestion will really be appreciated!

Asked By: Vin.AI

||

Answers:

When “formats” are controlled by templates, the result depends not only on Django settings but also on the system and the browser settings

The best thing to do is to send it as a pre-formated string to the html template :

pip install Babel

from babel.numbers import format_currency
format_currency(524300, 'INR', locale='en_IN')

Result what ever are Django and system and Browser settings :

₹5,24,300.00
Answered By: Houda

I solved this problem using Django’s custom format files for L10N.

I dig deep into docs and found this, solved my problem.

So, my new revision is here which solved my query even without changing LANGUAGE_CODE.

My current mysite/settings.py is:

USE_I18N = False
USE_L10N = True

FORMAT_MODULE_PATH = [
    'mysite.formats'
]

And tree structure of formats is:

./mysite/formats/
|- __init__.py
|- en/
   |- __init__.py
   |- formats.py
# formats.py
USE_THOUSAND_SEPARATOR = True
NUMBER_GROUPING = (3, 2, 0)

And it works!

Answered By: Vin.AI

In your settings.py,

USE_L10N = True # or False

FORMAT_MODULE_PATH = [
    'my_project.formats'
]

USE_THOUSAND_SEPARATOR = True #this line must be present in settings

Create formats.py as in,

› my_project/
  › formats/
    › en/
       __init__.py
       formats.py
    __init__.py

In formats.py,

NUMBER_GROUPING = (3, 2, 0)

Ref, https://docs.djangoproject.com/en/4.1/topics/i18n/formatting/

Answered By: Raj Yadav

LANGUAGE_CODE = ‘hi’

USE_I18N = False

NUMBER_GROUPING = (3, 2, 0)

It’s Work


Notes:
I am also using language code en-IN , en-in, en_IN , according to documentaion
https://docs.djangoproject.com/en/4.1/topics/i18n/formatting/

https://docs.djangoproject.com/en/4.1/ref/settings/#std:setting-NUMBER_GROUPING

still is not working

Answered By: Himanshu Gupta
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.