如何将 .NET 自定义文化映射到资源文件

本文关键字:映射 资源 源文件 文化 自定义 NET | 更新日期: 2023-09-27 18:36:47

>我创建了一个类库项目,并添加了基于 http://msdn.microsoft.com/en-us/library/ms172469(v=vs.90) 的自定义区域性.aspx

类库项目由一个简单的 asp.net Web 项目引用,我只是在其中显示基于当前区域性的字符串消息。

除了 messages.es-ES.resx之外,我还添加了一个资源文件"messages.x-en-US-sample.resx"。当我将我的 ui 区域性和区域性 (currentThread) 设置为 x-en-US-sample 时,资源字符串不会自动映射到此文件,就像我使用 es-ES 区域性自动拾取 messages.es-ES.resx 时一样。

我在这里错过了什么?

如何将 .NET 自定义文化映射到资源文件

以下是我整理的一个简单测试。只要您的资源文件显示了扩展名,这就可以工作。

   // Resource file names:
    // XXX.resx (default)
    // XXX.de-DE.resx (German)
    // XXX.en-Hobbit.resx (custom) 
    static void Main()
    {
        BuildAndRegisterHobbitCulture();
        const string goodDay = "GoodDay";
        // Default.
        Console.WriteLine("{0}: {1}", Thread.CurrentThread.CurrentCulture.Name, 
            Resources.ResourceManager.GetString(goodDay));
        // German.
        SetCulture("de-DE");
        Console.WriteLine("{0}: {1}", Thread.CurrentThread.CurrentCulture.Name, 
            Resources.ResourceManager.GetString(goodDay));
        // Custom - Hobbit.
        SetCulture("en-Hobbit");
        Console.WriteLine("{0}: {1}", Thread.CurrentThread.CurrentCulture.Name, 
            Resources.ResourceManager.GetString(goodDay));
        Console.ReadLine();
    }
    private static void SetCulture(string culture)
    {
        var cultureInfo = CultureInfo.GetCultureInfo(culture);
        Thread.CurrentThread.CurrentCulture = cultureInfo;
        Thread.CurrentThread.CurrentUICulture = cultureInfo;
    }
    private static void BuildAndRegisterHobbitCulture()
    {
        if (CultureInfo.GetCultures(CultureTypes.AllCultures).Any(ci => ci.Name == "en-Hobbit"))
            return;
        var cultureAndRegionInfoBuilder = new CultureAndRegionInfoBuilder("en-Hobbit", CultureAndRegionModifiers.None);
        cultureAndRegionInfoBuilder.LoadDataFromCultureInfo(CultureInfo.CreateSpecificCulture("en-GB"));
        cultureAndRegionInfoBuilder.LoadDataFromRegionInfo(new RegionInfo("en-GB"));
        cultureAndRegionInfoBuilder.Register();
    }

为什么要设置x-en-US文化?

设置为:

Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");

使用资源文件:

MyResourceManager.yourcountry-en-US.resx

我认为x-en-US不是有效的文化信息代码,应该en-US

这里有一个 Microsoft 示例,您可以在其中注册自定义区域性并分配它们:

public static void Main()
   {
      // Create a custom culture for ru-US.
      CultureAndRegionInfoBuilder car1 = new CultureAndRegionInfoBuilder("ru-US", 
                                             CultureAndRegionModifiers.None);
      car1.LoadDataFromCultureInfo(CultureInfo.CreateSpecificCulture("ru-RU"));
      car1.LoadDataFromRegionInfo(new RegionInfo("en-US"));
      car1.CultureEnglishName = "Russian (United States)";
      car1.CultureNativeName = "русский (США)";
      car1.CurrencyNativeName = "Доллар (США)";
      car1.RegionNativeName = "США";
      // Register the culture. 
      try {
         car1.Register();
      }    
      catch (InvalidOperationException) {
         // Swallow the exception: the culture already is registered.
      }
      // Use the custom culture.
      CultureInfo ci = CultureInfo.CreateSpecificCulture("ru-US");
      Thread.CurrentThread.CurrentCulture = ci;
      Console.WriteLine("Current Culture: {0}", 
                        Thread.CurrentThread.CurrentCulture.Name);
      Console.WriteLine("Writing System: {0}", 
                        Thread.CurrentThread.CurrentCulture.TextInfo);
   }