Last active
April 10, 2021 21:36
-
-
Save ashour/5f169a6dd9b6293691629ee0d06cae6f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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