Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: L10N and en-IN

Tags:

python

django

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!

like image 483
Vin.AI Avatar asked Sep 11 '25 00:09

Vin.AI


2 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
like image 128
Houda Avatar answered Sep 13 '25 21:09

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!

like image 29
Vin.AI Avatar answered Sep 13 '25 21:09

Vin.AI