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:
...
<data name="EMPLOYEE">
<value>Employee</value>
</data>
<data name="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
using System;
using System.Resources;
using System.Reflection;
namespace ResourcesDemo
{
public class ResourceText
{
private ResourceText()
{}
private static ResourceManager _resourceManager;
}
}
The constructor of ResourceText
class is marked as private because only it's static methods are used.
Let's add now code for ResourceManager
creation
public static void InitializeResources()
{
Assembly resourceAssembly = Assembly.GetExecutingAssembly();
_resourceManager = new ResourceManager(
"ResourcesDemo.TextRes", resourceAssembly);
_resourceManager.IgnoreCase = true;
}
This method will be called from Global.asax Application_Start
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
Let's now add the main method of this class for text retrieval
public static string GetString(string key)
{
try
{
string s = _resourceManager.GetString( key );
if( null == s ) throw(new Exception());
return s;
}
catch
{
return String.Format("[?:{0}]", key);
}
}
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.
protected void Application_Start(Object sender, EventArgs e)
{
ResourceText.InitializeResources();
}
Testing what we've done
Add a Web Form to the project and add a label in it
<asp:Label id=Label1 runat="server"></asp:Label>
On PageLoad
event of the web form add the following line:
private void Page_Load(object sender, System.EventArgs e)
{
Label1.Text = ResourceText.GetString(
"DELETE_EMPLOYEE_CONFIRMATION");
}
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):
...
<data name="EMPLOYEE">
<value>Employ�</value>
</data>
<data name="DELETE_EMPLOYEE_CONFIRMATION">
<value>�tes-vous s�r que vous voulez supprimer cet employ� ?
N'importe, pas il sera quand m�me supprim�...</value>
</data>
...
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.
using System.Threading;
using System.Globalization;
...
private void Page_Load(object sender, System.EventArgs e)
{
Thread.CurrentThread.CurrentUICulture =
CultureInfo.CreateSpecificCulture("fr-FR");
}
Run the application and voil� the text in French appear on the page.
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.