|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis 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 fileCreate 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 classThis is the class that will read the resources( through the using System;
using System.Resources;
using System.Reflection;
namespace ResourcesDemo
{
/// <summary>
/// Class for resource retrieval
/// </summary>
public class ResourceText
{
private ResourceText()
{}
private static ResourceManager _resourceManager;
}
}
The constructor of Let's add now code for public static void InitializeResources()
{
Assembly resourceAssembly = Assembly.GetExecutingAssembly();
_resourceManager = new ResourceManager(
"ResourcesDemo.TextRes", resourceAssembly);
_resourceManager.IgnoreCase = true;
//This is my preference. You can change this...
}
This method will be called from Global.asax 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 The first argument ( 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 Adding initialization code in Global.asax.cs fileThe static method protected void Application_Start(Object sender, EventArgs e)
{
ResourceText.InitializeResources();
}
Testing what we've doneAdd a Web Form to the project and add a label in it <asp:Label id=Label1 runat="server"></asp:Label>
On 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 languagesUntil 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 languageWe 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 threadCurrentUICulture 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. ConclusionThat'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.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||