Click here to Skip to main content
Click here to Skip to main content
Go to top

ASP.NET 2.0 Globalization & Localization solution

, 14 May 2007
Rate this:
Please Sign up or sign in to vote.
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

<formulas></formulas>

(Diagram 1)

(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:

(Diagram 3)

Read more here:

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

Walk through:

1. Create database and web project

(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.

(Diagram 5)

(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

Share

About the Author

Chagel
Web Developer
China China
No Biography provided

Comments and Discussions

 
QuestionGreat Pinmemberonyangofred19-Nov-12 17:56 
GeneralMy vote of 5 Pinmembermanoj kumar choubey8-Feb-12 2:40 
Generalwow Pinmembersdking30-Aug-10 20:23 
GeneralBest Localization Plug-in for Visual Studio. PinmemberAlexander Nesterenko17-Dec-08 21:35 
GeneralMessage Automatically Removed PinmemberDotNetGuts23-Aug-08 7:52 
Generali have an error ,help me. Pinmembertungsir25-Apr-08 22:32 
Generalthanks for your code Pinmembertungsir25-Apr-08 17:58 
GeneralPurgatory PinmemberAlexandru Lungu20-Feb-07 22:56 
GeneralSource Zip file is crrupt Pinmembereporta26-Oct-06 2:01 
AnswerRe: Source Zip file is crrupt PinmemberChagel26-Oct-06 15:44 
GeneralRe: Source Zip file is crrupt Pinmembereporta27-Oct-06 4:14 
AnswerRe: Source Zip file is crrupt PinmemberChagel27-Oct-06 15:39 
GeneralSource Zip file is crrupt please change PinmemberDr.Balouchi12-Sep-06 0:44 
GeneralRe: Source Zip file is crrupt please change PinmemberChagel25-Sep-06 15:40 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140905.1 | Last Updated 15 May 2007
Article Copyright 2006 by Chagel
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid