Introduction

This tutorial describes how to read text resources from satellite assemblies in ASP.NET applications. The method is not unique and other methods can be found. This particular one is based on Duwamish 7.0 sample application. Let's now start step by step

Creating the project and the main resource file

Create a new ASP.NET Web Application project and give it a name. I will name mine "ResourcesDemo". The project will contain in the root a resource file that will be the application's default resource. This resource will be part in the main assembly so any changes to this require a project recompilation. So let's add an "Assembly Resource File" from the "Add New Item" menu item. The name this file will have is important because will be used when reading the text resources. I will name my file "TextRes.resx". The file can now be edited either in its Data form or directly in Xml form. Add some strings to the file to play with. Here is how it looks my resx file data (in xml view) after I added 2 strings:

...
<dataname="EMPLOYEE"><value>Employee</value></data><dataname="DELETE_EMPLOYEE_CONFIRMATION"><value>Are you sure you want to delete this employee?
Doesn't matter it will be deleted any way...</value></data>
...

Adding the resource manager class

This is the class that will read the resources( through the ResourceManager object) and provide a static method for text resource retrieval. Let's name this class ResourceText

To locate the resource we will need first to locate the assembly that contains the resource file. A simple method for doing this is to use the Assembly static method GetExecutingAssembly to get the assembly we are in. An alternative method is to use the method Load to locate the assembly.

The first argument (baseName) in the ResourceManager constructor is the full name of the resource file including the namespace but without extension. Be very careful here: if your default namespace is something like n1.n2.n3 then the baseName must be n1.n2.n3.ResourceFileNameWithoutExtension

If the key is not found in the resource a text containing the key itself is returned to help quickly view the missing strings. For example for EMPLOYEE key the string [?:EMPLOYEE] is returned if not found in resource file. This can be change to suit your particular needs.

Adding initialization code in Global.asax.cs file

The static method InitializeResources must be called before we ask for a string to ResourceText class. So we will put a call to this method in Application_Start in Global.asax.cs file assure the initialization is done.

Now if we will run the project we will se an page with the text we put in the resource file.

The real stuff - adding more languages

Until here we sow how we can read string resources from a resource located in the main assembly of a web application. This is no doubt very useful as we can put the text from the pages in a single file and not spread in the hole application. But the real usefulness of the resources show up when multilingual applications must be made.

The first thing we must done is add another resource assembly file. This file must be named in the following format: [ResourceFileName].[language].resx so we will name our file TextRes.fr.resx for French language as an example.

Edit the file to add the same keys as in the TextRes.resx file. Here is an example of the file data (in Xml view):

Now if you look in the bin folder of your web application you will notice a folder named "fr" that contains the dll ResourcesDemo.resources.dll. This dll contains the resource file for French.

Changing the language

We added the file and now we have 2 resource files one for English - the default and one for French. To use other language then the default we must set the application thread CurrentUICulture to the CultureInfo for French language. We do this in the Application_BeginRequest in Global.asax.cs file.

Conclusion

That's all. See the demo application for this code assembled together. You will need to copy the project folder in your wwwroot and create an application in IIS. The demo application was build and tested on a Windows 2000 Professional with .Net Framework 1.0 SP2.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

i m using its demo version .. but every time when i run the application .. every time the exception throws that the resource key is not found ... in spite that the key is mentioned into the resource file .. .. how i will load the particular resource file... ???? why everu time .. "s" variable got the null value ...