.NET provides a very easy way to set resources for your labels in HTML markup itself (as show above) using classes like
IResourceProvider. This article illustrates a simple example to implement this using ASP.NET & C# (solution attached is created in Visual Studio 2008).
Remember how in .NET 1.1 Resgen tool was used to generate ".resources" file from simple text file and loading the culture specific .resources file in the
ResourceManger instance? Well now we have this cool way of reading culture specific resources from resource files for our multilingual applications. This is enabled using class
Using the Code
In the solution, we have Web client that consumes the
CustomResoureProvider library. And we have a library project "
CommonResources" that holds all the culture specific resource files.
In the Default.aspx page, we have a dropdown to select different languages. On selecting different languages, the culture specific resource file to selected language is loaded by the
CustomResourceProvider and corresponding resources are rendered.
Web client is informed of the
CustomeResourceProvider by specifying globalization tag in the web.config as:
Version=220.127.116.11 , Culture=neutral"/>
Another important thing to note here is the overridden method
You can always add more resource files for supporting more languages in your multilingual applications. The advantage of this way of reading resources is, to add a new language support to a web application we don't have to make any code changes. All we have to do is add a Resource file with proper culture specific name and have all the resources translated to the new language in this file and you are ready to go.
Points of Interest
One thing that bugged me while doing this is that when the resource files in the "
CommonResources" project (in attached solution) were having the name "Resources.resx", the English resources were getting read fine. But on changing the language, culture specific resources were not getting read but English resources were getting read. I'm guessing that might have something to do with "
Resources" being a keyword. Any one having more information in this regard, please do post comments.
- 5th March, 2009: Initial post