3
我的web應用程序使用ResourceManager通過放置在附屬程序集中的資源文件來處理本地化。由於應用程序正在增長,我想將resx文件拆分爲多個文件(對於每種語言),但在客戶端,似乎必須爲每個要讀取的文件創建一個ResourceManager實例。有什麼辦法可以將多個resx文件封裝到一個文件中?使用多個resx文件進行本地化
感謝
我的web應用程序使用ResourceManager通過放置在附屬程序集中的資源文件來處理本地化。由於應用程序正在增長,我想將resx文件拆分爲多個文件(對於每種語言),但在客戶端,似乎必須爲每個要讀取的文件創建一個ResourceManager實例。有什麼辦法可以將多個resx文件封裝到一個文件中?使用多個resx文件進行本地化
感謝
如果您正在使用的網站(ASP.NET網站項目模板),你可能想這樣做:HttpContext.GetGlobalResourceObject(classKey, resourceKey)
其中的ClassKey是你的.resx文件的名稱,獲取ResourceKey是在本地化字符串您資源。
如果您使用的web應用,你有你的資源作爲一個單獨的項目(C#庫),你可能想看看這個代碼:
public class SatelliteResourceManager
{
private const string Resources = "Resources";
private readonly string _assemblyName;
public SatelliteResourceManager(string assemblyName)
{
_assemblyName = assemblyName;
}
public Assembly Assembly { get { return Assembly.Load(_assemblyName); } }
protected IEnumerable<Type> ResourceTypes
{
get
{
return Assembly.GetTypes().Where(a => a.IsClass && a.Namespace == Resources);
}
}
public IEnumerable<ResourceManager> GetAllManagers()
{
foreach (var manager in ResourceTypes)
{
yield return (ResourceManager) manager.GetProperty("ResourceManager").GetValue(this, null);
}
}
public string GetGlobalResource(string classKey, string resourceKey, string fallback)
{
var manager = GetAllManagers().FirstOrDefault(m => m.BaseName.EndsWith(classKey, StringComparison.InvariantCultureIgnoreCase));
if (manager != null) return manager.GetString(resourceKey);
return fallback;
}
}
你傳遞一個需要實例的類參數與您的資源項目程序集名稱,您可以從項目屬性中獲取此信息。 我還添加了一個字段「Resources」,如果您決定覆蓋每個.resx文件的默認名稱空間,請注意您可能希望擴展此類以允許爲您的.resx文件提供多個「名稱空間」(截圖附)