。NET中转换国家代码

问题描述 投票:39回答:10

在.NET中,有任何方法可以将三个字母国家/地区代码(在ISO 3166-1 alpha-3中定义)转换为两个字母语言代码(在ISO 3166-1 alpha-2中定义)。转换BEL为BE?

已经查看了System.Globalization中的RegionInfo类,但构造函数似乎不支持这三个字母代码。

c# .net locale globalization iso
10个回答
36
投票

RegionInfo类确实知道三个字母的代码(在ThreeLetterISORegionName属性中),但我认为没有办法根据此代码获取ThreeLetterISORegionName,您需要枚举所有区域并将它们添加到您自己的字典中,并以三个字母的代码为键。

但是,我认为.NET Framework使用RegionInfo文化配合使用,而不是与ISO 3166-1含义的国家/地区配合使用。因此,许多国家/地区都无法使用ISO 3166-1标准(例如尝试RegionInfo)。我想您应该创建自己的国家/地区代码簿。

编辑:从当前国家/地区代码簿中的246个国家/地区中,SX可用于其中的125个国家,其余国家(121)不支持。结论:这不是获取国家/地区代码簿的好方法。


0
投票

请参阅Nuget软件包“ Bia.Countries”using System; using System.Globalization; public class SamplesRegionInfo { public static void Main() { // Displays the property values of the RegionInfo for "US". RegionInfo myRI1 = new RegionInfo( "US" ); Console.WriteLine( " Name: {0}", myRI1.Name ); Console.WriteLine( " DisplayName: {0}", myRI1.DisplayName ); Console.WriteLine( " EnglishName: {0}", myRI1.EnglishName ); Console.WriteLine( " IsMetric: {0}", myRI1.IsMetric ); Console.WriteLine( " ThreeLetterISORegionName: {0}", myRI1.ThreeLetterISORegionName ); Console.WriteLine( " ThreeLetterWindowsRegionName: {0}", myRI1.ThreeLetterWindowsRegionName ); Console.WriteLine( " TwoLetterISORegionName: {0}", myRI1.TwoLetterISORegionName ); Console.WriteLine( " CurrencySymbol: {0}", myRI1.CurrencySymbol ); Console.WriteLine( " ISOCurrencySymbol: {0}", myRI1.ISOCurrencySymbol ); } } /* This code produces the following output. Name: US DisplayName: United States EnglishName: United States IsMetric: False ThreeLetterISORegionName: USA ThreeLetterWindowsRegionName: USA TwoLetterISORegionName: US CurrencySymbol: $ ISOCurrencySymbol: USD */ 在[]方面做得很好

Bia.Countries.Iso3166.Countries.GetCountryByAlpha2()


37
投票

似乎没有内置的方法可以做到这一点。您可以使用RegionInfo中的方法。但是,如果您需要转换许多国家/地区代码,则可以避免文化枚举,并预先计算用于国家/地区代码转换的映射字典。

这就是这种生成器的外观:

GenericTypeTea's answer

运行此生成器以获取国家代码映射字典,如下所示:

namespace ISOCountryCodes
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;

    class Program
    {
        public static void Main(string[] args)
        {
            var countryCodesMapping = new Dictionary<string, RegionInfo>();
            CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);

            foreach (var culture in cultures)
            {
                try
                {
                    var region = new RegionInfo(culture.LCID);
                    countryCodesMapping[region.ThreeLetterISORegionName] = region;
                }
                catch (CultureNotFoundException)
                {
                    var consoleColor = Console.ForegroundColor;

                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Culture not found: " + culture.Name);
                    Console.ForegroundColor = consoleColor;
                }
            }

            Console.WriteLine("var countryCodesMapping = new Dictionary<string, string>() {");
            foreach (var mapping in countryCodesMapping.OrderBy(mapping => mapping.Key))
            {
                Console.WriteLine("   {{ \"{0}\", \"{1}\" }},    // {2}", mapping.Key, mapping.Value.TwoLetterISORegionName, mapping.Value.EnglishName);
            }

            Console.WriteLine("};");
        }
    }
}

20
投票

UPDATED:

之前没有正确阅读问题。现在,以下应该已从ISO 3166-1 alpha-3正确转换为ISO 3166-1 alpha-2:


没有内置的方法。您需要遍历var countryCodesMapping = new Dictionary<string, string>() { { "AFG", "AF" }, // Afghanistan { "ALB", "AL" }, // Albania { "ARE", "AE" }, // U.A.E. { "ARG", "AR" }, // Argentina { "ARM", "AM" }, // Armenia { "AUS", "AU" }, // Australia { "AUT", "AT" }, // Austria { "AZE", "AZ" }, // Azerbaijan { "BEL", "BE" }, // Belgium { "BGD", "BD" }, // Bangladesh { "BGR", "BG" }, // Bulgaria { "BHR", "BH" }, // Bahrain { "BIH", "BA" }, // Bosnia and Herzegovina { "BLR", "BY" }, // Belarus { "BLZ", "BZ" }, // Belize { "BOL", "BO" }, // Bolivia { "BRA", "BR" }, // Brazil { "BRN", "BN" }, // Brunei Darussalam { "CAN", "CA" }, // Canada { "CHE", "CH" }, // Switzerland { "CHL", "CL" }, // Chile { "CHN", "CN" }, // People's Republic of China { "COL", "CO" }, // Colombia { "CRI", "CR" }, // Costa Rica { "CZE", "CZ" }, // Czech Republic { "DEU", "DE" }, // Germany { "DNK", "DK" }, // Denmark { "DOM", "DO" }, // Dominican Republic { "DZA", "DZ" }, // Algeria { "ECU", "EC" }, // Ecuador { "EGY", "EG" }, // Egypt { "ESP", "ES" }, // Spain { "EST", "EE" }, // Estonia { "ETH", "ET" }, // Ethiopia { "FIN", "FI" }, // Finland { "FRA", "FR" }, // France { "FRO", "FO" }, // Faroe Islands { "GBR", "GB" }, // United Kingdom { "GEO", "GE" }, // Georgia { "GRC", "GR" }, // Greece { "GRL", "GL" }, // Greenland { "GTM", "GT" }, // Guatemala { "HKG", "HK" }, // Hong Kong S.A.R. { "HND", "HN" }, // Honduras { "HRV", "HR" }, // Croatia { "HUN", "HU" }, // Hungary { "IDN", "ID" }, // Indonesia { "IND", "IN" }, // India { "IRL", "IE" }, // Ireland { "IRN", "IR" }, // Iran { "IRQ", "IQ" }, // Iraq { "ISL", "IS" }, // Iceland { "ISR", "IL" }, // Israel { "ITA", "IT" }, // Italy { "JAM", "JM" }, // Jamaica { "JOR", "JO" }, // Jordan { "JPN", "JP" }, // Japan { "KAZ", "KZ" }, // Kazakhstan { "KEN", "KE" }, // Kenya { "KGZ", "KG" }, // Kyrgyzstan { "KHM", "KH" }, // Cambodia { "KOR", "KR" }, // Korea { "KWT", "KW" }, // Kuwait { "LAO", "LA" }, // Lao P.D.R. { "LBN", "LB" }, // Lebanon { "LBY", "LY" }, // Libya { "LIE", "LI" }, // Liechtenstein { "LKA", "LK" }, // Sri Lanka { "LTU", "LT" }, // Lithuania { "LUX", "LU" }, // Luxembourg { "LVA", "LV" }, // Latvia { "MAC", "MO" }, // Macao S.A.R. { "MAR", "MA" }, // Morocco { "MCO", "MC" }, // Principality of Monaco { "MDV", "MV" }, // Maldives { "MEX", "MX" }, // Mexico { "MKD", "MK" }, // Macedonia (FYROM) { "MLT", "MT" }, // Malta { "MNE", "ME" }, // Montenegro { "MNG", "MN" }, // Mongolia { "MYS", "MY" }, // Malaysia { "NGA", "NG" }, // Nigeria { "NIC", "NI" }, // Nicaragua { "NLD", "NL" }, // Netherlands { "NOR", "NO" }, // Norway { "NPL", "NP" }, // Nepal { "NZL", "NZ" }, // New Zealand { "OMN", "OM" }, // Oman { "PAK", "PK" }, // Islamic Republic of Pakistan { "PAN", "PA" }, // Panama { "PER", "PE" }, // Peru { "PHL", "PH" }, // Republic of the Philippines { "POL", "PL" }, // Poland { "PRI", "PR" }, // Puerto Rico { "PRT", "PT" }, // Portugal { "PRY", "PY" }, // Paraguay { "QAT", "QA" }, // Qatar { "ROU", "RO" }, // Romania { "RUS", "RU" }, // Russia { "RWA", "RW" }, // Rwanda { "SAU", "SA" }, // Saudi Arabia { "SCG", "CS" }, // Serbia and Montenegro (Former) { "SEN", "SN" }, // Senegal { "SGP", "SG" }, // Singapore { "SLV", "SV" }, // El Salvador { "SRB", "RS" }, // Serbia { "SVK", "SK" }, // Slovakia { "SVN", "SI" }, // Slovenia { "SWE", "SE" }, // Sweden { "SYR", "SY" }, // Syria { "TAJ", "TJ" }, // Tajikistan { "THA", "TH" }, // Thailand { "TKM", "TM" }, // Turkmenistan { "TTO", "TT" }, // Trinidad and Tobago { "TUN", "TN" }, // Tunisia { "TUR", "TR" }, // Turkey { "TWN", "TW" }, // Taiwan { "UKR", "UA" }, // Ukraine { "URY", "UY" }, // Uruguay { "USA", "US" }, // United States { "UZB", "UZ" }, // Uzbekistan { "VEN", "VE" }, // Bolivarian Republic of Venezuela { "VNM", "VN" }, // Vietnam { "YEM", "YE" }, // Yemen { "ZAF", "ZA" }, // South Africa { "ZWE", "ZW" }, // Zimbabwe }; 以获得CultureInfo才能手动找到匹配项(这效率很低,因此建议进行一些缓存):

RegionInfo

13
投票

我只是把这个又快又脏的类串在一起,以查询和管理ISO3166-1条目。那里只有一个示例方法可以通过alpha 3代码进行查询,但是应该足够简单,可以根据需要添加其他转换。

示例用法:

public string ConvertThreeLetterNameToTwoLetterName(string name)
{
    if (name.Length != 3)
    {
        throw new ArgumentException("name must be three letters.");
    }

    name = name.ToUpper();

    CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
    foreach (CultureInfo culture in cultures)
    {
        RegionInfo region = new RegionInfo(culture.LCID);
        if (region.ThreeLetterISORegionName.ToUpper() == name)
        {
            return region.TwoLetterISORegionName;
        }
    }

    return null;
}

class:

string countryName = ISO3166.FromAlpha3("LUX").Name;

4
投票

上面的某些答案是O(N)进行查找,如果您只进行一次,这很好,但是通常您希望每个过程调用一次以上,在这种情况下,您可能会想要一些东西更高效。

public static class ISO3166
{
    /// <summary>
    /// Obtain ISO3166-1 Country based on its alpha3 code.
    /// </summary>
    /// <param name="alpha3"></param>
    /// <returns></returns>
    public static ISO3166Country FromAlpha3(string alpha3)
    {
        Collection<ISO3166Country> collection = BuildCollection();
        return collection.FirstOrDefault(p => p.Alpha3 == alpha3);
    }

    #region Build Collection
    private static Collection<ISO3166Country> BuildCollection()
    {
        Collection<ISO3166Country> collection = new Collection<ISO3166Country>();

        // This collection built from Wikipedia entry on ISO3166-1 on 9th Feb 2016

        collection.Add(new ISO3166Country("Afghanistan", "AF", "AFG", 4));
        collection.Add(new ISO3166Country("Åland Islands", "AX", "ALA", 248));
        collection.Add(new ISO3166Country("Albania", "AL", "ALB", 8));
        collection.Add(new ISO3166Country("Algeria", "DZ", "DZA", 12));
        collection.Add(new ISO3166Country("American Samoa", "AS", "ASM", 16));
        collection.Add(new ISO3166Country("Andorra", "AD", "AND", 20));
        collection.Add(new ISO3166Country("Angola", "AO", "AGO", 24));
        collection.Add(new ISO3166Country("Anguilla", "AI", "AIA", 660));
        collection.Add(new ISO3166Country("Antarctica", "AQ", "ATA", 10));
        collection.Add(new ISO3166Country("Antigua and Barbuda", "AG", "ATG", 28));
        collection.Add(new ISO3166Country("Argentina", "AR", "ARG", 32));
        collection.Add(new ISO3166Country("Armenia", "AM", "ARM", 51));
        collection.Add(new ISO3166Country("Aruba", "AW", "ABW", 533));
        collection.Add(new ISO3166Country("Australia", "AU", "AUS", 36));
        collection.Add(new ISO3166Country("Austria", "AT", "AUT", 40));
        collection.Add(new ISO3166Country("Azerbaijan", "AZ", "AZE", 31));
        collection.Add(new ISO3166Country("Bahamas", "BS", "BHS", 44));
        collection.Add(new ISO3166Country("Bahrain", "BH", "BHR", 48));
        collection.Add(new ISO3166Country("Bangladesh", "BD", "BGD", 50));
        collection.Add(new ISO3166Country("Barbados", "BB", "BRB", 52));
        collection.Add(new ISO3166Country("Belarus", "BY", "BLR", 112));
        collection.Add(new ISO3166Country("Belgium", "BE", "BEL", 56));
        collection.Add(new ISO3166Country("Belize", "BZ", "BLZ", 84));
        collection.Add(new ISO3166Country("Benin", "BJ", "BEN", 204));
        collection.Add(new ISO3166Country("Bermuda", "BM", "BMU", 60));
        collection.Add(new ISO3166Country("Bhutan", "BT", "BTN", 64));
        collection.Add(new ISO3166Country("Bolivia (Plurinational State of)", "BO", "BOL", 68));
        collection.Add(new ISO3166Country("Bonaire, Sint Eustatius and Saba", "BQ", "BES", 535));
        collection.Add(new ISO3166Country("Bosnia and Herzegovina", "BA", "BIH", 70));
        collection.Add(new ISO3166Country("Botswana", "BW", "BWA", 72));
        collection.Add(new ISO3166Country("Bouvet Island", "BV", "BVT", 74));
        collection.Add(new ISO3166Country("Brazil", "BR", "BRA", 76));
        collection.Add(new ISO3166Country("British Indian Ocean Territory", "IO", "IOT", 86));
        collection.Add(new ISO3166Country("Brunei Darussalam", "BN", "BRN", 96));
        collection.Add(new ISO3166Country("Bulgaria", "BG", "BGR", 100));
        collection.Add(new ISO3166Country("Burkina Faso", "BF", "BFA", 854));
        collection.Add(new ISO3166Country("Burundi", "BI", "BDI", 108));
        collection.Add(new ISO3166Country("Cabo Verde", "CV", "CPV", 132));
        collection.Add(new ISO3166Country("Cambodia", "KH", "KHM", 116));
        collection.Add(new ISO3166Country("Cameroon", "CM", "CMR", 120));
        collection.Add(new ISO3166Country("Canada", "CA", "CAN", 124));
        collection.Add(new ISO3166Country("Cayman Islands", "KY", "CYM", 136));
        collection.Add(new ISO3166Country("Central African Republic", "CF", "CAF", 140));
        collection.Add(new ISO3166Country("Chad", "TD", "TCD", 148));
        collection.Add(new ISO3166Country("Chile", "CL", "CHL", 152));
        collection.Add(new ISO3166Country("China", "CN", "CHN", 156));
        collection.Add(new ISO3166Country("Christmas Island", "CX", "CXR", 162));
        collection.Add(new ISO3166Country("Cocos (Keeling) Islands", "CC", "CCK", 166));
        collection.Add(new ISO3166Country("Colombia", "CO", "COL", 170));
        collection.Add(new ISO3166Country("Comoros", "KM", "COM", 174));
        collection.Add(new ISO3166Country("Congo", "CG", "COG", 178));
        collection.Add(new ISO3166Country("Congo (Democratic Republic of the)", "CD", "COD", 180));
        collection.Add(new ISO3166Country("Cook Islands", "CK", "COK", 184));
        collection.Add(new ISO3166Country("Costa Rica", "CR", "CRI", 188));
        collection.Add(new ISO3166Country("Côte d'Ivoire", "CI", "CIV", 384));
        collection.Add(new ISO3166Country("Croatia", "HR", "HRV", 191));
        collection.Add(new ISO3166Country("Cuba", "CU", "CUB", 192));
        collection.Add(new ISO3166Country("Curaçao", "CW", "CUW", 531));
        collection.Add(new ISO3166Country("Cyprus", "CY", "CYP", 196));
        collection.Add(new ISO3166Country("Czech Republic", "CZ", "CZE", 203));
        collection.Add(new ISO3166Country("Denmark", "DK", "DNK", 208));
        collection.Add(new ISO3166Country("Djibouti", "DJ", "DJI", 262));
        collection.Add(new ISO3166Country("Dominica", "DM", "DMA", 212));
        collection.Add(new ISO3166Country("Dominican Republic", "DO", "DOM", 214));
        collection.Add(new ISO3166Country("Ecuador", "EC", "ECU", 218));
        collection.Add(new ISO3166Country("Egypt", "EG", "EGY", 818));
        collection.Add(new ISO3166Country("El Salvador", "SV", "SLV", 222));
        collection.Add(new ISO3166Country("Equatorial Guinea", "GQ", "GNQ", 226));
        collection.Add(new ISO3166Country("Eritrea", "ER", "ERI", 232));
        collection.Add(new ISO3166Country("Estonia", "EE", "EST", 233));
        collection.Add(new ISO3166Country("Ethiopia", "ET", "ETH", 231));
        collection.Add(new ISO3166Country("Falkland Islands (Malvinas)", "FK", "FLK", 238));
        collection.Add(new ISO3166Country("Faroe Islands", "FO", "FRO", 234));
        collection.Add(new ISO3166Country("Fiji", "FJ", "FJI", 242));
        collection.Add(new ISO3166Country("Finland", "FI", "FIN", 246));
        collection.Add(new ISO3166Country("France", "FR", "FRA", 250));
        collection.Add(new ISO3166Country("French Guiana", "GF", "GUF", 254));
        collection.Add(new ISO3166Country("French Polynesia", "PF", "PYF", 258));
        collection.Add(new ISO3166Country("French Southern Territories", "TF", "ATF", 260));
        collection.Add(new ISO3166Country("Gabon", "GA", "GAB", 266));
        collection.Add(new ISO3166Country("Gambia", "GM", "GMB", 270));
        collection.Add(new ISO3166Country("Georgia", "GE", "GEO", 268));
        collection.Add(new ISO3166Country("Germany", "DE", "DEU", 276));
        collection.Add(new ISO3166Country("Ghana", "GH", "GHA", 288));
        collection.Add(new ISO3166Country("Gibraltar", "GI", "GIB", 292));
        collection.Add(new ISO3166Country("Greece", "GR", "GRC", 300));
        collection.Add(new ISO3166Country("Greenland", "GL", "GRL", 304));
        collection.Add(new ISO3166Country("Grenada", "GD", "GRD", 308));
        collection.Add(new ISO3166Country("Guadeloupe", "GP", "GLP", 312));
        collection.Add(new ISO3166Country("Guam", "GU", "GUM", 316));
        collection.Add(new ISO3166Country("Guatemala", "GT", "GTM", 320));
        collection.Add(new ISO3166Country("Guernsey", "GG", "GGY", 831));
        collection.Add(new ISO3166Country("Guinea", "GN", "GIN", 324));
        collection.Add(new ISO3166Country("Guinea-Bissau", "GW", "GNB", 624));
        collection.Add(new ISO3166Country("Guyana", "GY", "GUY", 328));
        collection.Add(new ISO3166Country("Haiti", "HT", "HTI", 332));
        collection.Add(new ISO3166Country("Heard Island and McDonald Islands", "HM", "HMD", 334));
        collection.Add(new ISO3166Country("Holy See", "VA", "VAT", 336));
        collection.Add(new ISO3166Country("Honduras", "HN", "HND", 340));
        collection.Add(new ISO3166Country("Hong Kong", "HK", "HKG", 344));
        collection.Add(new ISO3166Country("Hungary", "HU", "HUN", 348));
        collection.Add(new ISO3166Country("Iceland", "IS", "ISL", 352));
        collection.Add(new ISO3166Country("India", "IN", "IND", 356));
        collection.Add(new ISO3166Country("Indonesia", "ID", "IDN", 360));
        collection.Add(new ISO3166Country("Iran (Islamic Republic of)", "IR", "IRN", 364));
        collection.Add(new ISO3166Country("Iraq", "IQ", "IRQ", 368));
        collection.Add(new ISO3166Country("Ireland", "IE", "IRL", 372));
        collection.Add(new ISO3166Country("Isle of Man", "IM", "IMN", 833));
        collection.Add(new ISO3166Country("Israel", "IL", "ISR", 376));
        collection.Add(new ISO3166Country("Italy", "IT", "ITA", 380));
        collection.Add(new ISO3166Country("Jamaica", "JM", "JAM", 388));
        collection.Add(new ISO3166Country("Japan", "JP", "JPN", 392));
        collection.Add(new ISO3166Country("Jersey", "JE", "JEY", 832));
        collection.Add(new ISO3166Country("Jordan", "JO", "JOR", 400));
        collection.Add(new ISO3166Country("Kazakhstan", "KZ", "KAZ", 398));
        collection.Add(new ISO3166Country("Kenya", "KE", "KEN", 404));
        collection.Add(new ISO3166Country("Kiribati", "KI", "KIR", 296));
        collection.Add(new ISO3166Country("Korea (Democratic People's Republic of)", "KP", "PRK", 408));
        collection.Add(new ISO3166Country("Korea (Republic of)", "KR", "KOR", 410));
        collection.Add(new ISO3166Country("Kuwait", "KW", "KWT", 414));
        collection.Add(new ISO3166Country("Kyrgyzstan", "KG", "KGZ", 417));
        collection.Add(new ISO3166Country("Lao People's Democratic Republic", "LA", "LAO", 418));
        collection.Add(new ISO3166Country("Latvia", "LV", "LVA", 428));
        collection.Add(new ISO3166Country("Lebanon", "LB", "LBN", 422));
        collection.Add(new ISO3166Country("Lesotho", "LS", "LSO", 426));
        collection.Add(new ISO3166Country("Liberia", "LR", "LBR", 430));
        collection.Add(new ISO3166Country("Libya", "LY", "LBY", 434));
        collection.Add(new ISO3166Country("Liechtenstein", "LI", "LIE", 438));
        collection.Add(new ISO3166Country("Lithuania", "LT", "LTU", 440));
        collection.Add(new ISO3166Country("Luxembourg", "LU", "LUX", 442));
        collection.Add(new ISO3166Country("Macao", "MO", "MAC", 446));
        collection.Add(new ISO3166Country("Macedonia (the former Yugoslav Republic of)", "MK", "MKD", 807));
        collection.Add(new ISO3166Country("Madagascar", "MG", "MDG", 450));
        collection.Add(new ISO3166Country("Malawi", "MW", "MWI", 454));
        collection.Add(new ISO3166Country("Malaysia", "MY", "MYS", 458));
        collection.Add(new ISO3166Country("Maldives", "MV", "MDV", 462));
        collection.Add(new ISO3166Country("Mali", "ML", "MLI", 466));
        collection.Add(new ISO3166Country("Malta", "MT", "MLT", 470));
        collection.Add(new ISO3166Country("Marshall Islands", "MH", "MHL", 584));
        collection.Add(new ISO3166Country("Martinique", "MQ", "MTQ", 474));
        collection.Add(new ISO3166Country("Mauritania", "MR", "MRT", 478));
        collection.Add(new ISO3166Country("Mauritius", "MU", "MUS", 480));
        collection.Add(new ISO3166Country("Mayotte", "YT", "MYT", 175));
        collection.Add(new ISO3166Country("Mexico", "MX", "MEX", 484));
        collection.Add(new ISO3166Country("Micronesia (Federated States of)", "FM", "FSM", 583));
        collection.Add(new ISO3166Country("Moldova (Republic of)", "MD", "MDA", 498));
        collection.Add(new ISO3166Country("Monaco", "MC", "MCO", 492));
        collection.Add(new ISO3166Country("Mongolia", "MN", "MNG", 496));
        collection.Add(new ISO3166Country("Montenegro", "ME", "MNE", 499));
        collection.Add(new ISO3166Country("Montserrat", "MS", "MSR", 500));
        collection.Add(new ISO3166Country("Morocco", "MA", "MAR", 504));
        collection.Add(new ISO3166Country("Mozambique", "MZ", "MOZ", 508));
        collection.Add(new ISO3166Country("Myanmar", "MM", "MMR", 104));
        collection.Add(new ISO3166Country("Namibia", "NA", "NAM", 516));
        collection.Add(new ISO3166Country("Nauru", "NR", "NRU", 520));
        collection.Add(new ISO3166Country("Nepal", "NP", "NPL", 524));
        collection.Add(new ISO3166Country("Netherlands", "NL", "NLD", 528));
        collection.Add(new ISO3166Country("New Caledonia", "NC", "NCL", 540));
        collection.Add(new ISO3166Country("New Zealand", "NZ", "NZL", 554));
        collection.Add(new ISO3166Country("Nicaragua", "NI", "NIC", 558));
        collection.Add(new ISO3166Country("Niger", "NE", "NER", 562));
        collection.Add(new ISO3166Country("Nigeria", "NG", "NGA", 566));
        collection.Add(new ISO3166Country("Niue", "NU", "NIU", 570));
        collection.Add(new ISO3166Country("Norfolk Island", "NF", "NFK", 574));
        collection.Add(new ISO3166Country("Northern Mariana Islands", "MP", "MNP", 580));
        collection.Add(new ISO3166Country("Norway", "NO", "NOR", 578));
        collection.Add(new ISO3166Country("Oman", "OM", "OMN", 512));
        collection.Add(new ISO3166Country("Pakistan", "PK", "PAK", 586));
        collection.Add(new ISO3166Country("Palau", "PW", "PLW", 585));
        collection.Add(new ISO3166Country("Palestine, State of", "PS", "PSE", 275));
        collection.Add(new ISO3166Country("Panama", "PA", "PAN", 591));
        collection.Add(new ISO3166Country("Papua New Guinea", "PG", "PNG", 598));
        collection.Add(new ISO3166Country("Paraguay", "PY", "PRY", 600));
        collection.Add(new ISO3166Country("Peru", "PE", "PER", 604));
        collection.Add(new ISO3166Country("Philippines", "PH", "PHL", 608));
        collection.Add(new ISO3166Country("Pitcairn", "PN", "PCN", 612));
        collection.Add(new ISO3166Country("Poland", "PL", "POL", 616));
        collection.Add(new ISO3166Country("Portugal", "PT", "PRT", 620));
        collection.Add(new ISO3166Country("Puerto Rico", "PR", "PRI", 630));
        collection.Add(new ISO3166Country("Qatar", "QA", "QAT", 634));
        collection.Add(new ISO3166Country("Réunion", "RE", "REU", 638));
        collection.Add(new ISO3166Country("Romania", "RO", "ROU", 642));
        collection.Add(new ISO3166Country("Russian Federation", "RU", "RUS", 643));
        collection.Add(new ISO3166Country("Rwanda", "RW", "RWA", 646));
        collection.Add(new ISO3166Country("Saint Barthélemy", "BL", "BLM", 652));
        collection.Add(new ISO3166Country("Saint Helena, Ascension and Tristan da Cunha", "SH", "SHN", 654));
        collection.Add(new ISO3166Country("Saint Kitts and Nevis", "KN", "KNA", 659));
        collection.Add(new ISO3166Country("Saint Lucia", "LC", "LCA", 662));
        collection.Add(new ISO3166Country("Saint Martin (French part)", "MF", "MAF", 663));
        collection.Add(new ISO3166Country("Saint Pierre and Miquelon", "PM", "SPM", 666));
        collection.Add(new ISO3166Country("Saint Vincent and the Grenadines", "VC", "VCT", 670));
        collection.Add(new ISO3166Country("Samoa", "WS", "WSM", 882));
        collection.Add(new ISO3166Country("San Marino", "SM", "SMR", 674));
        collection.Add(new ISO3166Country("Sao Tome and Principe", "ST", "STP", 678));
        collection.Add(new ISO3166Country("Saudi Arabia", "SA", "SAU", 682));
        collection.Add(new ISO3166Country("Senegal", "SN", "SEN", 686));
        collection.Add(new ISO3166Country("Serbia", "RS", "SRB", 688));
        collection.Add(new ISO3166Country("Seychelles", "SC", "SYC", 690));
        collection.Add(new ISO3166Country("Sierra Leone", "SL", "SLE", 694));
        collection.Add(new ISO3166Country("Singapore", "SG", "SGP", 702));
        collection.Add(new ISO3166Country("Sint Maarten (Dutch part)", "SX", "SXM", 534));
        collection.Add(new ISO3166Country("Slovakia", "SK", "SVK", 703));
        collection.Add(new ISO3166Country("Slovenia", "SI", "SVN", 705));
        collection.Add(new ISO3166Country("Solomon Islands", "SB", "SLB", 90));
        collection.Add(new ISO3166Country("Somalia", "SO", "SOM", 706));
        collection.Add(new ISO3166Country("South Africa", "ZA", "ZAF", 710));
        collection.Add(new ISO3166Country("South Georgia and the South Sandwich Islands", "GS", "SGS", 239));
        collection.Add(new ISO3166Country("South Sudan", "SS", "SSD", 728));
        collection.Add(new ISO3166Country("Spain", "ES", "ESP", 724));
        collection.Add(new ISO3166Country("Sri Lanka", "LK", "LKA", 144));
        collection.Add(new ISO3166Country("Sudan", "SD", "SDN", 729));
        collection.Add(new ISO3166Country("Suriname", "SR", "SUR", 740));
        collection.Add(new ISO3166Country("Svalbard and Jan Mayen", "SJ", "SJM", 744));
        collection.Add(new ISO3166Country("Swaziland", "SZ", "SWZ", 748));
        collection.Add(new ISO3166Country("Sweden", "SE", "SWE", 752));
        collection.Add(new ISO3166Country("Switzerland", "CH", "CHE", 756));
        collection.Add(new ISO3166Country("Syrian Arab Republic", "SY", "SYR", 760));
        collection.Add(new ISO3166Country("Taiwan, Province of China[a]", "TW", "TWN", 158));
        collection.Add(new ISO3166Country("Tajikistan", "TJ", "TJK", 762));
        collection.Add(new ISO3166Country("Tanzania, United Republic of", "TZ", "TZA", 834));
        collection.Add(new ISO3166Country("Thailand", "TH", "THA", 764));
        collection.Add(new ISO3166Country("Timor-Leste", "TL", "TLS", 626));
        collection.Add(new ISO3166Country("Togo", "TG", "TGO", 768));
        collection.Add(new ISO3166Country("Tokelau", "TK", "TKL", 772));
        collection.Add(new ISO3166Country("Tonga", "TO", "TON", 776));
        collection.Add(new ISO3166Country("Trinidad and Tobago", "TT", "TTO", 780));
        collection.Add(new ISO3166Country("Tunisia", "TN", "TUN", 788));
        collection.Add(new ISO3166Country("Turkey", "TR", "TUR", 792));
        collection.Add(new ISO3166Country("Turkmenistan", "TM", "TKM", 795));
        collection.Add(new ISO3166Country("Turks and Caicos Islands", "TC", "TCA", 796));
        collection.Add(new ISO3166Country("Tuvalu", "TV", "TUV", 798));
        collection.Add(new ISO3166Country("Uganda", "UG", "UGA", 800));
        collection.Add(new ISO3166Country("Ukraine", "UA", "UKR", 804));
        collection.Add(new ISO3166Country("United Arab Emirates", "AE", "ARE", 784));
        collection.Add(new ISO3166Country("United Kingdom of Great Britain and Northern Ireland", "GB", "GBR", 826));
        collection.Add(new ISO3166Country("United States of America", "US", "USA", 840));
        collection.Add(new ISO3166Country("United States Minor Outlying Islands", "UM", "UMI", 581));
        collection.Add(new ISO3166Country("Uruguay", "UY", "URY", 858));
        collection.Add(new ISO3166Country("Uzbekistan", "UZ", "UZB", 860));
        collection.Add(new ISO3166Country("Vanuatu", "VU", "VUT", 548));
        collection.Add(new ISO3166Country("Venezuela (Bolivarian Republic of)", "VE", "VEN", 862));
        collection.Add(new ISO3166Country("Viet Nam", "VN", "VNM", 704));
        collection.Add(new ISO3166Country("Virgin Islands (British)", "VG", "VGB", 92));
        collection.Add(new ISO3166Country("Virgin Islands (U.S.)", "VI", "VIR", 850));
        collection.Add(new ISO3166Country("Wallis and Futuna", "WF", "WLF", 876));
        collection.Add(new ISO3166Country("Western Sahara", "EH", "ESH", 732));
        collection.Add(new ISO3166Country("Yemen", "YE", "YEM", 887));
        collection.Add(new ISO3166Country("Zambia", "ZM", "ZMB", 894));
        collection.Add(new ISO3166Country("Zimbabwe", "ZW", "ZWE", 716));

        return collection;
    }
    #endregion
}

/// <summary>
/// Representation of an ISO3166-1 Country
/// </summary>
public class ISO3166Country
{
    public ISO3166Country(string name, string alpha2, string alpha3, int numericCode)
    {
        this.Name = name;
        this.Alpha2 = alpha2;
        this.Alpha3 = alpha3;
        this.NumericCode = numericCode;
    }

    public string Name { get; private set; }

    public string Alpha2 { get; private set; }

    public string Alpha3 { get; private set; }

    public int NumericCode { get; private set; }
}

然后您可以使用以下方法进行查找:

var Iso3ToIso2Mapping = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
  .Select(ci => new RegionInfo(ci.LCID))
  .GroupBy(ri => ri.ThreeLetterISORegionName)
  .ToDictionary(g => g.Key, g => g.First().TwoLetterISORegionName);

打印:

var iso2CountryCode = Iso3ToIso2Mapping["AUS"];
Console.WriteLine(iso2CountryCode);

1
投票

AU Nuget程序包,提供了ISO3166国家/地区的列表,其中包括名称,2和3字母代码以及数字代码。


0
投票

对潜伏者来说:如果您只是想为.NET世界中给定的两个字母的语言名称派生“默认”国家(例如,“ en” =>“ US”, “ ja” =>“ JP”),就可以了]]

ISO3166

我正在使用仅接受ISO3166代码的JS库,并且我们的网站针对25种语言提供了完美支持。始终测试您的方案


0
投票

所有国际国家代码和国家名称的C#类


0
投票

您可以尝试使用.NET RegionInfo

© www.soinside.com 2019 - 2024. All rights reserved.