Implicit localization, strongly typed resources, App_LocalResources, and embedded resources

tl; dr Does strict code generation of the resource with normal (not built-in) resources in App_LocalResources ?

If not, why, and does the alternative of using embedded resources in satellite assemblies with implicit localization work?

The rest of this post simply explains where I am currently resolving these issues, feel free to ignore it if you know the answers.


When using implicit localization (syntax meta:resourceKey="Foo" ), I understand that I would have to write my own resource provider if you wanted to embed resources in satellite assemblies. The reason is that ASP.NET always uses the default provider for this and that this provider expects resx files in App_LocalResources , which can be obtained at run time. Also see this question , which has no answer at the time of this writing.

If this assumption is true, then it is not possible to use strongly typed generated classes (using ResXFileCodeGenerator ) without writing such a provider (which we would like to avoid), since the use of built-in resources is required to ensure code generation.

Since using generated types works fine for global resources, I want to ask a question about the second assumption:

  • If I can generate strongly typed classes for global resources (in App_GlobalResources using GlobalResourceProxyGenerator ) without embedding them in a satellite assembly ( Build Action set to Content as opposed to Embedded ), then Why can't I do the same for local resources? Why can't the generated code find and use resx files in App_LocalResources ?

Note: The exception that was thrown while trying to do this is a System.Resources.MissingManifestResourceException containing the following message:

Could not find resources suitable for the specified culture or neutral culture. Make sure "PROJECT.App_LocalResources.PAGE.aspx.resources" was correctly built-in or connected to the "PROJECT" assembly at compile time or that all necessary satellite assemblies are required to be downloadable and fully signed.

I know this message is misleading, as it explicitly searches for satellite assemblies instead of trying resx files (or that they compile at run time, App_LocalResources.dll , I think).

  • If there is a good reason why this is unacceptable (and therefore we have to use built-in resources in satellite assemblies), is there a good implementation of a resource provider that can look for resources in satellite assemblies when performing implicit localization? Of course, someone tried to do this before, and this does not seem to work as an application developer to solve this problem with plumbing.

  • As an additional question to the previous one, I also assume that when using the built-in resources in satellite assemblies, it would be impossible to put resx files in the App_* directories, since these are special directories used at runtime. Indeed, resx files are resx even deployed, so directories will be empty. Is this correct, and is there something that will pass as best practices in this regard?


I suppose another way to ResXFileCodeGenerator question is: can I make ResXFileCodeGenerator behave like GlobalResourceProxyGenerator when it comes to creating code that can load assemblies compiled by the runtime, as opposed to satellite collections compiled during assembly?

+7
webforms localization embedded-resource
source share
1 answer

Embedded resources can coexist with ASP.NET resource provider resources, which are located in the App_LocalResources / App_GlobalResources folder. But all the built-in localization functions of WebForms work only with the resources provided by the ASP.NET resource provider, which means that the default resources come only from the App_ folders and not from the built-in resources.

Nested strongly typed resources do not use the ASP.NET resource provider — they use the .NET stock resource manager, and when you use them, you lose some of the optimizations that the ASP.NET ResourceProvider system uses for caching and loading resources. It is more efficient in ASP.NET scripting.

As you rightly point out, this can be done by creating a specialized resource provider that reads embedded resources (or resources from another source, such as a database), but you need to create this resource provider and connect it. I wrote about this in an article a while ago (using the SQL database provider): http://www.west-wind.com/presentations/wwDbResourceProvider/

I would not recommend mixing the resources of the App_ folder from the resource provider with strongly typed resources, because you will have two different sets of resources loaded using different mechanisms. It works and can be executed, but it's just not very controversial. Choose one approach or another. For Web Forms, the resource provider model works better simply because it is the only way to use implicit resources.

Note that ASP.NET MVC usually does not use the ASP.NET resource provider (although it can) and rather relies on highly typed resources embedded in the code. If your WebForms code is mostly script based, then using embedded resources may work well, but if you need to bind management properties, a resource provider is the only way to go.

+1
source share

All Articles