对于我的网站,我有一个用于本地化目的的自定义资源提供程序(本地化字符串存储在数据库中)。它工作得很好,但我希望它与默认的 Resx 资源提供程序一起使用:在 resx 资源中查找本地化的字符串,如果不存在,则将其从数据库中提取。
但是看起来,一旦我将 IIS 全局化设置更改为使用我自己的资源提供程序工厂,默认的 resx 资源提供程序工厂就会被忽略。
我想解决方案是扩展我自己的资源提供者,但我找不到如何从我的资源提供者内部引用 resx 资源。
谢谢。
最佳答案
编辑
正如评论中所指出的,我在下面的回答是错误的。您可以使用反射获取 ResXResourceProviderFactory,如下所示。
IResourceProvider resxProvider;
string typeName = "System.Web.Compilation.ResXResourceProviderFactory, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
ResourceProviderFactory factory = (ResourceProviderFactory)Activator.CreateInstance(Type.GetType(typeName));
resxProvider = factory.CreateGlobalResourceProvider(classKey);
(获取本地资源提供者的类似方法。)
然后,要获取资源,只需调用 GetObject:
object resource = p.GetObject("ResourceKey", new System.Globalization.CultureInfo("en"));
您可以使用 GetGlobalResourceObject和 GetLocalResourceObject方法(HttpContext 类的一部分)在自定义本地化类中处理 .ResX 文件。
例如,要从“MyResxFile.resx”(在 *App_GlobalResources* 下)获取名为“ResourceKey”的资源,对于当前区域性,您可以使用:
HttpContext.GetGlobalResourceObject(
"MyResxFile",
"ResourceKey",
System.Threading.Thread.CurrentThread.CurrentCulture
);
关于c# - 本土化。扩展 ASP.NET Resx 资源提供程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11336763/