Reputation: 3310
Is there a way to get the ISO ALPHA-2 code (country code) from a country name such as United Kingdom = GB?
I'm trying to achieve the nearly the opposite of the code below
//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());
}
Upvotes: 13
Views: 23573
Reputation: 32223
This Class is used to you collect information on Countries and related Cultures.
Its Constructor lets you choose if you want to load all possible Countries or just the Specific Cultures:
CountryList countries = new CountryList([true][false]);
where true
means CultureTypes.AllCultures
and false
means CultureTypes.SpecificCultures
For example, whith these parameters:
CountryList countries = new CountryList(true);
List<CountryInfo> countryInfo = countries.GetCountryInfoByName("United States", false);
(true
/false
in GetCountryInfoByName()
mean use/don't the Native Name)
this method returns three results:
1 Country - United States
3 Cultures - English, English (United States), Spanish (United States)
Using the Native Name:
List<CountryInfo> countryInfo = Countries.GetCountryInfoByName("United States", true);
1 Country - United States
2 Cultures - English, English (United States)
With Specific Cultures and Native Names:
CountryList countries = new CountryList(false);
List<CountryInfo> countryInfo = countries.GetCountryInfoByName("United States", true);
1 Country - United States
1 Culture - English (United States)
More related to your question, this Class exposes these methods:
string twoLettersName = countries.GetTwoLettersName("United States", true);
Returns US
string threeLettersName = countries.GetThreeLettersName("United States", true);
Returns USA
List<string> ietfTags = countries.GetIetfLanguageTag("United States", true);
Returns en-US
List<int> geoIds = countries.GetRegionGeoId("United States", true);
Returns 244
public class CountryList {
private CultureTypes cultureType;
public CountryList(bool AllCultures)
{
cultureType = AllCultures ? CultureTypes.AllCultures : CultureTypes.SpecificCultures;
Countries = GetAllCountries(cultureType);
}
public List<CountryInfo> Countries { get; set; }
public List<CountryInfo> GetCountryInfoByName(string CountryName, bool NativeName)
{
return NativeName ? Countries.Where(info => info.Region?.NativeName == CountryName).ToList()
: Countries.Where(info => info.Region?.EnglishName == CountryName).ToList();
}
public List<CountryInfo> GetCountryInfoByName(string CountryName, bool NativeName, bool IsNeutral)
{
return NativeName ? Countries.Where(info => info.Region?.NativeName == CountryName &&
info.Culture?.IsNeutralCulture == IsNeutral).ToList()
: Countries.Where(info => info.Region?.EnglishName == CountryName &&
info.Culture?.IsNeutralCulture == IsNeutral).ToList();
}
public string? GetTwoLettersName(string CountryName, bool NativeName)
{
CountryInfo? country = NativeName ? Countries.Where(info => info.Region?.NativeName == CountryName).FirstOrDefault()
: Countries.Where(info => info.Region?.EnglishName == CountryName).FirstOrDefault();
return country?.Region?.TwoLetterISORegionName;
}
public string? GetThreeLettersName(string CountryName, bool NativeName)
{
CountryInfo? country = NativeName ? Countries.Where(info => info.Region?.NativeName == CountryName).FirstOrDefault()
: Countries.Where(info => info.Region?.EnglishName == CountryName).FirstOrDefault();
return country?.Region?.ThreeLetterISORegionName;
}
public List<string?>? GetIetfLanguageTag(string CountryName, bool UseNativeName)
{
return UseNativeName ? Countries.Where(info => info.Region?.NativeName == CountryName)
.Select(info => info.Culture?.IetfLanguageTag).ToList()
: Countries.Where(info => info.Region?.EnglishName == CountryName)
.Select(info => info.Culture?.IetfLanguageTag).ToList();
}
public List<int?>? GetRegionGeoId(string CountryName, bool UseNativeName)
{
return UseNativeName ? Countries.Where(info => info.Region?.NativeName == CountryName)
.Select(info => info.Region?.GeoId).ToList()
: 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; }
}
Upvotes: 8
Reputation: 1079
You can use my package Nager.Country
. I have a functionality that makes this process easy. It is also possible to search for the country with a Spanish name e.x. 'Reino Unido'
PM> install-package Nager.Country
ICountryProvider countryProvider = new CountryProvider();
var countryInfo = countryProvider.GetCountryByName("United Kingdom");
//countryInfo.Alpha2Code = GB
Upvotes: 1
Reputation: 1289
In the above your are stripping only the DisplayName from the "country"... You should be able to get the code from that country value as well since you are looping through each culture and getting the country from the culture name.
Instead of a list of names, why not make a Dictionary of (code, DisplayName)?
You can easily look up a value(display name) from a key(code) or a key(code) from a value(display name), and you have the benefit of maintaining only 1 collection.
Upvotes: 0
Reputation: 629
You can do something like this:
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;
Upvotes: 17