tickle/modules/utils/getLocale.mjs

17 lines
514 B
JavaScript
Raw Normal View History

2023-06-07 20:07:47 +00:00
//@ts-check
/**
* Retrieves the current locale. Only works if `navigator` is available.
* Otherwise, returns the `defaultLang` passed property
* @param {string} [defaultLang] defaults to an empty string
* @returns The browser locale, formatted as `xx_XX`
*/
export const getLocale = (defaultLang = "") =>
(typeof navigator !== "undefined" &&
(navigator.languages ? navigator.languages[0] : navigator.language)
.split(".")[0]
.replace("-", "_")) ||
defaultLang;
export default getLocale;