简体   繁体   中英

.Net: get ISO3166 code for German country name

I got 2 methods: one that converts a given ISO3166 code to the corresponding country's name in German (eg "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)?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM