Localizing Web Applications with ASP.NET 2.0
page 1 of 2
Published: 23 Jun 2006
Unedited - Community Contributed
Abstract
In this article, Pradeep demonstrates how to localize web applications with ASP.NET 2.0.
by Pradeep Shukla
Feedback
Average Rating: 
Views (Total / Last 10 Days): 18492/ 9
Article Contents:

Introduction

Auto-culture handling can be enabled for each page in ASP.NET 2.0 by including the Culture="auto" and UICulture="auto" attributes in the Page directive of each page. UICulture can be considered as the physical UI, fonts, colors etc. Culture affects formatting, for example date formats,numeric formats. Once enabled, the ASP.NET runtime will automatically map Accept-Language headers to CultureInfo objects and attach them to the current thread. As a developer we need to concentrate on creating the resource file for different cultures that need to be supported in the web Application.ASP.NET will then automatically load the appropriate resources for each culture.

The Resource files can be created by right clicking on the Project->Add New Item->Resource in the Solution Explorer. The extension names for the Resource file is .resx. While creation we need to follow the naming conventions. For example, if the aspx page name is “form1.aspx” then the English resource file for the page should be “form1.aspx.en.resx” where en is the keyword for English culture. Similarly, for French the resource file name should be “form1.aspx.fr.resx”.

Note: There should be one default resource file for each page. In case ASP.NET does not find the appropriate resource for the current culture then the default resource file will be automatically loaded. The naming convention for the default resource file should be in “pagename.aspx.resx” format. For the above example the default resource file name should be “form1.aspx.resx”.

Figure 1

In ASP.NET 2.0 the local resource files is placed inside the App_LocalResources folder.

Figure 2

The resource files can be edited by double clicking on the resource file name in the solution explorer (Please see Resource Editor).Every resource file contains three fields Name, Value and Comment. Here I will try to explain it with an example so that it will be easier for the user to understand.

Suppose in your web page say “form1.aspx” you have a label control id=label1 and the user wants to make its text property to be supported for english and french language (“Welcome” for english and  “Accueil” for french)

In the form1.aspx.en.resx file give a name say “test” and value as "Welcome"

Name              Value              Comment

test              Welcome

Similarly in the form1.aspx.fr.resx file

Name              Value              Comment

test              Accueil         

Here you must have noticed that “test” is common for both the resource files. This is because in the ASP.NET page the Name field will be called for loading the value to the control. The resource file can be called both at design and at runtime.

In design time the resource file is called by adding the following piece of code in the text property of the control.

Listing 1

<%$ Resources:<ResourceName>%>  

For the above example, the HTML tag for the Label control will look like

Listing 2

<asp:Label ID="label1" Runat=Server Text="<%$ Resources:test%>"> </asp:Label>

At runtime the resource file is called by using API's function. GetLocalResourceObject is the function for this purpose.

Listing 3

Me.label1.Text =Convert.ToString(GetLocalResourceObject("test"))

For this we have to use the following name spaces

Listing 4

Imports System.Threading
Imports System.Resources 
Imports System.Globalization 

So make sure to include them in your page.

Depending upon the culture set in the application ASP.NET will look for the resource name in the resource files. Here if the culture is French, ASP.NET will look for test resource name in form1.aspx.fr.resx file and will load its value to the appropriate property of the control.

Now to test this, change the default culture on your browser.

On IE 6 this can be done by going to

Tools -> Internet Options -> Languages

Click on Add to add the French language to the language list and then move the French language to the top of the list by selecting the language and clicking the MoveUp button. Now the default culture of your browser is set to French.

Figure 3

Figure 4

Reload the Page; you will see that the Web Server has served the French resource in the label control.


View Entire Article

User Comments

Title: resource not found   
Name: maxima
Date: 2010-06-29 5:40:41 PM
Comment:
I did exactly as you said and at compile time I get the following error message:"The resource object with key "test" was not found"
Title: Getting error   
Name: Mubi
Date: 2010-01-22 4:15:10 PM
Comment:
\S
\
\
\
\
\S
\
\
\
\
Title: good job   
Name: guest
Date: 2007-01-25 5:28:29 AM
Comment:
I will say just tnx man :)
Title: Nice Article   
Name: Guest
Date: 2006-11-27 5:40:15 AM
Comment:
Its a very good article.simple and easy to understand.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-17 8:15:53 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search