2009-08-20 137 views
9

我想在App_GlobalResources文件夾中爲名爲「shopping.en-sg.resx」的新加坡英語(en-sg)創建資源文件。在ASP.NET中創建自定義區域

編譯過程中出現錯誤。

錯誤1命名空間 '資源' 已經包含了定義 '購物' C:\ WINDOWS \ Microsoft.NET \框架\ V2.0.50727 \臨時 ASP.NET 文件\網絡\ 2cd6afe9 \ 737b0a13 \ App_GlobalResources.vomuzavz.1.cs 26

谷歌搜索後,我發現「EN-SG」不是默認的文化,我要創建自定義的文化吧。我不知道這個的詳細步驟。

我應該怎麼做才能創建文化並消除編譯錯誤?

我按照MSDN的例子中,創建一個名爲 「shopping.x-EN-US-sample.resx」 文件,把下面的代碼到的BasePage的功能(保護覆蓋無效InitializeCulture()):

CultureAndRegionInfoBuilder cib = null; 

cib = new CultureAndRegionInfoBuilder(
    "x-en-US-sample", CultureAndRegionModifiers.None); 

CultureInfo ci = new CultureInfo("en-US"); 
cib.LoadDataFromCultureInfo(ci); 
RegionInfo ri = new RegionInfo("US"); 
cib.LoadDataFromRegionInfo(ri); 

cib.Register(); 

ci = new CultureInfo("x-en-US-sample"); 

但是,編譯錯誤仍然存​​在。

更新:

,您可以輕鬆創建一個空的網站,兩個文件「shopping.en-sg.resx」,並在App_GlobalResources文件夾「shopping.resx」重現該問題。

回答

15

您可以創建基於現有的文化一種新的文化:

string culture = "en-sg"; 
string name = "Singaporean English"; 

CultureInfo cultureInfo = new CultureInfo("en-GB"); 
RegionInfo regionInfo = new RegionInfo(cultureInfo.Name); 

CultureAndRegionInfoBuilder cultureAndRegionInfoBuilder = new CultureAndRegionInfoBuilder(culture, CultureAndRegionModifiers.None); 

cultureAndRegionInfoBuilder.LoadDataFromCultureInfo(cultureInfo); 
cultureAndRegionInfoBuilder.LoadDataFromRegionInfo(regionInfo); 

// Custom Changes 
cultureAndRegionInfoBuilder.CultureEnglishName = name; 
cultureAndRegionInfoBuilder.CultureNativeName = name; 

cultureAndRegionInfoBuilder.Register(); 

補充: 剛纔檢查的參考文獻: 我:

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Globalization; 
using System.IO; 
using System.Reflection; 
using System.Runtime.CompilerServices; 

已添加(更新的基礎上,評論):

至於錯誤消息:

你看到的錯誤是某些資源命名衝突的結果。檢查資源名稱,這些被編譯到dll中,你需要檢查命名空間名稱是否不衝突。您可以使用反射器工具檢查:http://www.red-gate.com/products/reflector/

+0

我應該在哪裏放置這段代碼以避免錯誤? – Billy 2009-08-20 07:43:28

+0

此代碼只需要在需要額外文化的機器上運行一次。因爲它已經存在,也許你會得到錯誤? – 2009-08-20 07:46:05

+0

把代碼放在global.asax中? 如果我將「en-sg」更改爲「en-us」,那很好。 我只想要一段代碼,我可以簡單地把它放在某個地方,然後我可以使用shopping.en-sg.resx – Billy 2009-08-20 07:59:44

1
+0

我試過了,它不起作用。更多信息。被添加。 – Billy 2009-08-20 07:33:40

+1

修復鏈接:http://msdn.microsoft.com/en-us/library/ms172469(v=vs.100).aspx – Sprintstar 2014-05-12 09:03:25

+0

謝謝@Sprintstar:我更新了我的帖子中的鏈接。 – 2014-05-12 14:15:18

3

以下是創建en-sg文化所需的步驟和代碼。

  1. 創建控制檯應用程序。 (C:\ Windows \ Microsoft.NET \ Framework \ v2.0.50727 \ sysglobl.dll)
  2. 添加對sysglobl(C:
  3. 以管理員身份在Web服務器和開發機器上運行它。

它會根據我發現的最接近匹配(en-au)創建一種文化。然後我重寫名字等,使它獨一無二。

你應該只需要運行一次。在創建它之前,它會刪除所有現有的,以防您在運行後進行任何修改。

public static void Main() 
    { 
     CultureAndRegionInfoBuilder cib = null; 

     try 
     { 
      Console.Clear(); 
      Console.WriteLine("Unregister the \"en-SG\" " + "custom culture if it already exists..."); 
      CultureAndRegionInfoBuilder.Unregister("en-SG"); 
      Console.WriteLine("The custom culture was unregistered successfully."); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine("Error while unregistering..."); 
      Console.WriteLine(e); 
     } 

     try 
     { 
      cib = new CultureAndRegionInfoBuilder("en-SG", CultureAndRegionModifiers.None); 

      // Populate the new CultureAndRegionInfoBuilder object with culture information. 
      CultureInfo ci = new CultureInfo("en-AU"); 
      cib.LoadDataFromCultureInfo(ci); 

      // Populate the new CultureAndRegionInfoBuilder object with region information. 
      RegionInfo ri = new RegionInfo("SG"); 
      cib.LoadDataFromRegionInfo(ri); 

      cib.CultureEnglishName = "English (Singapore)"; 
      cib.CultureNativeName = "English (Singapore)"; 
      cib.IsMetric = true; 

      // Display some of the properties of the CultureAndRegionInfoBuilder object. 
      Console.WriteLine("CultureName:. . . . . . . . . . {0}", cib.CultureName); 
      Console.WriteLine("CultureEnglishName: . . . . . . {0}", cib.CultureEnglishName); 
      Console.WriteLine("CultureNativeName:. . . . . . . {0}", cib.CultureNativeName); 
      Console.WriteLine("GeoId:. . . . . . . . . . . . . {0}", cib.GeoId); 
      Console.WriteLine("IsMetric: . . . . . . . . . . . {0}", cib.IsMetric); 
      Console.WriteLine("ISOCurrencySymbol:. . . . . . . {0}", cib.ISOCurrencySymbol); 
      Console.WriteLine("RegionEnglishName:. . . . . . . {0}", cib.RegionEnglishName); 
      Console.WriteLine("RegionName: . . . . . . . . . . {0}", cib.RegionName); 
      Console.WriteLine("RegionNativeName: . . . . . . . {0}", cib.RegionNativeName); 
      Console.WriteLine("ThreeLetterISOLanguageName: . . {0}", cib.ThreeLetterISOLanguageName); 
      Console.WriteLine("ThreeLetterISORegionName: . . . {0}", cib.ThreeLetterISORegionName); 
      Console.WriteLine("ThreeLetterWindowsLanguageName: {0}", cib.ThreeLetterWindowsLanguageName); 
      Console.WriteLine("ThreeLetterWindowsRegionName: . {0}", cib.ThreeLetterWindowsRegionName); 
      Console.WriteLine("TwoLetterISOLanguageName: . . . {0}", cib.TwoLetterISOLanguageName); 
      Console.WriteLine("TwoLetterISORegionName: . . . . {0}", cib.TwoLetterISORegionName); 
      Console.WriteLine(); 

      // Register the custom culture. 
      Console.WriteLine("Register the custom culture..."); 
      cib.Register(); 

      // Display some of the properties of the custom culture. 
      ci = new CultureInfo("en-SG"); 

      Console.WriteLine("Name: . . . . . . . . . . . . . {0}", ci.Name); 
      Console.WriteLine("EnglishName:. . . . . . . . . . {0}", ci.EnglishName); 
      Console.WriteLine("NativeName: . . . . . . . . . . {0}", ci.NativeName); 
      Console.WriteLine("TwoLetterISOLanguageName: . . . {0}", ci.TwoLetterISOLanguageName); 
      Console.WriteLine("ThreeLetterISOLanguageName: . . {0}", ci.ThreeLetterISOLanguageName); 
      Console.WriteLine("ThreeLetterWindowsLanguageName: {0}", ci.ThreeLetterWindowsLanguageName); 

     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e); 
     } 
     Console.ReadKey(); 
    } 
相關問題