【问题标题】:Get Region info of Moldova获取摩尔多瓦的地区信息
【发布时间】:2013-11-05 10:13:37
【问题描述】:

我正在尝试通过 ISO 3166-1 TwoLetters 国家名称 - “MD”获取区域信息。

var r = new RegionInfo("MD");

但我得到以下异常:

不支持文化名称“MD”。

这很奇怪,因为微软支持的国家摩尔多瓦表是存在的:

http://msdn.microsoft.com/en-us/library/dd374073.aspx

【问题讨论】:

    标签: c# .net globalization


    【解决方案1】:

    根据MSDN documentation on RegionInfo关于文化名称:

    预定义的文化名称列在 Go Global Developer Center 的国家语言支持 (NLS) API 参考中。

    当你转到National Language Support (NLS) API Reference 时,MD 在那里找不到。

    【讨论】:

      【解决方案2】:

      你可以create your own culture info

      以管理员身份运行 Visual Studio。在您的项目中,添加对 sysglobl 的引用。

      using System;
      using System.IO;
      using System.Globalization;
      using System.Linq;
      using System.Xml.Linq;
      
      class Program
      {
          public static void Main()
          {
              CultureAndRegionInfoBuilder cib = null;
              try
              {
                  // Create a CultureAndRegionInfoBuilder 
                  // object named "ro-MD".
                  cib = new CultureAndRegionInfoBuilder(
                                          "ro-MD", CultureAndRegionModifiers.None);
      
                  // Populate the new CultureAndRegionInfoBuilder 
                  // object with culture information.
                  CultureInfo ci = new CultureInfo("ro-RO");
                  cib.LoadDataFromCultureInfo(ci);
      
                  // Populate the new CultureAndRegionInfoBuilder 
                  // object with region information.
                  RegionInfo ri = new RegionInfo("RO");
                  cib.LoadDataFromRegionInfo(ri);
      
                  var filePath = "ro-MD.xml";
      
                  if (File.Exists(filePath))
                      File.Delete(filePath);
      
                  // Save as XML
                  cib.Save(filePath);
      
                  // TODO: modify the XML
                  var xDoc = XDocument.Load(filePath);
                  var ns = 
                      "http://schemas.microsoft.com/globalization/2004/08/carib/ldml";
      
                  xDoc.Descendants(XName.Get("regionEnglishName", ns))
                      .FirstOrDefault().Attribute("type").SetValue("Moldova");
      
                  xDoc.Descendants(XName.Get("regionNativeName", ns))
                      .FirstOrDefault().Attribute("type").SetValue("Moldova");
      
                  // and so on
                  xDoc.Save(filePath);
      
                  var roMd = CultureAndRegionInfoBuilder
                      .CreateFromLdml(filePath);
      
                  // this may throw an exception if the culture info exists 
                  try
                  {
                      CultureAndRegionInfoBuilder.Unregister("ro-MD");
                  }
                  catch (Exception)
                  {
                      //throw;
                  }
      
                  // Register the custom culture.
                  roMd.Register();
      
                  // Display some of the properties of the custom culture.
                  var riMd = new RegionInfo("ro-MD");
              }
              catch (Exception e)
              {
                  Console.WriteLine(e);
              }
          }
      }
      

      您只需要处理 XML 修改。

      注意:您似乎也可以在没有管理权限的情况下保存文化。这是一个参考:How to: Save Custom Cultures Without Administrative Privileges。我自己没有测试过,但对该文章的唯一评论似乎表明它不起作用。

      [更新]

      这也是一种有趣的方法(调用原生方法的包装器):

      Get the full list of Windows supported countries with C#.

      【讨论】:

        【解决方案3】:

        System.Globalization.RegionInfo 使用文化数据,如果该地区没有文化数据,则不会成功。这也是为什么为该地区的语言创建自定义文化会使其成功的原因。

        您可能想要的是使用支持所有当前 ISO-3166 国家/地区的新 Windows.Globalization.GeographicRegion

        或者您可以 p/Invoke 到 GetGeoInfo 和 EnumSystemGeoId,因为它们支持摩尔多瓦,如您上面参考的文档中所示。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-08-09
          • 1970-01-01
          • 1970-01-01
          • 2016-11-21
          • 1970-01-01
          • 2017-04-02
          • 2017-12-19
          • 1970-01-01
          相关资源
          最近更新 更多