Click here to Skip to main content
15,884,628 members
Articles / Web Development / ASP.NET

Custom Resource Provider

Rate me:
Please Sign up or sign in to vote.
4.64/5 (16 votes)
5 Mar 2009Public Domain2 min read 44.6K   1K   23   2
Custom resource provider to facilitate fetching global resources using HTML markups
Click to enlarge image

Introduction

.NET provides a very easy way to set resources for your labels in HTML markup itself (as show above) using classes like ResourceProviderFactory and IResourceProvider. This article illustrates a simple example to implement this using ASP.NET & C# (solution attached is created in Visual Studio 2008).

Background

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 ResourceProviderFactory and IResourceProvider interface. 

Using the Code

code2.jpg

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:

XML
<globalization uiCulture="auto"
culture="auto" resourceProviderFactoryType=
" CustomeResourceProviders.
ExternalResourceProviderFactory,
CustomeResourceProviders,
Version=1.0.0.0 , Culture=neutral"/>

Another important thing to note here is the overridden method InitializeCulture().

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.

History

  • 5th March, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Software Developer (Senior) Geometric
India India
B.E. in Information Technology
MCTS(.NET 2.0 )

Comments and Discussions

 
GeneralProblem with resource files in subfolders in this example Pin
tejo8115-Jul-09 2:18
tejo8115-Jul-09 2:18 
GeneralNice work! Pin
Matrix6-Mar-09 20:06
Matrix6-Mar-09 20:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.