What Are the Benefits of Resource(.Resx) Files

What are the benefits of resource(.resx) files?

  • Resource files give you an easy way to localize/internationalize your .net applications by automatically determining which language resx file to use based on the user's locale. To add more languages, simply add another translated resource file.

  • Resource files give you a central location to store your strings, files and scripts and refer to them in a strongly-typed manner (so the compile will break if you reference them improperly).

  • Resource files can be compiled into satellite assemblies, making it easy to change up the resources in a production application without having to recompile the whole thing.

What advantages are the of using .resx localization for an ASP.NET MVC app?

There are couple of advantages to the RESX infrastructure:

  • you don't have to load the proper per-language resources. Once the locale of the thread is established, the CLr takes care of finding the appropriate assembly and loading the resources.
  • it is easy to hand off the locale-specific resources for localizations to third-parties.
  • there is a default fallback mechanism for non-localized resources.

There is also one particular disadvantage to the RESX approach:

  • it is hard to support translation model where the users translate your resources for you.

I'd like to elaborate a bit about that last point. Take for example the Facebook translation model. Facebook has fairly simple way for people to provide and vote on translations of various resources. If these are stored in a database, it would be possible to use them after the proper editorial process without rebuilding and redeploying the application. With the RESX model, the resources assemblies will have to be rebuild and redeployed, which could have high enough cost depending on the deployment process.

Thus, before deciding what localization process to use, I would look at the decision of who is going to do the localization and what the deployment process for the localizaed resources would be after the main application is already deployed.

EDIT: I forgot to mention that these considerations are orthogonal to the ASP.NET framework choice (MVC or WebForms).

Performance Overheads when Using Resource Files (.resx)

String resources are cached in memory. Look at the code that's generated in "Resources.Designer.cs".

It uses a System.Resources.ResourceManager, and this does caching of the strings.

Also note this ResourceManager constructor. It mentions that you can change caching strategy:

This constructor uses the system-provided ResourceSet implementation.
To use a custom resource file format, you should derive from the
ResourceSet class, override the GetDefaultReader and GetDefaultWriter
methods, and pass that type to the ResourceManager(String, Assembly,
Type) constructor. Using a custom ResourceSet can be useful for
controlling resource caching policy
or supporting your own resource
file format, but is generally not necessary.

(my emphasis)

The documentation for ResourceSet explicitly says:

The ResourceSet class enumerates over an IResourceReader, loading every name and value, and storing them in a Hashtable

So we do know the exact caching strategy that you'll get by default.

[EDIT] Since you don't seem to believe me! :)

(1) Look at the documentation for the constructor ResourceManager(string baseName,Assembly assembly). It states:

This constructor uses the system-provided ResourceSet implementation.

(2) Now look at the documentation for ResourceSet. It states:

The ResourceSet class enumerates over an IResourceReader, loading every name and value, and storing them in a Hashtable.

Therefore this caching behaviour is indeed documented in MSDN, and additionally you can verify that this is what is happening by using Resharper to inspect the implementation.

Are resx files necessary?

Yes, you can delete the .resx files, BUT make sure that your pages are already setup to work without the .resx files. By this I mean that your labels have a text value and it does not require the .resx file to supply it.

Furthermore, if the files are already there, they don't cause all that many problems, I'd leave them.

Are resx files a suitable way to customise for different customers?

You can use them for storing different strings per customer - it will work. I imagine you don't have many customer to cater for and they do not require the strings to be modified very often. Otherwise the management of multiple files may become a real pain.

Storing per-customer strings in the database (with caching) may be a better option, especially if you want the customers to modify their strings.



Related Topics



Leave a reply



Submit