Localization. Extending ASP.NET Resx Resource Provider

梦想与她 提交于 2019-12-04 11:27:09

Edit

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);

(Similar method to get the Local resources provider.)

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

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

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

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
);

Okay, it looks that extending custom resource provider with the default resX resource provider doesn't fully solve the issue, since implicit localization expressions (meta:resourcekey) don't get localized.

The possible solution I found here is to use a custom ResourceExpressionBuilder:

Configuring a custom provider is great for situations in which all resources will be stored in an alternate location, and you don't plan to leverage resources located in App_LocalResources and App_GlobalResources, respectively. What if you want to support the standard implementation for local and global resources (default provider), while also having the option to pull some resources from another source (custom provider)? You can achieve this by implementing custom expressions that target the custom resource provider.

This will allow using resX resource provider for implicit and explicit localization and custom expressions for your custom resource provider:

<%-- Local ResX --%>
<asp:Localize ID="locLocal" runat="server" Text="DefaultLocal" meta:resourcekey="locLocal" />
<%-- Global ResX --%>
<asp:Localize ID="locGlobal" runat="server" Text="<%$ Resources:GlobalResourceStrings, locGlobal %>" />
<%-- Custom Resource Provider --%>
<asp:Localize ID="locCust" runat="server" Text="<%$ ExternalResources:MyResources|CustomResourceStrings, locCust %>" meta:localize="false" />

or in code as:

string s = (string)ExternalResourceExpressionBuilder.GetGlobalResourceObject("MyResources|CustomResourceStrings", "locCust");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!