0

我有两种方法:一种将给定的 ISO3166 代码转换为相应国家的德语名称(例如“de-DE”到“Deutschland”)。这工作正常。现在我需要一种相反的方法。目前它看起来像这样:

  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;
    }

这在我的开发机器上运行良好,因为安装的 .Net 框架是德语的。但是,在我们的实时服务器 (Windows Server 2012) 上,该方法为所有输入返回 null。发生这种情况是因为安装的 .Net 框架是英文的。我试图通过安装 .Net framework 4.5 的德语语言包来解决这个问题,但这并没有帮助。任何人都可以提出不同的解决方案(或 4.6 的工作语言包)吗?

4

1 回答 1

0

由于我找不到更简单的解决方案,因此我使用了一个 *csv 列表,其中包含来自此处的所有国家及其名称:https ://www.laenderdaten.info/downloads/

基于此,我创建了一个帮助程序类,它在列表中搜索给定的国家名称。

于 2016-07-12T11:50:54.647 回答