mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2025-12-18 06:45:04 +01:00
Merge branch 'number-formatt-to-js-framework' into 'master'
[ADD] Number formatter to JS framework See merge request allianceauth/allianceauth!1756
This commit is contained in:
@@ -122,3 +122,44 @@ const merged = objectDeepMerge(target, source1, source2);
|
||||
|
||||
console.log(merged); // {a: 5, b: {c: 6, d: 3}, e: 4}
|
||||
```
|
||||
|
||||
### numberFormatter()
|
||||
|
||||
Formats a number according to the specified locale.
|
||||
Usage:
|
||||
|
||||
In your template get the current language code:
|
||||
|
||||
```django
|
||||
{% get_current_language as LANGUAGE_CODE %}
|
||||
```
|
||||
|
||||
Then use it in your JavaScript code:
|
||||
|
||||
```javascript
|
||||
/* global numberFormatter */
|
||||
|
||||
const userLocale = '{{ LANGUAGE_CODE }}'; // e.g., 'en-US', 'de-DE'
|
||||
const number = 1234567.89;
|
||||
|
||||
const formattedNumber = numberFormatter({
|
||||
value: number,
|
||||
locales: userLocale,
|
||||
options: {
|
||||
style: 'currency',
|
||||
currency: 'ISK'
|
||||
}
|
||||
});
|
||||
|
||||
console.log(formattedNumber); // e.g. "ISK 1,234,567.89" or "1.234.567,89 ISK" depending on locale
|
||||
```
|
||||
|
||||
#### numberFormatter() Parameters
|
||||
|
||||
- `value`: The number to format.
|
||||
- `locales`: The locale(s) to use for formatting (e.g., `en-US`, `de-DE`, `['en-US',
|
||||
'de-DE']`). If not provided, the browser's default locale will be used and any
|
||||
language settings from the user will be ignored.
|
||||
- `options`: Additional options for number formatting (see [`Intl.NumberFormat` documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat))
|
||||
- `minimumFractionDigits` is set to 0 by default if not provided.
|
||||
- `maximumFractionDigits` is set to 2 by default if not provided.
|
||||
|
||||
Reference in New Issue
Block a user