Last active
July 26, 2023 10:26
-
-
Save stephan-nordnes-eriksen/f1181db7f453f6cbce8b1fe77c535f64 to your computer and use it in GitHub Desktop.
JavaScript/Node.js Get Country from iOS or Android locale string
This file contains hidden or 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
/** | |
* Get the country code (ISO 3166-1 alpha-2) from a valid locale string from Android or iOS. | |
* Valid locale strings are described here: | |
* ios: https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPInternational/LanguageandLocaleIDs/LanguageandLocaleIDs.html | |
* android: https://developer.android.com/reference/java/util/Locale.html | |
* | |
* Tested with ava. | |
* | |
* @param {*string} localeString The locale string from android or ios | |
*/ | |
function getCountryFromLocaleString(localeString){ | |
var hit = localeString.match("(^|[a-z])_[A-Z]{2}"); | |
if(hit){ | |
return hit[0].split("_")[1]; | |
} | |
} | |
function getLanguageFromLocaleString(localeString) { | |
var hit = localeString.match("^[a-z]{2}(-|_|$)"); | |
if (hit) { | |
return hit[0].substring(0,2); | |
} | |
} | |
//AVA TESTS: | |
import test from 'ava'; | |
test(t => { | |
t.is(getCountryFromLocaleString("en"), undefined); | |
t.is(getCountryFromLocaleString("de_DE"), "DE"); | |
t.is(getCountryFromLocaleString("_GB"), "GB"); | |
t.is(getCountryFromLocaleString("en_US_WIN"), "US"); | |
t.is(getCountryFromLocaleString("de__POSIX"), undefined); | |
t.is(getCountryFromLocaleString("zh_CN_#Hans"), "CN"); | |
t.is(getCountryFromLocaleString("zh_TW_#Hant-x-java"), "TW"); | |
t.is(getCountryFromLocaleString("th_TH_TH_#u-nu-thai"), "TH"); | |
t.is(getCountryFromLocaleString("en"), undefined); | |
t.is(getCountryFromLocaleString("fr"), undefined); | |
t.is(getCountryFromLocaleString("en_GB"), "GB"); | |
t.is(getCountryFromLocaleString("zh_HK"), "HK"); | |
t.is(getCountryFromLocaleString("az-Arab"), undefined); | |
t.is(getCountryFromLocaleString("zh-Hans"), undefined); | |
t.is(getCountryFromLocaleString("zh-Hans_HK"), "HK"); | |
t.is(getCountryFromLocaleString("nb_NO"), "NO"); | |
t.is(getCountryFromLocaleString("en_US"), "US"); | |
t.is(getCountryFromLocaleString("en_GB"), "GB"); | |
}); | |
test(t => { | |
t.is(getLanguageFromLocaleString("en"), "en"); | |
t.is(getLanguageFromLocaleString("de_DE"), "de"); | |
t.is(getLanguageFromLocaleString("_GB"), undefined); | |
t.is(getLanguageFromLocaleString("en_US_WIN"), "en"); | |
t.is(getLanguageFromLocaleString("de__POSIX"), "de"); | |
t.is(getLanguageFromLocaleString("zh_CN_#Hans"), "zh"); | |
t.is(getLanguageFromLocaleString("zh_TW_#Hant-x-java"), "zh"); | |
t.is(getLanguageFromLocaleString("th_TH_TH_#u-nu-thai"), "th"); | |
t.is(getLanguageFromLocaleString("en"), "en"); | |
t.is(getLanguageFromLocaleString("fr"), "fr"); | |
t.is(getLanguageFromLocaleString("en_GB"), "en"); | |
t.is(getLanguageFromLocaleString("zh_HK"), "zh"); | |
t.is(getLanguageFromLocaleString("az-Arab"), "az"); | |
t.is(getLanguageFromLocaleString("zh-Hans"), "zh"); | |
t.is(getLanguageFromLocaleString("zh-Hans_HK"), "zh"); | |
t.is(getLanguageFromLocaleString("nb_NO"), "nb"); | |
t.is(getLanguageFromLocaleString("en_US"), "en"); | |
t.is(getLanguageFromLocaleString("en_GB"), "en"); | |
}); | |
// For the semi-golfers | |
// function golfatron(l){ | |
// return (hit = l.match("(^|[a-z])_[A-Z]{2}")) ? hit[0].split("_")[1] : undefined; | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works for all the test examples on the respective websites, but in the case some locales does not work, let me know!