Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ashour/49e2f04a5664b18b26f43c413a27ee08 to your computer and use it in GitHub Desktop.
Save ashour/49e2f04a5664b18b26f43c413a27ee08 to your computer and use it in GitHub Desktop.
export default function getBrowserLocale(options = {}) {
const defaultOptions = { countryCodeOnly: false }
const opt = { ...defaultOptions, ...options }
const navigatorLocale =
navigator.languages !== undefined
? navigator.languages[0]
: navigator.language
if (!navigatorLocale) {
return undefined
}
const trimmedLocale = opt.countryCodeOnly
? navigatorLocale.trim().split(/-|_/)[0]
: navigatorLocale.trim()
return trimmedLocale
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment