Created
January 10, 2020 15:18
-
-
Save ashour/49e2f04a5664b18b26f43c413a27ee08 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
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