I'm using this bit of code (in Groovy, but it should be pretty clear what it does)
class MobileNumberUtilService {
def getISOCountryCode(rawNumber) {
def phoneNumberUtil = PhoneNumberUtil.getInstance()
def number
try {
number = phoneNumberUtil.findNumbers(rawNumber, null).iterator().next().number()
} catch (NoSuchElementException exception) {
return ''
}
CountryCodeToRegionCodeMap.countryCodeToRegionCodeMap[number.countryCode].first()
}
}
Running mobileNumberUtilService.getISOCountryCode('<internationalized American or Canadian number>')
returns [US, AG, AI, AS, BB, BM, BS, CA, DM, DO, GD, GU, JM, KN, KY, LC, MP, MS, PR, SX, TC, TT, VC, VG, VI]
.
Is there any way to distinguish those, short of some sort of web service lookup?