feat(ssr): format numbers using browser locale
All checks were successful
deploy / deploy (push) Successful in 58s

This commit is contained in:
Lee 2023-11-06 18:43:21 +00:00
parent e16727713c
commit cfaca9fb40
2 changed files with 30 additions and 1 deletions

@ -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;
});
}