2013-10-24 53 views
2

我創建了一個類庫項目並添加類庫項目由一個簡單的asp.net webproject,在這裏我只是引用基於http://msdn.microsoft.com/en-us/library/ms172469(v=vs.90).aspx我們如何在.NET CustomCulture映射到一個資源文件

一個customr文化顯示基於當前文化的字符串消息。

除了messages.es-ES.resx,我還添加了一個資源文件'messages.x-en-US-sample.resx'。當我將我的用戶界面文化和文化(currentThread)設置爲x-en-US-sample時,資源字符串不會自動映射到此文件,就像我在使用時一樣。 es-ES文化可以自動選擇消息.es-ES.resx。

我在這裏失蹤了什麼?

回答

0

爲什麼你設置x-en-US文化?

設置是:使用資源文件

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

MyResourceManager.yourcountry-en-US.resx 

我覺得x-en-US不是有效的CultureInfo碼,應該是en-US

您在這裏有一個微軟的例子在那裏你可以註冊自定義文化並分配它們:

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

以下是我放在一起的一個簡單測試。只要您的資源文件具有顯示的擴展名,這將工作。

// 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(); 
    } 
+0

在我的情況下,會產生一個'System.Globalization.CultureNotFoundException',並帶有其他信息:Culture不受支持 – Sjoerd222888

相關問題