2010-08-04 80 views
2

對於我的翻譯,我正在使用嵌入式.resx文件。其中一些翻譯我希望用另一個未嵌入的.resx文件覆蓋(例如〜/ App_Localresources/translations.en-US.resx)。ASP.NET Resourcemanager讀取本地.resx文件

這樣做的目的是,在編譯和部署應用程序後,用戶可以手動更改.resx文件以覆蓋一些嵌入式翻譯。

有沒有辦法使用普通的ResourceManager呢? (在.NET 4)

謝謝你的提示

回答

3
public class ResxResourceManager : System.Resources.ResourceManager { 
     public ResxResourceManager(string baseName, string resourceDir) { 
      Type[] paramTypes = new Type[] { typeof(string), typeof(string), typeof(Type) }; 
      object[] paramValues = new object[] { baseName, resourceDir, typeof(ResXResourceSet) }; 

      Type baseType = GetType().BaseType; 

      ConstructorInfo ci = baseType.GetConstructor(
       BindingFlags.Instance | BindingFlags.NonPublic, 
       null, paramTypes, null); 

      ci.Invoke(this, paramValues); 
     } 

    protected override string GetResourceFileName(CultureInfo culture) { 
     string resourceFileName = base.GetResourceFileName(culture); 
     return resourceFileName.Replace(".resources", ".resx"); 
    } 
} 
+5

你如何使用它?你能給個例子嗎? – Jaguar 2011-01-28 13:04:52

+0

謝謝,我正在尋找這一段時間。使用示例'ResxResourceManager rm = new ResxResourceManager(「NameOfResourceFile」,「PathToResourceDir」); rm.GetString(name,System.Globalization.CultureInfo.GetCultureInfo(「en-US」));' – tsukimi 2012-11-06 08:47:20

+0

此代碼看起來非常危險。它使用refelction來調用第二個基礎構造函數。這個構造函數是公開的(自.NET 2開始:也許它不在.NET 1或1.1中)。 – Richard 2014-07-04 14:56:45