mirumee / django-prices

Django fields for the prices module
158 stars 53 forks source link

"amount" template filter ignores LC_NUMERIC environment variable #66

Closed BredoGen closed 4 months ago

BredoGen commented 6 years ago

Hi.

There is a problem using Saleor/django-prices in case with some default locales: e.g:

LANGUAGE_CODE=ru
DEFAULT_CURRENCY=USD

Usage: {{ price|amount:'html' }} Output: 11,49 $

It is not valid output for USD currency (even for RU locale). The valid one is classic $11.49

Reason: "|amount" template filter forces currency format by passing the current language to Babel as locale (see _templatetags/pricesi18n.py:42).

Babel supports overriding numeric locale via LC_NUMERIC env.

It will be very useful if get_locale_data() in prices_i18n.py will be checking this env before passing currency locale to Babel.

patrys commented 6 years ago

It is a valid output for both ru and ru-RU locales. Comma is used as a decimal separator and currency code is properly placed as a suffix.

I've also verified that with the Unicode CLDR database. Javascript behaves the same:

console.log((11.49).toLocaleString("ru", {style: "currency", currency: "USD"}))
// "11,49 $"
BredoGen commented 6 years ago

It is a valid output for both ru and ru-RU locales. Comma is used as a decimal separator and currency code is properly placed as a suffix.

Sorry, yes, both versions are valid for Russian. But $ as prefix is more common for prices in e-commerce.

Introducing optional LC_NUMERIC env will give a chance to use different LANGUAGE and NUMERIC locale for such cases (Babel already uses it in format_currency() method).

patrys commented 6 years ago

Wouldn't using LC_NUMERIC override the format for all languages? There are systems where that variable is exported by the OS which would force the user to explicitly override it which I'm sure would result in bugs being filed against Saleor. What about the decimal point character being different?

BredoGen commented 6 years ago

Wouldn't using LC_NUMERIC override the format for all languages? There are systems where that variable is exported by the OS which would force the user to explicitly override it which I'm sure would result in bugs being filed against Saleor.

You're right. If LC_NUMERIC will be exported by the OS - it'll break multi-language currency localization setup.

What about the decimal point character being different?

Using dot as decimal separator for USD prices in Russian is OK and even more common. Everyone is used to see USD prices in English formatting in e-commerce.

Comma as separator and suffix notation is more common for rubles prices: 10,000 RUB.

So, there is no easy solution to include in core.

It's better for me to override "amount" filter implementing my custom logic there (maybe via dict in settings which will override numeric locale for some languages). Now I realize that this feature is redundant for core functionality.

Sorry for erroneous report

patrys commented 6 years ago

What if there was a way to override certain settings on a per-locale setting? For example to say that you want to use ¤ #,##0.00 for US dollars in Russian.

BredoGen commented 6 years ago

What if there was a way to override certain settings on a per-locale setting? For example to say that you want to use ¤ #,##0.00 for US dollars in Russian.

It will be great feature and it'll completely resolve the problem