本土化。扩展ASP.NET RESX资源提供者 [英] Localization. Extending ASP.NET Resx Resource Provider

查看:139
本文介绍了本土化。扩展ASP.NET RESX资源提供者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关我的网站我有本地化目的的自定义资源提供者(本地化字符串存储在数据库)。它工作得很好,但我想它与默认的RESX资源提供者的工作:查找本地化的字符串中的resx资源,如果不存在,然后从数据库中拉出。

For my website I have a custom resource provider for localization purposes (localized strings are stored in database). It works just fine, but I would like it to work with the default Resx Resource Provider: look up localized string in resx resources and if doesn't exist then pull it from the database.

但看起来,一旦更改IIS设置的全球化用我自己的资源提供者的工厂,那么默认的RESX资源提供者的工厂被忽略。

But it looks that as soon as I change IIS globalization setting to use my own resource provider factory, then the default resx resource provider factory gets ignored.

我猜的解决办法是延长我自己的资源提供者,但我找不到如何从我的资源提供者的内部参考RESX资源。

I guess the solution would be to extend my own resource provider, but I can't find how to reference resx resources from inside of my resource provider.

感谢。

推荐答案

修改

下面我的答案是错误的,因为在评论中指出。你可以得到的 ResXResourceProviderFactory 的通过使用反射如下:

My answer below is wrong, as pointed out in the comments. You can get the ResXResourceProviderFactory by using reflection as follows.

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的的:

Then, to get a resource, all that's needed is to call GetObject:

object resource = p.GetObject("ResourceKey", new System.Globalization.CultureInfo("en"));


您可以使用 GetGlobalResourceObject 并在 GetLocalResourceObject 方法(部分HttpContext的的类),自定义的本地化类内的.resx文件的工作。


You can use the GetGlobalResourceObject and GetLocalResourceObject methods (part of the HttpContext class) to work with .ResX files within your custom localization classes.

例如,要获得资源被称为的ResourceKey,从MyResxFile.resx(下* * App_GlobalResources文件),对于当前的文化,你会使用这样的:

For example, to get a resource called "ResourceKey" from "MyResxFile.resx" (under *App_GlobalResources*), for the current culture, you would use this:

HttpContext.GetGlobalResourceObject(
    "MyResxFile", 
    "ResourceKey", 
    System.Threading.Thread.CurrentThread.CurrentCulture
);

这篇关于本土化。扩展ASP.NET RESX资源提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆