Click here to Skip to main content
Click here to Skip to main content

Custom Resource Provider

, 5 Mar 2009 Public Domain
Rate this:
Please Sign up or sign in to vote.
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:

 <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

Share

About the Author

sunit_82
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 Pinmembertejo8115-Jul-09 3:18 
Nice that you made an example that works from this article: http://msdn.microsoft.com/en-us/library/aa905797.aspx[^].
 
I used your example but found that it does not work when you put your resourcefiles in subfolders, that is because of this code:
 
        private void EnsureResourceManager()
        {
            if (this.m_resourceManager == null)
            {
                lock (this)
                {
                    Assembly asm = Assembly.Load(this.m_assemblyName);
                    ResourceManager rm = new ResourceManager(String.Format(CultureInfo.InvariantCulture, "{0}.{1}", this.m_assemblyName, this.m_classKey, asm);
                    this.m_resourceManager = rm;
                }
            }
        }
 
assemblyName.classKey is not always enough, it could also be assemblyName.subFolder.classKey
 
It is possible to use this to get the correct baseName:
 
        private string GetBaseName(Assembly assembly)
        {
            var resourceNames = assembly.GetManifestResourceNames();
 
            foreach (string name in resourceNames)
                if(name.Contains(String.Format(CultureInfo.InvariantCulture, ".{0}.{1}", this.m_classKey, "resources")))
                    return name.Replace(".resources",String.Empty);
 
            return String.Empty;
        }
 
Here you check which resourcefiles are in the assembly and you return the correct baseName including assemblyName and possible subFolders
GeneralNice work! PinmemberMatrix6-Mar-09 21:06 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 5 Mar 2009
Article Copyright 2009 by sunit_82
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid