The user's location as a CountryCode. (defaults to "US")
// If the locale is "en-US"
getUserLocation() // Returns "US" (United States)
// If the locale is "en-GB"
getUserLocation() // Returns "GB" (United Kingdom)
// If the locale is "en-CA"
getUserLocation() // Returns "CA" (Canada)
export function getUserLocation(): CountryCode {
if (typeof chrome === 'undefined' || typeof chrome.i18n === 'undefined') {
return 'US';
}
return chrome.i18n.getUILanguage().split('-')[1];
}
Gets the user's location (two-letter country code) from the browser's i18n API. This just splits the locale by the "-" character and returns the second part. This is a simple way to get the country code from the locale.