Sunday, February 18, 2007

Ease of applying Globalization and Localization with ASP.net 2.0

I had previously written, an article explaining Localization and Globalization concept in asp.net 1.3, but with asp.net 2.0 the same generalize concept of localization and globalization can be implemented more easily.

I would suggest reader to read the previous article which help you to identify the ease of applying the concept.
http://dotnetguts.blogspot.com/2006/11/understanding-globalization-and.html

Understanding concept with Example
Step1: Create a Web Project.

Step2: Design your form.
For this example : Take 5 label controls, 4 textbox controls and 1 drop-down list control.
Place the control on form as shown in figure.



Step3: Identifying Resources that needs to be localized (Globalization Phase).
We want the output as shown in figure:



So here Resource that needs localization are

Language Name Label
Long Date Label
currency Label
Amount Label
Welcome Label (It would be same for all language, except its formatting might change, Note: you can do this easily with style-sheet concept, but as an example here I am explaining it with globalization concept.)

Now, Applying Localization to resources which needs to be localized and globalization phase. (Localization Phase begin).

Step5: Creating Resource File.
For this example i am creating resource file for French and German culture and other culture would get value from default resource file. Right click the Project in Solution Explorer and add a “asp.net folder”

Add App_GlobalResources Folder Add App_LocalResources Folder




Adding Global Resource File under App_GlobalResources Folder




Adding Local Resource File under App_LocalResources Folder

Note: While creating resource file, you should name resource file with filename.aspx.cultercode.resx

Eg:
1 For Default.aspx file, Default resource file would be Default.aspx.resx


2 For German resource file for Default.aspx would be Default.aspx.de-DE.resx


3 For French resource file for Default.aspx would be Default.aspx.fr-FR.resx



Step6: Adding code in .CS file to make it work

Adding Namespace
using System.Globalization;
using System.Drawing;
using System.Threading;

Filling Dropdown with set of languages for user-choice.
Code in Page Load Event.

if(!Page.IsPostBack)
{
//Applying Globalization to Header Text
lblWelcome.Text = Resources.GlobalResource.HeaderTitleText; lblWelcome.ForeColor = Color.FromName(Resources.GlobalResource.HeaderTitleColor);
//CultureInfo Class GetCultures gives collection of language.
foreach(CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{
//Filling the language collection into drop-downlistbox for user choice.
ddlLanguage.Items.Add(new ListItem(ci.EnglishName,ci.Name));
}
//Calling the Event when page loads for the first time.
ddlLanguage_SelectedIndexChanged(sender,e);
}


Assigning selected language value to current thread, to reflect changes.
private void ddlLanguage_SelectedIndexChanged(object sender, System.EventArgs e)
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo(ddlLanguage.SelectedValue);
Thread.CurrentThread.CurrentCulture = new CultureInfo(ddlLanguage.SelectedValue);

//Applying Localization
ApplyLocalization();
ApplyUILocalization();
}


Code which requires localization value.
private void ApplyUILocalization()
{
lblLanguageName.Text = Convert.ToString( this.GetLocalResourceObject("LanguageName"));
lblLongDate.Text = Convert.ToString( this.GetLocalResourceObject("LongDate"));
lblCurrency.Text = Convert.ToString( this.GetLocalResourceObject("Currency"));
lblAmount.Text = Convert.ToString( this.GetLocalResourceObject("Amount"));
}


Code which does not requires localization.
private void ApplyLocalization()
{
//Lets Assume currency and amount to keep example simple.
double dblCurrency = 500.50;
double dblAmount = 9580000.75;

//No extra effort for localization as you see here.
txtLanguageName.Text = ddlLanguage.SelectedItem.Text;
txtLongDate.Text = DateTime.Now.ToLongDateString();
txtCurrency.Text = dblCurrency.ToString("c");
txtAmount.Text = dblAmount.ToString("n");
}


Now, run the application and select the different culture and based on selection you will get the localized display as shown in figure:

When associated resource file not available culture from Default Resource File would be shown.









how simplest syntax structure is used to perform globalization and localization.

5 comments:

Anonymous said...

This is very GoodExample .
i understood very easly.
please give example on changing the text with different languages.

DotNetGuts said...

Thanks,

Localization and Globalization concept applies to resources (like display labels, etc) and upto certain extent to data, like currency, date format, digit seperator and so... unfortunately you cannot change text data feed in by user automatically as language chages, (On base of recent event attended by me, i come to know that in future microsoft will be going to add this feature to OS, wherein you have liberty to change data as you change language).

Anonymous said...

I am asp.net developer

Good Example for Localization, Thanks.

Shadow said...

This is very good example, i understood very easily....but u said ApplyLocalization()and ApplyUILocalization() are localization. these functions are behaving differently at different cultures, so this is not localization, it is globalization..and lblWelcome label is also not globalization,it is not behaving differently at different places, so it is localization....so please change it..thanq

Venkat Rao Tammineni said...

Hi,

I want to enable my website as multi languages.I have master page and default page.I default page i have number of controls about 200 plus controls.I am not able to understand how to enable multi languages for for all the controls.

Please can you guide...


Thanks and Regards,

Ven

Most Recent Post

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape