Localization. ASP.NET Resx Resource Provider Extension

For my site, I have a custom resource provider for localization purposes (localized strings are stored in the database). It works fine, but I would like it to work with the default Resx resource provider: find the localized string in the resx resources, and if not, pull it out of the database.

But it seems that as soon as I change the IIS global integration parameter to use my own factory resource provider, then the default factory resx resource provider is ignored.

I assume that the solution will be to expand my own resource provider, but I cannot find how to reference resx resources from within my resource provider.

Thanks.

+4
c # localization globalization
source share
2 answers

Edit

My answer below is incorrect as indicated in the comments. You can get a ResXResourceProviderFactory 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); 

(A similar method to get the local resource provider.)

Then, to get the resource, you just need to call GetObject:

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

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

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

 HttpContext.GetGlobalResourceObject( "MyResxFile", "ResourceKey", System.Threading.Thread.CurrentThread.CurrentCulture ); 
+3
source share

Well, it looks like the extension of the user resource provider with the resX resource provider by default does not completely solve the problem, since the implicit localization expressions ( meta:resourcekey ) are not localized.

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

Configuring a custom provider is great for situations where all resources will be stored elsewhere, and you do not plan to use the resources located in App_LocalResources and App_GlobalResources, respectively. What if you want to support the standard implementation for local and global resources (the default provider), as well as the ability to derive some resources from another source (user provider)? You can achieve this by implementing custom expressions targeted at a custom resource provider.

This will allow you to use the 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 like:

 string s = (string)ExternalResourceExpressionBuilder.GetGlobalResourceObject("MyResources|CustomResourceStrings", "locCust"); 
0
source share

All Articles