feat(ssr): format numbers using browser locale
All checks were successful
deploy / deploy (push) Successful in 58s
All checks were successful
deploy / deploy (push) Successful in 58s
This commit is contained in:
parent
e16727713c
commit
cfaca9fb40
@ -1,3 +1,5 @@
|
||||
import { getBrowserLocales } from "./utils";
|
||||
|
||||
/**
|
||||
* Checks if the given value is an number.
|
||||
*
|
||||
@ -24,7 +26,7 @@ export function formatNumber(number: any, decimals?: number) {
|
||||
if (typeof number !== "number") {
|
||||
return number;
|
||||
}
|
||||
return number.toLocaleString(undefined, {
|
||||
return number.toLocaleString(getBrowserLocales(), {
|
||||
minimumFractionDigits: decimals,
|
||||
maximumFractionDigits: decimals,
|
||||
});
|
||||
|
@ -49,3 +49,30 @@ export function getPageFromSearchQuery(
|
||||
|
||||
return page;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the users locales from the browser
|
||||
*
|
||||
* @param options the options to use
|
||||
* @returns the browser locales
|
||||
*/
|
||||
export function getBrowserLocales(options = {}) {
|
||||
const defaultOptions = {
|
||||
languageCodeOnly: false,
|
||||
};
|
||||
const opt = {
|
||||
...defaultOptions,
|
||||
...options,
|
||||
};
|
||||
const browserLocales =
|
||||
navigator.languages === undefined
|
||||
? [navigator.language]
|
||||
: navigator.languages;
|
||||
if (!browserLocales) {
|
||||
return undefined;
|
||||
}
|
||||
return browserLocales.map((locale) => {
|
||||
const trimmedLocale = locale.trim();
|
||||
return opt.languageCodeOnly ? trimmedLocale.split(/-|_/)[0] : trimmedLocale;
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user