-
Notifications
You must be signed in to change notification settings - Fork 73
Confusion between currency and locale formats #37
Comments
The locale specific formatting is a bit tricky. @edorivai created a solution for us to be able to format currencies based on user's locale but you won't be able to format Some examples:
Our locale formatting data is very limited at the moment. You can add the missing locales here localeFormats.json and send a PR. Another solution is to use the |
Accommodates smirzaei#37 for EUR locales.
Accommodates smirzaei#37 for EUR locales.
Accommodates smirzaei#37 for EUR locales.
I created the locale based formatting, only for the use case of formatting the currency that corresponds with that specific locale. Frankly, I didn't even think about formatting a different locale. However, the current setup still allows you to use a locale based format for a different currency! @smirzaei is right, the locale formatting data is very limited, I only added the nuances in rendering Euro values, and am hoping for others to help us out here by adding more locale formats. To give you a bit more info into how it works; the locale formats in localeFormats.json are used to overwrite the default formatting for a currency. If you don't specify a specific currency code, but you do specify the locale, it will try to find the appropriate currency for that locale. For example {
"code": "EUR",
"symbol": "€",
"thousandsSeparator": " ",
"decimalSeparator": ",",
"symbolOnLeft": false,
"spaceBetweenAmountAndSymbol": true,
"decimalDigits": 2
}, And the locale formatting for {
"symbolOnLeft": true,
"spaceBetweenAmountAndSymbol": false,
"thousandsSeparator": "."
}, The formatting data is applied like so: assign({}, defaultCurrency, findCurrency(code), localeFormat) Which means that the fields in the locale format for {
"code": "EUR",
"symbol": "€",
"thousandsSeparator": ".", // overwritten
"decimalSeparator": ",",
"symbolOnLeft": true, // overwritten
"spaceBetweenAmountAndSymbol": false, // overwritten
"decimalDigits": 2
}, This mutates an original format of So this system should work fairly well for your use case. All you need to do is add the formatting rules for the locales you need, and you should be good to go. One last thing; this issue actually brought up a problem with the current version of the locale formats. I've already submitted a PR in #38, so you might want to wait until that is merged ;). |
Oh, and if it wasn't clear already, after adding your locale formatting, just use it like so: currencyFormatter.format(1234.56, { code: 'USD', locale: 'fr-CA' });
// => '1 234.56 $'
currencyFormatter.format(1234.56, { code: 'USD', locale: 'en-CA' });
// => '$1 234.56' Output will depend on your implementation of the locale format of course :) |
Thank you for considering this issue. |
@Gwened thanks for the link to currencyFormatter.js. That's a really cool lib, especially their base of currencies and locales. I'm gonna see if I can reuse their config base in this lib. It seems that they don't support code splitting (in other words; only including the currencies and locales that you want to support), which is something we're looking into for v2. |
It seems this lib only allows one format per currency.
However, the format, in particular symbolOnLeft, usually depends on the locale.
For example, I think Canadians speaking French will use 5$ while English speakers write $5.
In a similar way, if I'm targeting brits traveling in Europe, I would write €5 instead of 5 €.
Is there something I'm missing?
The text was updated successfully, but these errors were encountered: