这个类用于收集有关国家和相关文化的信息。
它的构造函数允许您选择是加载所有可能的国家还是只加载特定的文化:
CountryList countries = new CountryList([true][false])
true
表示 CultureTypes.AllCultures,false
表示 CultureTypes.SpecificCultures。
例如,使用这些参数:
CountryList countries = new CountryList(true)
List<CountryInfo> countryInfo = countries.GetCountryInfoByName("United States", false)
(在 GetCountryInfoByName()
中的 true
/false
意味着使用/不使用本地名称)
该方法返回三个结果:
1个国家 - 美国
3种文化 - 英语,英语(美国),西班牙语(美国)
使用本地名称:
List<CountryInfo> countryInfo = Countries.GetCountryInfoByName("United States", true)
1 国家 - 美国
2 文化 - 英语,英语(美国)
带有特定文化和本地名称:
CountryList countries = new CountryList(false)
List<CountryInfo> countryInfo = countries.GetCountryInfoByName("United States", true)
1 国家 - 美国
1 文化 - 英语(美国)
更多与您问题相关的内容,此类公开了以下方法:
string twoLettersName = countries.GetTwoLettersName("United States", true)
返回美国
string threeLettersName = countries.GetThreeLettersName("United States", true)
返回
美国
。
List<string> ietfTags = countries.GetIetfLanguageTag("United States", true)
返回
en-US
。
List<int> geoIds = countries.GetRegionGeoId("United States", true)
返回
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; }
}