SIGN UP MEMBER LOGIN:    
ARTICLE

How to Implement Web Application Localization in .NET 4.0

Posted by Kevin Wang Articles | ASP.NET Programming June 29, 2011
In this article, we will explore the necessary details for working with resources in ASP.NET applications and for creating international ASP.NET applications based on embedded resources and the integrated localization support.
Reader Level:


In this article, we will explore the necessary details for working with resources in ASP.NET applications and for creating international ASP.NET applications based on embedded resources and the integrated localization support.

Now there is an EasyUpdate Admin Software that supports English; we are going to localize it into Chinese.

Step 1:

Generate Resources for every page.

WLocaliz1.gif

For example:

There is a Login Page named Login.aspx.

And I am going to create two resource files named Login.aspx.resx for English and Login.aspx.zh-cn.resx for Chinese for the login page.

Select Tools > Generate Local Resources. Visual Studio then generates a resource file in the App_LocalResources folder, which includes the values for every control of the page currently open in design view.

WLocaliz2.gif

You will see it as below.

WLocaliz3.gif

Double-click it and you will see:

WLocaliz4.gif

Visual Studio automatically generates the default resources for the controls of the page only. You must add any further culture-specific resources manually by copying the generated resources and giving them the appropriate name (for example,(Login.aspx. resx)<-> Login.aspx.zh-cn.resx) then translate the values.

WLocaliz5.gif

In addition to generating the resource file, Visual Studio has changed the page's source code. For every [Localizable] property of each control placed on the page, it has added a localization expression, as shown in the following code snippet:

<asp:Button ID="ButtonLogin" runat="server" Text="Login" OnClick="ButtonLogin_Click"
                                        meta:resourcekey="ButtonLoginResource1" />


Localization expressions are identified by the meta:resourceKey attribute of the tag.

WLocaliz6.gif

So when we run it and change the language in the DropdownList we will see:

WLocaliz7.gif

WLocaliz8.gif

Step 2:

Sharing Resources Between Pages:

There are some tips and hardcoding used in some pages; we need to save them into resource files, because they don't belong to any page, so we will create Global resources.

Global resources are placed in the App_GlobalResources folder.

WLocaliz9.gif

WLocaliz10.gif

Now when the user clicks the Login button without inputing anything, I would like to show an Errormessage to the user reading from global resources.
The code for the Login button click event is as follows.

public partial class Login : System.Web.UI.Page
{
string strUserNameError;
}
protected void Page_Load(object sender, EventArgs e)
{
    strUserNameError = Resources.Messages.strUserNameError.ToString();
}
protected void ButtonLogin_Click(object sender, EventArgs e)
{
    if (TextBoxUserName.Text == "")
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "nameOrPwdError", "alert('" + strUserNameError + "');", true);
    }
}

The output is as follows.

WLocaliz11.gif

WLocaliz12.gif

Step 3:

Switching the culture programmatically by overriding Page.InitializeCulture Method:

In Login Page

protected override void InitializeCulture()
{
    if (Request.Form["DropDownListLogin"] != null)
    {
        string selectedLanguage = Request.Form["DropDownListLogin "];

        Response.Cookies["EasyUpdateLanguage"].Value = selectedLanguage;
        m_strLanguage = selectedLanguage;
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);

    }
    else if ( Request.Cookies["EasyUpdateLanguage"] != null )
    {

        string selectedLanguage = Request.Cookies["EasyUpdateLanguage"].Value;

        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
    }
    base.InitializeCulture();


Step 4:

Set a cookie value in Login Page and Read it in all other pages by overriding Page.InitializeCulture Method:

In Other Pages

Wrap the code follow into a class (ChangeCulture):System.web.ui.page

Let other page's classes inherit ChangeCulture.

protected override void InitializeCulture()
{
    if (Request.Form["ctl00$ctl00$ DropDownListMaster "] != null)
    {
        string selectedLanguage = Request.Form["ctl00$ctl00$DropDownListMaster"];

        Response.Cookies["EasyUpdateLanguage"].Value = selectedLanguage;
        String m_strLanguage = selectedLanguage;
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);

    }
    else if (Request.Cookies["EasyUpdateLanguage"] != null)
    {

        string selectedLanguage = Request.Cookies["EasyUpdateLanguage"].Value;
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
    }
    base.InitializeCulture();
}

Remark:

The first time the page reads cookies, and after Page_load, the page will overwrite a cookie value and give the cookie value to dropdownlist.selected.value, so that I can get the value when changing the language in a dropdownlist, only in this way can I unite the language and the culture in every page.

Thank you!

Login to add your contents and source code to this article
share this article :
post comment
 

It seems that this article duplicates MSDN, and adds nothing more.

Posted by Arshan Komburahai Nov 04, 2011

Good article for beginners for localization in asp.net.

Posted by Pradip Pandey Jun 29, 2011

Good work on localization. Keep it up.

Posted by Manish Dwivedi Jun 29, 2011

Thanks for useful article..

Posted by Dinesh Beniwal Jun 29, 2011
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor