Surprisingly, there are loads of great data snippets hidden inside .Net classes. If you've ever found yourself adding localization to your website, you may have come across the CultureInfo Class in .Net. I know that sometimes I have needed to get a list of country ISO codes and have struggled to get a comprehensive list, or I have needed to retrieve the list from a Database containing the names. This can add an extra Database call and could result in extra code maintenance, and possibly a performance hit.
Well, this is where CultureInfo comes to the rescue. Using this Class, you will be able to easily retrieve a list of country names and their ISO codes using built-in .Net code.
Dictionary<string, string> cultureDetails = new Dictionary<string, string>();
foreach (CultureInfo cultureInfo in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{
RegionInfo regionInfo = new RegionInfo(cultureInfo.Name);
if (!cultureDetails.ContainsKey(regionInfo.EnglishName))
{
cultureDetails.Add(regionInfo.EnglishName, regionInfo.Name);
}
}
var orderedCultureDetails = cultureDetails.OrderBy(x => x.Key);
Which gives you a list of 128 countries and their region information:
Read more: Coding Tips & Tricks
QR:
0 comments:
Post a Comment