Skip to content

Instantly share code, notes, and snippets.

@ashour
Last active April 10, 2021 21:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashour/5f169a6dd9b6293691629ee0d06cae6f to your computer and use it in GitHub Desktop.
Save ashour/5f169a6dd9b6293691629ee0d06cae6f to your computer and use it in GitHub Desktop.
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;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment