Reputation: 1171
I got 2 methods: one that converts a given ISO3166 code to the corresponding country's name in German (e.g. "de-DE" to "Deutschland"). This works fine. Now I need a method that works the other way around. Currently it looks like this:
public static string GetISO3166CodeForGermanCountryName(string countryName)
{
if (string.IsNullOrEmpty(countryName) || countryName.Length == 2)
return countryName;
Thread.CurrentThread.CurrentCulture = new CultureInfo("de-AT");
CultureInfo cInfo = cultures.FirstOrDefault(culture => new RegionInfo(culture.LCID).DisplayName == countryName);
if (cInfo != null && cInfo.Name.Contains("-"))
return cInfo.Name.Split('-')[1]; //take the 2nd part of the culture info name (e.g. "de-AT" --> "AT")
else
return null;
}
This works fine on my dev machine because the installed .Net framework is in German. However, on our live server (Windows Server 2012), the method returns null for all inputs. This happens because the installed .Net framework there is in English. I tried to fix this by installing the German language pack for .Net framework 4.5, but that didn't help. Can anyone suggest a different solution (or a working language pack for 4.6)?
Upvotes: 0
Views: 457
Reputation: 1171
Since I could not find a simpler solution, I used a *csv list that contains all countries and their names from here: https://www.laenderdaten.info/downloads/
Based on that, I created a helper class that searches through the list for a given country name.
Upvotes: 0