简体   繁体   English

.Net:获取德国国家名称的ISO3166代码

[英].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"). 我有2种方法:一种将给定的ISO3166代码转换为德语中相应国家的名称(例如,从“ de-DE”到“ 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. 这在我的开发机上可以正常工作,因为已安装的.Net框架是德语的。 However, on our live server (Windows Server 2012), the method returns null for all inputs. 但是,在我们的活动服务器(Windows Server 2012)上,该方法为所有输入返回null。 This happens because the installed .Net framework there is in English. 发生这种情况是因为那里安装的.Net框架是英文的。 I tried to fix this by installing the German language pack for .Net framework 4.5, but that didn't help. 我试图通过为.Net Framework 4.5安装德语包来解决此问题,但这没有帮助。 Can anyone suggest a different solution (or a working language pack for 4.6)? 谁能建议其他解决方案(或适用于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/ 由于找不到更简单的解决方案,因此我使用了* csv列表,其中包含来自以下国家/地区的所有国家及其名称: https : //www.laenderdaten.info/downloads/

Based on that, I created a helper class that searches through the list for a given country name. 基于此,我创建了一个助手类,该助手类在列表中搜索给定的国家/地区名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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