Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Convert ISO Alpha 2 to Alpha 3 country code

Is it possible to convert ISO 3166 alpha-2 country code to alpha 3 country code in iOS, for instance DE to DEU?

like image 516
Koteg Avatar asked Jul 20 '12 09:07

Koteg


People also ask

Should I use alpha-2 or Alpha 3 country codes?

The country codes can be represented either as a two-letter code (alpha-2) which is recommended as the general-purpose code, a three-letter code (alpha-3) which is more closely related to the country name and a three-digit numeric code (numeric-3) which can be useful if you need to avoid using Latin script.

How can I get country code from country?

If you want to specify language for results, you can use Locale("your language") as the parameter of getDisplayCountry() . "ZH" is the language code of Chinese. You will get "荷兰", which is the Chinese name of Netherlands. And you can use Locale("languages", "ISO-3166 code") to specify your language variant.

Can ISO country code?

Country Code CAN Country code according to ISO-3166 Alpha-3 CAN is the three-letter country abbreviation for Canada.

Is there a 2 letter country code?

Two-letter country codes are used to represent countries and states (often both widely recognized and not) as a code of two letters. ISO 3166-1 alpha-2 is the main set of two-letter country codes that is currently used.


1 Answers

Base on the answer Franck here is the code swift4 for loading the plist and then to convert 3 letters Country ISO code

The plist: Full conversion ISO 3166-1-Alpha2 to Alpha3

//
//  CountryUtility.swift
//

import Foundation

struct CountryUtility {


    static private func loadCountryListISO() -> Dictionary<String, String>? {

        let pListFileURL = Bundle.main.url(forResource: "iso3166_2_to_iso3166_3", withExtension: "plist", subdirectory: "")
        if let pListPath = pListFileURL?.path,
            let pListData = FileManager.default.contents(atPath: pListPath) {
            do {
                let pListObject = try PropertyListSerialization.propertyList(from: pListData, options:PropertyListSerialization.ReadOptions(), format:nil)

                guard let pListDict = pListObject as? Dictionary<String, String> else {
                    return nil
                }

                return pListDict
            } catch {
                print("Error reading regions plist file: \(error)")
                return nil
            }
        }
        return nil
    }


    /// Convertion ISO 3166-1-Alpha2 to Alpha3
    /// Country code of 2 letters to 3 letters code
    /// E.g: PT to PRT
    static func getCountryCodeAlpha3(countryCodeAlpha2: String) -> String? {

        guard let countryList = CountryUtility.loadCountryListISO() else {
            return nil
        }

        if let countryCodeAlpha3 = countryList[countryCodeAlpha2]{
            return countryCodeAlpha3
        }
        return nil
    }


    static func getLocalCountryCode() -> String?{

        guard let countryCode = NSLocale.current.regionCode else { return nil }
        return countryCode
    }


    /// This function will get full country name based on the phone Locale
    /// E.g. Portugal
    static func getLocalCountry() -> String?{

        let countryLocale = NSLocale.current
        guard let countryCode = countryLocale.regionCode else { return nil }
        let country = (countryLocale as NSLocale).displayName(forKey: NSLocale.Key.countryCode, value: countryCode)
        return country
    }

}

To use you just need to:

if let countryCode = CountryUtility.getLocalCountryCode() {

            if let alpha3 = CountryUtility.getCountryCodeAlpha3(countryCodeAlpha2: countryCode){
                print(alpha3) ///result: PRT
            }
        }
like image 86
Tiago Mendes Avatar answered Sep 20 '22 08:09

Tiago Mendes