Click here to Skip to main content
15,867,308 members
Articles / Web Development / ASP.NET
Article

ASP.NET 2.0 Globalization & Localization solution

Rate me:
Please Sign up or sign in to vote.
4.31/5 (19 votes)
14 May 20074 min read 243.3K   2.9K   65   14
internationalization of web application using ASP.NET 2.0 and SQL Server 2005

Introduction

In this article, we will discuss the internationalization of web application using ASP.NET 2.0 and SQL Server 2005. Through the given solution, we can achieve the scenario below:

1. Display content in localization for these users who haven't set prefered language.

2. Change language and culture immediately after selection.

3. Store settings in database to remember the prefered language

<shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"><stroke joinstyle="miter"><formulas><f eqn="if lineDrawn pixelLineWidth 0"><f eqn="sum @0 1 0"><f eqn="sum 0 0 @1"><f eqn="prod @2 1 2"><f eqn="prod @3 21600 pixelWidth"><f eqn="prod @3 21600 pixelHeight"><f eqn="sum @0 0 1"><f eqn="prod @6 1 2"><f eqn="prod @7 21600 pixelWidth"><f eqn="sum @8 21600 0"><f eqn="prod @7 21600 pixelHeight"><f eqn="sum @10 21600 0"></formulas><path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"><lock v:ext="edit" aspectratio="t">Image 1

(Diagram 1)

Image 2

(Diagram 2)

We use ASP.Net 2.0 Culture-sensetive formating and global resource settings here. This article isn't to discuss all about the internationalization in ASP.Net 2.0, but give one solution to bring the example how to implement that. However, I sugguest you'd better have a read about the relative information of ASP.NET 2.0 internationalization and localization before continuing.

ASP.NET 2.0 Internationalizing concepts:

Image 3

(Diagram 3)

Read more here:

http://www.asp.net/QuickStart/aspnet/doc/localization/default.aspx

Walk through:

1. Create database and web project

Image 4

(Diagram 4)

Only two tables, one is for user's information, the other is language or culture information.

After creating the database, then we can setup a new web project in VS2005/VWD2005.

2. Display localization content

ASP.Net 2.0 can render the localized HTML for special browsers. It checks the "Accept-Language" HTTP header to identify the browser settings. To perform the localization, we can simply add the attributes for @Page or add globalization section in web.config file.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Culture="auto" UICulture="auto" %>

Or

<!--// Web.config section under system.web-->

<globalization culture="auto" uiCulture="auto"/>

The differences between "Culture" and "UICulture" are Culture controls the formatting of dates, numbers, and currency whereas UICulture is for resource loading.

3. Change the UICulture when select specified language.

As in Diagram 2, when we select the language in dropdownlist control, the page's calendar, currency, and static text are going to be converted to corresponding localization. When ASP.NET web server receives one request, it will start one thread to handle the process, and the thread's culture and UICulture decide which culture of resource files will be loaded. Before this, we will come to set the resources for ASP.NET.

Right-click the web project, choose "App_GlobalResources" under "Add ASP.NET folder". And add a new resource file under that folder names "Resource.resx". To input the text is very easy, and no need to talk more here. Let's take a look at how to use the Global Resources on ASP.NET pages.

(1) On .aspx files, we can set the resource expression though the property toolbox.

Image 5

(Diagram 5)

Image 6

(Diagram 6)

This will add the explicit expression like:

<asp:Label ID="lbName" runat="server" Text="<%$ Resources:Resource, LoginUser %>"></asp:Label>

(2) Also we can use programmatic access in behind code by GetGlobalResourceObject() static method in HttpContext or though the default Resources namespace directly.

HttpContext.GetGlobalResourceObject("Resource", "LoginUser")

Or

Resources.Resource.LoginUser

By now, we can introduce the important properties of CurrentThread. They are Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture. If we set these two properties for current thread then we can get our needed culture resources files. And the best practice to evaluate is to override the page's InitializeCulture method. To make this happen, we should make the page reload to init again because the dropdownlist selected changed event occurs after this mehod. Coding likes below:

protected void ddlCulture_SelectedIndexChanged(object sender, EventArgs e){

Session["PreferedCulture"] = this.ddlCulture.SelectedValue;
Server.Transfer(Request.Url.LocalPath);
}

protected string CurrentCulture{
get{
if(null != Session["PreferedCulture"]){
return Session["PreferedCulture"].ToString();
}
return String.Empty;
}
}

protected override void InitializeCulture(){
if(!String.IsNullOrEmpty(CurrentCulture)){
try{
Thread.CurrentThread.CurrentCulture = new CultureInfo(CurrentCulture);
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
}
catch{throw;}
}
}

4. Make application UICulture change.

After section 3, we can change the page's culture with our need. But if you add one hyperlink to another page and visit it, you'll find that's not your want. On the second page, all settings are kept in the old culture. Although two pages use the same thread, they have the different culture info, and we haven't changed it artificially. I guess when the page2 class initializes, it initializes the culture with the default settings.

To solve this and make the whole application or website can use the same culture info, we add one Parent class file to every .ASPX file. And invoke the InitializeCulture method in it, every other page inherits this base class.

5. Touch the database now, and make all together.

From the sample code, we read all users and languages from database. The application will display in defferent culture settings for defferent users. When we change the language for specified user, it will write back the settings to database.

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.

A list of licenses authors might use can be found here


Written By
Web Developer
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionGreat Pin
onyangofred19-Nov-12 17:56
onyangofred19-Nov-12 17:56 
Just what I needed at the right time. Kudos Mr Chinese Laugh | :laugh:
Ogolla Onyango Fred

GeneralMy vote of 5 Pin
Manoj Kumar Choubey8-Feb-12 2:40
professionalManoj Kumar Choubey8-Feb-12 2:40 
Generalwow Pin
sdking30-Aug-10 20:23
sdking30-Aug-10 20:23 
GeneralBest Localization Plug-in for Visual Studio. Pin
Alexander Nesterenko17-Dec-08 21:35
Alexander Nesterenko17-Dec-08 21:35 
GeneralMessage Closed Pin
23-Aug-08 7:52
DotNetGuts23-Aug-08 7:52 
Generali have an error ,help me. Pin
tungsir25-Apr-08 22:32
tungsir25-Apr-08 22:32 
Generalthanks for your code Pin
tungsir25-Apr-08 17:58
tungsir25-Apr-08 17:58 
GeneralPurgatory Pin
Alexandru Lungu20-Feb-07 22:56
professionalAlexandru Lungu20-Feb-07 22:56 
GeneralSource Zip file is crrupt Pin
eporta26-Oct-06 2:01
eporta26-Oct-06 2:01 
AnswerRe: Source Zip file is crrupt Pin
Chagel26-Oct-06 15:44
Chagel26-Oct-06 15:44 
GeneralRe: Source Zip file is crrupt Pin
eporta27-Oct-06 4:14
eporta27-Oct-06 4:14 
AnswerRe: Source Zip file is crrupt Pin
Chagel27-Oct-06 15:39
Chagel27-Oct-06 15:39 
GeneralSource Zip file is crrupt please change Pin
Shafqat Balouchi12-Sep-06 0:44
Shafqat Balouchi12-Sep-06 0:44 
GeneralRe: Source Zip file is crrupt please change Pin
Chagel25-Sep-06 15:40
Chagel25-Sep-06 15:40 

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

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