在 .NET 中有什么方法可以将三个字母的国家代码(在 ISO 3166-1 alpha-3 中定义)转换为两个字母的语言代码(在 ISO 3166-1 alpha-2 中定义),例如。将 BEL 转换为 BE?
看过 System.Globalization 中的 RegionInfo 类,但构造函数似乎不支持三个字母代码。
在 .NET 中有什么方法可以将三个字母的国家代码(在 ISO 3166-1 alpha-3 中定义)转换为两个字母的语言代码(在 ISO 3166-1 alpha-2 中定义),例如。将 BEL 转换为 BE?
看过 System.Globalization 中的 RegionInfo 类,但构造函数似乎不支持三个字母代码。
该类RegionInfo
确实知道三字母代码(在ThreeLetterISORegionName
属性中),但我认为没有办法RegionInfo
基于此代码,您需要枚举所有区域并将它们添加到您自己的字典中,三个-字母代码作为键。
但是,我认为 .NET Framework 用于RegionInfo
处理文化,而不是 ISO 3166-1 意义上的国家。因此,ISO 3166-1 标准中的许多国家/地区不可用(尝试例如SX
)。我想你应该创建自己的国家密码本。
编辑:来自我当前国家代码簿中的 246 个国家,RegionInfo
其中 125 个可用,其余 (121) 个不受支持。结论:这不是获取国家密码本的好方法。
看起来没有内置的方法可以做到这一点。您可以使用GenericTypeTea 的答案中的方法。但是如果您需要转换多个国家代码,您可以避免使用 Cultures 枚举并预先计算映射字典以进行国家代码转换。
这就是这样的生成器的样子:
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("};");
}
}
}
运行此生成器以获取国家代码映射字典,如下所示:
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
};
更新:
之前没有正确阅读问题。现在应该正确地将以下内容从 ISO 3166-1 alpha-3 转换为 ISO 3166-1 alpha-2:
没有内置的方法。您需要遍历CultureInfo
s 以获取RegionInfo
s 以便手动找到匹配项(这是非常低效的,因此建议进行一些缓存):
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;
}
我只是将这个快速而肮脏的类串在一起来查询和管理 ISO3166-1 条目。只有一个示例方法可以通过 alpha 3 代码进行查询,但应该足够简单,可以根据需要添加其他转换。
示例用法:
string countryName = ISO3166.FromAlpha3("LUX").Name;
班级:
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; }
}
上面的一些答案对于查找来说是 O(N),如果你只做一次,这很好,但通常你会希望每个进程不止一次调用它,在这种情况下,你可能需要更高效的东西.
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);
印刷:
AU
ISO3166 Nuget包提供了 ISO3166 国家/地区列表,包括名称、2 和 3 字母代码以及数字代码。
所有国际国家代码和国家名称的 C# 类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace International_Country_Name_And_Code
{
public class Class1
{
public string Name { get; set; }
public string ISO { get; set; }
public string Code { get; set; }
string[][] Country = new string[][]
{
new string [] { "Afghanistan", "+93", "AF" },
new string [] { "Albania", "+355", "AL" },
new string [] { "Algeria", "+213", "DZ" },
new string [] { "AmericanSamoa", "+1 684", "AS" },
new string [] { "Andorra", "+376", "AD" },
new string [] { "Angola", "+244", "AO" },
new string [] { "Anguilla", "+1 264", "AI" },
new string [] { "Antigua and Barbuda", "+1268", "AG" },
new string [] { "Argentina", "+54", "AR" },
new string [] { "Armenia", "+374", "AM" },
new string [] { "Aruba", "+297", "AW" },
new string [] { "Australia", "+61", "AU" },
new string [] { "Austria", "+43", "AT" },
new string [] { "Azerbaijan", "+994", "AZ" },
new string [] { "Bahamas", "+1 242", "BS" },
new string [] { "Bahrain", "+973", "BH" },
new string [] { "Bangladesh", "+880", "BD" },
new string [] { "Barbados", "+1 246", "BB" },
new string [] { "Belarus", "+375", "BY" },
new string [] { "Belgium", "+32", "BE" },
new string [] { "Belize", "+501", "BZ" },
new string [] { "Benin", "+229", "BJ" },
new string [] { "Bermuda", "+1 441", "BM" },
new string [] { "Bhutan", "+975", "BT" },
new string [] { "Bosnia and Herzegovina", "+387", "BA" },
new string [] { "Botswana", "+267", "BW" },
new string [] { "Brazil", "+55", "BR" },
new string [] { "British Indian Ocean Territory", "+246", "IO" },
new string [] { "Bulgaria", "+359", "BG" },
new string [] { "Burkina Faso", "+226", "BF" },
new string [] { "Burundi", "+257", "BI" },
new string [] { "Cambodia", "+855", "KH" },
new string [] { "Cameroon", "+237", "CM" },
new string [] { "Canada", "+1", "CA" },
new string [] { "Cape Verde", "+238", "CV" },
new string [] { "Cayman Islands", "+ 345", "KY" },
new string [] { "Central African Republic", "+236", "CF" },
new string [] { "Chad", "+235", "TD" },
new string [] { "Chile", "+56", "CL" },
new string [] { "China", "+86", "CN" },
new string [] { "Christmas Island", "+61", "CX" },
new string [] { "Colombia", "+57", "CO" },
new string [] { "Comoros", "+269", "KM" },
new string [] { "Congo", "+242", "CG" },
new string [] { "Cook Islands", "+682", "CK" },
new string [] { "Costa Rica", "+506", "CR" },
new string [] { "Croatia", "+385", "HR" },
new string [] { "Cuba", "+53", "CU" },
new string [] { "Cyprus", "+537", "CY" },
new string [] { "Czech Republic", "+420", "CZ" },
new string [] { "Denmark", "+45", "DK" },
new string [] { "Djibouti", "+253", "DJ" },
new string [] { "Dominica", "+1 767", "DM" },
new string [] { "Dominican Republic", "+1 849", "DO" },
new string [] { "Ecuador", "+593", "EC" },
new string [] { "Egypt", "+20", "EG" },
new string [] { "El Salvador", "+503", "SV" },
new string [] { "Equatorial Guinea", "+240", "GQ" },
new string [] { "Eritrea", "+291", "ER" },
new string [] { "Estonia", "+372", "EE" },
new string [] { "Ethiopia", "+251", "ET" },
new string [] { "Faroe Islands", "+298", "FO" },
new string [] { "Fiji", "+679", "FJ" },
new string [] { "Finland", "+358", "FI" },
new string [] { "France", "+33", "FR" },
new string [] { "French Guiana", "+594", "GF" },
new string [] { "French Polynesia", "+689", "PF" },
new string [] { "Gabon", "+241", "GA" },
new string [] { "Gambia", "+220", "GM" },
new string [] { "Georgia", "+995", "GE" },
new string [] { "Germany", "+49", "DE" },
new string [] { "Ghana", "+233", "GH" },
new string [] { "Gibraltar", "+350", "GI" },
new string [] { "Greece", "+30", "GR" },
new string [] { "Greenland", "+299", "GL" },
new string [] { "Grenada", "+1 473", "GD" },
new string [] { "Guadeloupe", "+590", "GP" },
new string [] { "Guam", "+1 671", "GU" },
new string [] { "Guatemala", "+502", "GT" },
new string [] { "Guinea", "+224", "GN" },
new string [] { "Guinea-Bissau", "+245", "GW" },
new string [] { "Guyana", "+595", "GY" },
new string [] { "Haiti", "+509", "HT" },
new string [] { "Honduras", "+504", "HN" },
new string [] { "Hungary", "+36", "HU" },
new string [] { "Iceland", "+354", "IS" },
new string [] { "India", "+91", "IN" },
new string [] { "Indonesia", "+62", "ID" },
new string [] { "Iraq", "+964", "IQ" },
new string [] { "Ireland", "+353", "IE" },
new string [] { "Israel", "+972", "IL" },
new string [] { "Italy", "+39", "IT" },
new string [] { "Jamaica", "+1 876", "JM" },
new string [] { "Japan", "+81", "JP" },
new string [] { "Jordan", "+962", "JO" },
new string [] { "Kazakhstan", "+7 7", "KZ" },
new string [] { "Kenya", "+254", "KE" },
new string [] { "Kiribati", "+686", "KI" },
new string [] { "Kuwait", "+965", "KW" },
new string [] { "Kyrgyzstan", "+996", "KG" },
new string [] { "Latvia", "+371", "LV" },
new string [] { "Lebanon", "+961", "LB" },
new string [] { "Lesotho", "+266", "LS" },
new string [] { "Liberia", "+231", "LR" },
new string [] { "Liechtenstein", "+423", "LI" },
new string [] { "Lithuania", "+370", "LT" },
new string [] { "Luxembourg", "+352", "LU" },
new string [] { "Madagascar", "+261", "MG" },
new string [] { "Malawi", "+265", "MW" },
new string [] { "Malaysia", "+60", "MY" },
new string [] { "Maldives", "+960", "MV" },
new string [] { "Mali", "+223", "ML" },
new string [] { "Malta", "+356", "MT" },
new string [] { "Marshall Islands", "+692", "MH" },
new string [] { "Martinique", "+596", "MQ" },
new string [] { "Mauritania", "+222", "MR" },
new string [] { "Mauritius", "+230", "MU" },
new string [] { "Mayotte", "+262", "YT" },
new string [] { "Mexico", "+52", "MX" },
new string [] { "Monaco", "+377", "MC" },
new string [] { "Mongolia", "+976", "MN" },
new string [] { "Montenegro", "+382", "ME" },
new string [] { "Montserrat", "+1664", "MS" },
new string [] { "Morocco", "+212", "MA" },
new string [] { "Myanmar", "+95", "MM" },
new string [] { "Namibia", "+264", "NA" },
new string [] { "Nauru", "+674", "NR" },
new string [] { "Nepal", "+977", "NP" },
new string [] { "Netherlands", "+31", "NL" },
new string [] { "Netherlands Antilles", "+599", "AN" },
new string [] { "New Caledonia", "+687", "NC" },
new string [] { "New Zealand", "+64", "NZ" },
new string [] { "Nicaragua", "+505", "NI" },
new string [] { "Niger", "+227", "NE" },
new string [] { "Nigeria", "+234", "NG" },
new string [] { "Niue", "+683", "NU" },
new string [] { "Northern Mariana Islands", "+1670", "MP" },
new string [] { "Norway", "+47", "NO" },
new string [] { "Oman", "+968", "OM" },
new string [] { "Pakistan", "+92", "PK" },
new string [] { "Palau", "+680", "PW" },
new string [] { "Panama", "+507", "PA" },
new string [] { "Papua New Guinea", "+675", "PG" },
new string [] { "Paraguay", "+595", "PY" },
new string [] { "Peru", "+51", "PE" },
new string [] { "Philippines", "+63", "PH" },
new string [] { "Poland", "+48", "PL" },
new string [] { "Portugal", "+351", "PT" },
new string [] { "Puerto Rico", "+1939", "PR" },
new string [] { "Qatar", "+974", "QA" },
new string [] { "Romania", "+40", "RO" },
new string [] { "Rwanda", "+250", "RW" },
new string [] { "Samoa", "+685", "WS" },
new string [] { "San Marino", "+378", "SM" },
new string [] { "Saudi Arabia", "+966", "SA" },
new string [] { "Senegal", "+221", "SN" },
new string [] { "Serbia", "+381", "RS" },
new string [] { "Seychelles", "+248", "SC" },
new string [] { "Sierra Leone", "+232", "SL" },
new string [] { "Singapore", "+65", "SG" },
new string [] { "Slovakia", "+421", "SK" },
new string [] { "Slovenia", "+386", "SI" },
new string [] { "Solomon Islands", "+677", "SB" },
new string [] { "South Africa", "+27", "ZA" },
new string [] { "South Georgia and the South Sandwich Islands", "+500", "GS" },
new string [] { "Spain", "+34", "ES" },
new string [] { "Sri Lanka", "+94", "LK" },
new string [] { "Sudan", "+249", "SD" },
new string [] { "Suriname", "+597", "SR" },
new string [] { "Swaziland", "+268", "SZ" },
new string [] { "Sweden", "+46", "SE" },
new string [] { "Switzerland", "+41", "CH" },
new string [] { "Tajikistan", "+992", "TJ" },
new string [] { "Thailand", "+66", "TH" },
new string [] { "Togo", "+228", "TG" },
new string [] { "Tokelau", "+690", "TK" },
new string [] { "Tonga", "+676", "TO" },
new string [] { "Trinidad and Tobago", "+1 868", "TT" },
new string [] { "Tunisia", "+216", "TN" },
new string [] { "Turkey", "+90", "TR" },
new string [] { "Turkmenistan", "+993", "TM" },
new string [] { "Turks and Caicos Islands", "+1 649", "TC" },
new string [] { "Tuvalu", "+688", "TV" },
new string [] { "Uganda", "+256", "UG" },
new string [] { "Ukraine", "+380", "UA" },
new string [] { "United Arab Emirates", "+971", "AE" },
new string [] { "United Kingdom", "+44", "GB" },
new string [] { "United States", "+1", "US" },
new string [] { "Uruguay", "+598", "UY" },
new string [] { "Uzbekistan", "+998", "UZ" },
new string [] { "Vanuatu", "+678", "VU" },
new string [] { "Wallis and Futuna", "+681", "WF" },
new string [] { "Yemen", "+967", "YE" },
new string [] { "Zambia", "+260", "ZM" },
new string [] { "Zimbabwe", "+263", "ZW" },
new string [] { "Åland Islands", "+358", "AX" },
new string [] { "Antarctica", "+672", "AQ" },
new string [] { "Bolivia, Plurinational State of", "+591", "BO" },
new string [] { "Brunei Darussalam", "+673", "BN" },
new string [] { "Cocos (Keeling) Islands", "+61", "CC" },
new string [] { "Congo, The Democratic Republic of the", "+243", "CD" },
new string [] { "Cote d'Ivoire", "+225", "CI" },
new string [] { "Falkland Islands (Malvinas)", "+500", "FK" },
new string [] { "Guernsey", "+44", "GG" },
new string [] { "Holy See (Vatican City State)", "+379", "VA" },
new string [] { "Hong Kong", "+852", "HK" },
new string [] { "Iran, Islamic Republic of", "+98", "IR" },
new string [] { "Isle of Man", "+44", "IM" },
new string [] { "Jersey", "+44", "JE" },
new string [] { "Korea, Democratic People's Republic of", "+850", "KP" },
new string [] { "Korea, Republic of", "+82", "KR" },
new string [] { "Lao People's Democratic Republic", "+856", "LA" },
new string [] { "Libyan Arab Jamahiriya", "+218", "LY" },
new string [] { "Macao", "+853", "MO" },
new string [] { "Macedonia, The Former Yugoslav Republic of", "+389", "MK" },
new string [] { "Micronesia, Federated States of", "+691", "FM" },
new string [] { "Moldova, Republic of", "+373", "MD" },
new string [] { "Mozambique", "+258", "MZ" },
new string [] { "Palestinian Territory, Occupied", "+970", "PS" },
new string [] { "Pitcairn", "+872", "PN" },
new string [] { "Réunion", "+262", "RE" },
new string [] { "Russia", "+7", "RU" },
new string [] { "Saint Barthélemy", "+590", "BL" },
new string [] { "Saint Helena, Ascension and Tristan Da Cunha", "+290", "SH" },
new string [] { "Saint Kitts and Nevis", "+1 869", "KN" },
new string [] { "Saint Lucia", "+1 758", "LC" },
new string [] { "Saint Martin", "+590", "MF" },
new string [] { "Saint Pierre and Miquelon", "+508", "PM" },
new string [] { "Saint Vincent and the Grenadines", "+1 784", "VC" },
new string [] { "Sao Tome and Principe", "+239", "ST" },
new string [] { "Somalia", "+252", "SO" },
new string [] { "Svalbard and Jan Mayen", "+47", "SJ" },
new string [] { "Syrian Arab Republic", "+963", "SY" },
new string [] { "Taiwan, Province of China", "+886", "TW" },
new string [] { "Tanzania, United Republic of", "+255", "TZ" },
new string [] { "Timor-Leste", "+670", "TL" },
new string [] { "Venezuela, Bolivarian Republic of", "+58", "VE" },
new string [] { "Viet Nam", "+84", "VN" },
new string [] { "Virgin Islands, British", "+1 284", "VG" },
new string [] { "Virgin Islands, U.S.", "+1 340", "VI" }
};
}
}
请参阅 Nuget 包'Bia.Countries'
https://github.com/ilyabreev/Bia.Countries做得很好
Bia.Countries.Iso3166.Countries.GetCountryByAlpha2()
对于潜伏者:如果您只想为 .NET 世界中给定的两个字母语言名称派生“默认”国家/地区(例如“ en ”=>“ US ”、“ ja ”=>“ JP ”),这可以解决问题
// Can be read from CultureInfo.Name (or CultureInfo.Parent.Name if using non-generic culture)
var language = "ja"; // Japanese
// Creates "ja-JP" culture
var defaultCulture = CultureInfo.CreateSpecificCulture(language);
// After this, just take the split[1] (careful, check index) to get country code
var split = defaultCulture.Name.Split('-');
我使用一个只接受 ISO3166 代码的 JS 库,并且对于 25 种语言,我们的网站完美地支持它。始终测试您的场景
您可以尝试使用此处描述的.NET RegionInfo:
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
*/