Thursday, September 25, 2008

What is globalization and localization?

Theory:

Working for applications which are specific to a location (Windows applications, domestic web applications, windows service etc) , whose users shall provide requirements about there interface display language (mostly English) can be hard coded or not required to be configured or bothered at run time.

But web applications can be accessed by different users from different countries and locations. But the web application or web site which you publish in a machine or server is location specific.

Since application server is location specific and the request it receives from users through browsers are globalize or not from same locations , your web application should be capable enough of sending rich content (HTML) and language specific response to specific browsers. This can be done easily by using Resource files.

Obviously to differentiate requests from different browsers, each Http Request from browser sent to the web server sends the request object information which includes language from browser settings available for the client browser.
(In Internet explorer: - Tools/ Internet Options /General (Tab)/Languages (button), you can change your browser settings while testing globalization concept).
Request.UserLanguages[0] gives the first language in the list selected by client.

Now starts the real use of Resource files on application server side which are called according to this Userlanguage.

protected void Page_Load(object sender, EventArgs e)
{
string[] strUserLanguage=Request.UserLanguages;
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(strUserLanguage[0]);
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(strUserLanguage[0]);
this.InitializeCulture();
}

You can observe in the above lines of code I am calling group of instructions or lines of code in page load of a page.
What is happening here?

First I am taking the user (Browser request object) location language into variable.
As we know every time web server recieves a request , it is state less. That is it request object is not persistent or state full after response is sent to browser.
So every time it recieves a request it does not know from where it is though it has received request from same browser many times earlier.

So each time it recieves a request , page load event fires every time and in this event handler fuunction (Page_Load) I should change culture information available on currently created thread to run the request according to user language.

CurrentUICulture : - Modifying current UICulture , User Interface in web page (language) according to users language.

CurrentCulture : - Modifying Curent Culture ,Options dependant upon users culture are affected (Currency,Time format etc…).
Atlast I am initialzing the culture of the request object before processing it.

Globalisation : Developing a web site whose response content (language settings) is updated dynamically according to users language (Request object).

Localisation : Developing a web site whose response content is always static but configurable through resource files.


Practical :


Globaliastion :

1) Right click on your web site in visual studio 2005.
2) Add Asp.Net folder\App_GlobalResources
3) Right click on App_GlobalResources folder and click on Add new item and add a “Resource.resx” file.
Note : Be specific about the file name , let it be Resource.resx.


For each different languages that you are willing to support through your website, should be added in the simillar fashion but the file names should be accoring to the language.
Syntax :- Resource.language.resx
Resource.en-US.resx where en-US is the language from US according to browser language settings.

So if there is request from a country whose language settings is not matched with the resource file , I mean if there is no resource file for a request (language) then process “Resource.resx” file (default file) will be considered.

For example let us take telugu from India which is “te-IN”.

Create a new aspx page (Globalisation.aspx).
In globalisation.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Threading;
using System.Collections.Specialized;

public partial class Globalisation : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["cid"] != null)
{
//Assigning culture to current running thread which is taken from query string
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Request.QueryString["cid"].ToString());
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(Request.QueryString["cid"].ToString());
this.InitializeCulture();
}
//After the culture is initialized then i am displaying current cultures native name (if india bharat desam ...)
//This is current culture dependant
lblCul.Text = System.Globalization.CultureInfo.CurrentCulture.NativeName;

//Here comes the role of resource file, sine the laanguage is from query string
//suppose if it is 'te-IN' (telugu - india) , then Resource.te-IN.resx file available
//in app_globalresources folder is considered , if does not exist then Resource.resx (Default file is considered)
//This is UI culture specific
btnShow.Text = Resources.Resource.btnShow.ToString();

//i am displaying current cultures Currency (if india Rs)
//This is current culture dependant
lblCurrency.Text = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol;
}
}

I am expecting a query sting which sends the language instead of taking from request object. This helps in checking globalisation for different languages just by changing the query string instead of changing in browser every time.

Please read comments mentioned in above code for each line.

So create a resoource file with name Resource.te-IN.resx to test application of Telugug India as explained earlier.

When you double click on the created resource file you can see in this manner.

















I have entered text ,
btnShow under name column says the id of the control from my aspx page for which I am willing to change the text.
The text that I want to display for thi te-IN culture is my name in telugu. (You can get language specific fints from many 3rd part web sites)
At this instant you can even value in english no problem.





<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblCul" runat="server">
</asp:Label>
<asp:Label ID="lblUICul" runat="server">
</asp:Label>
<asp:Label ID="lblCurrency" runat="server">
</asp:Label>
<asp:Button ID="btnShow" runat="server" />
<asp:Calendar id="clnder" runat="server"></asp:Calendar>
</div>
</form>
</body>
</html>




You can observe in the above aspx page html content there is no text for button (id=btnShow), but while running appliaction I am able to view text saying my name in telugu.

















If Resource.te-IN.resx file does not exist then , the value defined for this “btnShow” ID from default (Resource.resx) is considered.



Conclusion :

So you need to bother map resource files only for displaying text or labels etc… but things like CurrentCulture dependant (currency, Time format) are automatically updated once you assign a specific language or culture to current running thread.

3 comments:

suneetha said...

I really liked this article. It is very informative. Suneetha

suneetha said...

Check the conclusion i guess you need to add "not" in between need and too in the first line.
"So you need to bother map resource "

suneetha

Programming and Database Concepts said...

Hi suneetha,
Thank you, for your compliments.
The context is true, that is resource files are used for only to control UI culture (displaying language speific words) where as culture dependant (currency ) are automatically updated or modified and not rquied to use resource files for culture dependancies.
cheers,
Srinivas