从国家名称获取ISO国家/地区代码

时间:2018-03-15 23:11:17

标签: c#

有没有办法从国家/地区名称获取ISO ALPHA-2代码(国家/地区代码),例如英国= GB?

我正在努力实现与下面代码几乎相反的代码

//To get the Country Names from the CultureInfo

 foreach (CultureInfo cul in CultureInfo.GetCultures(CultureTypes.SpecificCultures))

 {    
       country = new RegionInfo(new CultureInfo(cul.Name, false).LCID);    
       countryNames.Add(country.DisplayName.ToString());    
 }

4 个答案:

答案 0 :(得分:3)

您可以这样做:

var regions = CultureInfo.GetCultures(CultureTypes.SpecificCultures).Select(x => new RegionInfo(x.LCID));
var englishRegion = regions.FirstOrDefault(region => region.EnglishName.Contains("United Kingdom"));
var countryAbbrev = englishRegion.TwoLetterISORegionName;

答案 1 :(得分:2)

本课程用于收集有关国家和相关文化的信息。

它的构造函数允许您选择是否要加载所有可能的国家/地区或仅加载特定文化:

CountryList Countries = new CountryList([true][false]);

其中true表示CultureTypes.AllCulturesfalse表示CultureTypes.SpecificCultures

例如,这些参数:

CountryList Countries = new CountryList(true);
List<CountryList.CountryInfo> CountryInfo = Countries.GetCountryInfoByName("United States", false);

true中的false / GetCountryInfoByName()表示使用/不使用本机名称)
此方法返回三个结果:

1国家 - 美国
3文化 - 英语,英语(美国),西班牙语(美国)

使用本机名称:

List<CountryList.CountryInfo> CountryInfo = Countries.GetCountryInfoByName("United States", true);

1国家 - 美国
2文化 - 英语,英语(美国)

使用特定文化和本地名称:

CountryList Countries = new CountryList(false);
List<CountryList.CountryInfo> CountryInfo = Countries.GetCountryInfoByName("United States", true);

1国家 - 美国
1文化 - 英语(美国)

与您的问题更相关,本课程公开了这些方法:

string TwoLettersName = Countries.GetTwoLettersName("United States", true);

返回US

string ThreeLettersName = Countries.GetThreeLettersName("United States", true);

返回USA

List<string> IetfTags = Countries.GetIetfLanguageTag("United States", true);

返回en-US

List<int> GeoIds = Countries.GetRegionGeoId("United States", true);

返回244

public class CountryList
{
    private CultureTypes _AllCultures;
    public CountryList(bool AllCultures)
    {
        this._AllCultures = (AllCultures) ? CultureTypes.AllCultures : CultureTypes.SpecificCultures;
        this.Countries = GetAllCountries(this._AllCultures);
    }

    public List<CountryInfo> Countries { get; set; }

    public List<CountryInfo> GetCountryInfoByName(string CountryName, bool NativeName)
    {
        return (NativeName) ? this.Countries.Where(info => info.Region.NativeName == CountryName).ToList()
                            : this.Countries.Where(info => info.Region.EnglishName == CountryName).ToList();
    }

    public List<CountryInfo> GetCountryInfoByName(string CountryName, bool NativeName, bool IsNeutral)
    {
        return (NativeName) ? this.Countries.Where(info => info.Region.NativeName == CountryName && 
                                                           info.Culture.IsNeutralCulture == IsNeutral).ToList()
                            : this.Countries.Where(info => info.Region.EnglishName == CountryName && 
                                                           info.Culture.IsNeutralCulture == IsNeutral).ToList();
    }

    public string GetTwoLettersName(string CountryName, bool NativeName)
    {
        CountryInfo country = (NativeName) ? this.Countries.Where(info => info.Region.NativeName == CountryName)
                                                           .FirstOrDefault()
                                           : this.Countries.Where(info => info.Region.EnglishName == CountryName)
                                                           .FirstOrDefault();

        return (country != null) ? country.Region.TwoLetterISORegionName : string.Empty;
    }

    public string GetThreeLettersName(string CountryName, bool NativeName)
    {
        CountryInfo country = (NativeName) ? this.Countries.Where(info => info.Region.NativeName.Contains(CountryName))
                                                            .FirstOrDefault()
                                           : this.Countries.Where(info => info.Region.EnglishName.Contains(CountryName))
                                                            .FirstOrDefault();

        return (country != null) ? country.Region.ThreeLetterISORegionName : string.Empty;
    }

    public List<string> GetIetfLanguageTag(string CountryName, bool NativeName)
    {
        return (NativeName) ? this.Countries.Where(info => info.Region.NativeName == CountryName)
                                            .Select(info => info.Culture.IetfLanguageTag).ToList()
                            : this.Countries.Where(info => info.Region.EnglishName == CountryName)
                                            .Select(info => info.Culture.IetfLanguageTag).ToList();
    }

    public List<int> GetRegionGeoId(string CountryName, bool NativeName)
    {
        return (NativeName) ? this.Countries.Where(info => info.Region.NativeName == CountryName)
                                            .Select(info => info.Region.GeoId).ToList()
                            : this.Countries.Where(info => info.Region.EnglishName == CountryName)
                                            .Select(info => info.Region.GeoId).ToList();
    }

    private static List<CountryInfo> GetAllCountries(CultureTypes cultureTypes)
    {
        List<CountryInfo> Countries = new List<CountryInfo>();

        foreach (CultureInfo culture in CultureInfo.GetCultures(cultureTypes))
        {
            if (culture.LCID != 127)
                Countries.Add(new CountryInfo()  { 
                    Culture = culture, 
                    Region = new RegionInfo(culture.TextInfo.CultureName) 
                });
        }
        return Countries;
    }

    public class CountryInfo
    {
        public CultureInfo Culture { get; set; }
        public RegionInfo Region { get; set; }
    }
}

答案 2 :(得分:0)

在上面你只从“国家/地区”剥离了DisplayName ...... 您应该能够从该国家/地区的价值中获取代码,因为您循环遍历每种文化并从文化名称获取国家/地区。

为什么不编写一个字典(代码,DisplayName)而不是名字列表?

您可以从值(显示名称)轻松地从键(代码)或键(代码)中查找值(显示名称),并且您只能维护1个集合。

答案 3 :(得分:0)

您可以使用我的软件包Nager.Country。我具有使此过程变得容易的功能。也可以使用西班牙语名称e.x搜索国家。 “ Reino Unido”

PM> install-package Nager.Country
ICountryProvider countryProvider = new CountryProvider();
var countryInfo = countryProvider.GetCountryByName("United Kingdom");

//countryInfo.Alpha2Code = GB