marmelroy / PhoneNumberKit

A Swift framework for parsing, formatting and validating international phone numbers. Inspired by Google's libphonenumber.
MIT License
5.08k stars 808 forks source link

countryCode() does not return an ISO 639 compliant region codes but ISO 3166-1 #260

Closed mime29 closed 2 years ago

mime29 commented 5 years ago

This needs a fix with maybe the addition of a helper to return the country lists as well. https://www.wikiwand.com/fr/ISO_3166-1

I wanted to build a UI showing the country name and the prefix.

I used this function to retrieve all codes and prefixes:

static func allCountries() -> [Country] {
        let phoneKit = PhoneNumberKit()
        return phoneKit.allCountries()
            .compactMap {
                Country(prefix: "\(phoneKit.countryCode(for: $0) ?? 0)", iso639Code: $0)
            }
            .sorted(by: { (country1, country2) -> Bool in
                country1.name < country2.name
            })
    }

but then I wanted to match the code with localized countries name but iso639 codes were not matching any codes (or only a few). Then I implemented the ISO 3166-1 alpha-2 codes to return the country names et voila!

// https://www.wikiwand.com/en/ISO_3166-1
private extension String {
    var displayName: String? {
        switch self {
        case "AD": return L10n.andorra
        case "AE": return L10n.unitedArabEmirates
        case "AF": return L10n.afghanistan
        case "AG": return L10n.antigua
        case "AI": return L10n.anguilla
        case "AL": return L10n.albania
        case "AM": return L10n.armenia
        case "AO": return L10n.angola
        case "AQ": return L10n.antarctica
        case "AR": return L10n.argentina
        case "AS": return L10n.americanSamoa
        case "AT": return L10n.austria
        case "AU": return L10n.australia
        case "AW": return L10n.aruba
        case "AX": return L10n.aland
        case "AZ": return L10n.azerbaijan
        case "BA": return L10n.bosnia
        case "BB": return L10n.barbados
        case "BD": return L10n.bangladesh
        case "BE": return L10n.belgium
        case "BF": return L10n.burkinaFaso
        case "BG": return L10n.bulgaria
        case "BH": return L10n.bahrain
        case "BI": return L10n.burundi
        case "BJ": return L10n.benin
        case "BL": return L10n.saintBarthelemy
        case "BM": return L10n.bermuda
        case "BN": return L10n.brunei
        case "BO": return L10n.bolivia
        case "BQ": return L10n.bonaire
        case "BR": return L10n.brazil
        case "BS": return L10n.bahamas
        case "BT": return L10n.bhutan
        case "BV": return L10n.bouvetIsland
        case "BW": return L10n.botswana
        case "BY": return L10n.belarus
        case "BZ": return L10n.belize
        case "CA": return L10n.canada
        case "CC": return L10n.cocosIslands
        case "CD": return L10n.congo
        case "CF": return L10n.centralAfrican
        case "CG": return L10n.congo
        case "CH": return L10n.switzerland
        case "CI": return L10n.coteIvoire
        case "CK": return L10n.cookIslands
        case "CL": return L10n.chile
        case "CM": return L10n.cameroon
        case "CN": return L10n.china
        case "CO": return L10n.colombia
        case "CR": return L10n.costaRica
        case "CU": return L10n.cuba
        case "CV": return L10n.caboVerde
        case "CW": return L10n.curaçao
        case "CX": return L10n.christmasIsland
        case "CY": return L10n.cyprus
        case "CZ": return L10n.czechia
        case "DE": return L10n.germany
        case "DJ": return L10n.djibouti
        case "DK": return L10n.denmark
        case "DM": return L10n.dominica
        case "DO": return L10n.dominicanRepublic
        case "DZ": return L10n.algeria
        case "EC": return L10n.ecuador
        case "EE": return L10n.estonia
        case "EG": return L10n.egypt
        case "EH": return L10n.westernSahara
        case "ER": return L10n.eritrea
        case "ES": return L10n.spain
        case "ET": return L10n.ethiopia
        case "FI": return L10n.finland
        case "FJ": return L10n.fiji
        case "FK": return L10n.falkland
        case "FM": return L10n.micronesia
        case "FO": return L10n.faroe
        case "FR": return L10n.france
        case "GA": return L10n.gabon
        case "GB": return L10n.unitedKingdom
        case "GD": return L10n.grenada
        case "GE": return L10n.georgia
        case "GF": return L10n.french
        case "GG": return L10n.guernsey
        case "GH": return L10n.ghana
        case "GI": return L10n.gibraltar
        case "GL": return L10n.greenland
        case "GM": return L10n.gambia
        case "GN": return L10n.guinea
        case "GP": return L10n.guadeloupe
        case "GQ": return L10n.equatorialGuinea
        case "GR": return L10n.greece
        case "GS": return L10n.southGeorgia
        case "GT": return L10n.guatemala
        case "GU": return L10n.guam
        case "GW": return L10n.guineaBissau
        case "GY": return L10n.guyana
        case "HK": return L10n.hongKong
        case "HM": return L10n.heardIsland
        case "HN": return L10n.honduras
        case "HR": return L10n.croatia
        case "HT": return L10n.haiti
        case "HU": return L10n.hungary
        case "ID": return L10n.indonesia
        case "IE": return L10n.ireland
        case "IL": return L10n.israel
        case "IM": return L10n.isleOfMan
        case "IN": return L10n.india
        case "IO": return L10n.britishIndianOcean
        case "IQ": return L10n.iraq
        case "IR": return L10n.iran
        case "IS": return L10n.iceland
        case "IT": return L10n.italy
        case "JE": return L10n.jersey
        case "JM": return L10n.jamaica
        case "JO": return L10n.jordan
        case "JP": return L10n.japan
        case "KE": return L10n.kenya
        case "KG": return L10n.kyrgyzstan
        case "KH": return L10n.cambodia
        case "KI": return L10n.kiribati
        case "KM": return L10n.comoros
        case "KN": return L10n.saint
        case "KP": return L10n.koreaDemocratic
        case "KR": return L10n.koreaRepublic
        case "KW": return L10n.kuwait
        case "KY": return L10n.cayman
        case "KZ": return L10n.kazakhstan
        case "LA": return L10n.lao
        case "LB": return L10n.lebanon
        case "LC": return L10n.saintLucia
        case "LI": return L10n.liechtenstein
        case "LK": return L10n.sriLanka
        case "LR": return L10n.liberia
        case "LS": return L10n.lesotho
        case "LT": return L10n.lithuania
        case "LU": return L10n.luxembourg
        case "LV": return L10n.latvia
        case "LY": return L10n.libya
        case "MA": return L10n.morocco
        case "MC": return L10n.monaco
        case "MD": return L10n.moldova
        case "ME": return L10n.montenegro
        case "MF": return L10n.saintMartin
        case "MG": return L10n.madagascar
        case "MH": return L10n.marshall
        case "MK": return L10n.macedonia
        case "ML": return L10n.mali
        case "MM": return L10n.myanmar
        case "MN": return L10n.mongolia
        case "MO": return L10n.macao
        case "MP": return L10n.northernMarinaIslands
        case "MQ": return L10n.martinique
        case "MR": return L10n.mauritania
        case "MS": return L10n.montserrat
        case "MT": return L10n.malta
        case "MU": return L10n.mauritius
        case "MV": return L10n.maldives
        case "MW": return L10n.malawi
        case "MX": return L10n.mexico
        case "MY": return L10n.malaysia
        case "MZ": return L10n.mozambique
        case "NA": return L10n.namibia
        case "NC": return L10n.newCaledonia
        case "NE": return L10n.niger
        case "NF": return L10n.norfolkIsland
        case "NG": return L10n.nigeria
        case "NI": return L10n.nicaragua
        case "NL": return L10n.netherlands
        case "NO": return L10n.norway
        case "NP": return L10n.nepal
        case "NR": return L10n.nauru
        case "NU": return L10n.niue
        case "NZ": return L10n.newZealand
        case "OM": return L10n.oman
        case "PA": return L10n.panama
        case "PE": return L10n.peru
        case "PF": return L10n.french
        case "PG": return L10n.papua
        case "PH": return L10n.philippines
        case "PK": return L10n.pakistan
        case "PL": return L10n.poland
        case "PM": return L10n.saintPierre
        case "PN": return L10n.pitcairn
        case "PR": return L10n.puerto
        case "PS": return L10n.palestine
        case "PT": return L10n.portugal
        case "PW": return L10n.palau
        case "PY": return L10n.paraguay
        case "QA": return L10n.qatar
        case "RE": return L10n.réunion
        case "RO": return L10n.romania
        case "RS": return L10n.serbia
        case "RU": return L10n.russianFederation
        case "RW": return L10n.rwanda
        case "SA": return L10n.saudi
        case "SB": return L10n.solomonIslands
        case "SC": return L10n.seychelles
        case "SD": return L10n.sudan
        case "SE": return L10n.sweden
        case "SG": return L10n.singapore
        case "SH": return L10n.saintHelena
        case "SI": return L10n.slovenia
        case "SJ": return L10n.svalbard
        case "SK": return L10n.slovakia
        case "SL": return L10n.sierra
        case "SM": return L10n.sanMarino
        case "SN": return L10n.senegal
        case "SO": return L10n.somalia
        case "SR": return L10n.suriname
        case "SS": return L10n.southSudan
        case "ST": return L10n.saoTomeAndPrincipe
        case "SV": return L10n.elSalvador
        case "SX": return L10n.sintMaarten
        case "SY": return L10n.syrianArabRepublic
        case "SZ": return L10n.eswatini
        case "TC": return L10n.turksAndCaicosIslands
        case "TD": return L10n.chad
        case "TF": return L10n.frenchSouthernTerritories
        case "TG": return L10n.togo
        case "TH": return L10n.thailand
        case "TJ": return L10n.tajikistan
        case "TK": return L10n.tokelau
        case "TL": return L10n.timorLeste
        case "TM": return L10n.turkmenistan
        case "TN": return L10n.tunisia
        case "TO": return L10n.tonga
        case "TR": return L10n.turkey
        case "TT": return L10n.trinidadAndTobago
        case "TV": return L10n.tuvalu
        case "TW": return L10n.taiwan
        case "TZ": return L10n.tanzania
        case "UA": return L10n.ukraine
        case "UG": return L10n.uganda
        case "UM": return L10n.unitedStatesMinorOutlyingIslands
        case "US": return L10n.unitedStatesOfAmerica
        case "UY": return L10n.uruguay
        case "UZ": return L10n.uzbekistan
        case "VA": return L10n.holy
        case "VC": return L10n.saintVincent
        case "VE": return L10n.venezuela
        case "VG": return L10n.virginIslandsBritish
        case "VI": return L10n.virginIslandsUS
        case "VN": return L10n.vietnam
        case "VU": return L10n.vanuatu
        case "WF": return L10n.wallisAndFutuna
        case "WS": return L10n.samoa
        case "YE": return L10n.yemen
        case "YT": return L10n.mayotte
        case "ZA": return L10n.southAfrica
        case "ZM": return L10n.zambia
        case "ZW": return L10n.zimbabwe
        default: return nil
        }
    }
}
mime29 commented 5 years ago

I should maybe add one thing: The function doc says it's iso 639 codes. And clearly it's not.

blwinters commented 5 years ago

You can get the localized country name for the region code from Locale:

  func localizedCountryName(for regionCode: String) -> String? {
    let regionCode = regionCode.lowercased()
    return Locale.current.localizedString(forRegionCode: regionCode)
  }
mime29 commented 5 years ago

thank you, that's nice. It's not clear in Apple doc if they are using 3166 for regionCode https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPInternational/LanguageandLocaleIDs/LanguageandLocaleIDs.html but I would assume so. They say Region Designators are using it. so that can work. Still need to update the inline documentation for PhoneNumberKit to indicate that the codes are 3166-1 and not 639.

github-actions[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.