Asp.Net Globalization and Localization Introduction

Globalization is the process of creating an application that meets the needs of users from multiple cultures. It includes using the correct currency, date and time format, calendar, writing direction sorting rules and other issues. Accommodating these cultural differences in an application is called localization.

 

Before Asp.Net, to localize the web application we have to create multiple copies of  the same web site and translate it into different languages. In this case maintaining multiple websites is very difficult, to change any content we have to change it in all versions. But you can easily solve this problem in Asp.Net by using Resource files and Culture, UICulture properties.

 

Here we will discuss about Culture and UICulture properties.

 

There are two properties for Page class in Asp.Net to accommodate location, those are UICulture and Culture.  The UICulture property is used to specify which resource files are loaded for the page. The resource files can contain all the text content of your pages translated into a particular language. You can set this property to any standard culture name.  The Culture property is used to determine how strings such as dates, numerals, and currency amounts are formatted. It also determines how values are compared and sorted. For example, by modifying the Culture property, you can display dates with language-specific month names such as January (English), Januar (German), or Enero(Spanish).

 

You have to set the Culture property to specific value, because different english speaking people use different date formats, different currency names. But, you can set UICulture property to wither neutral or specific culture name, because text written in Candaian english is almost same as text written in US english. The values of Culture and UICulture properties are same or may be different.

 

Here I explained about different ways to set culture for an asp.net application.

 

First, we will discuss about how to set culture for each page manually. We can do this by assigning the values to Culture and UICulture properties in Page directive as shown below.

 

Default.aspx

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Set Culture Manually</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    The Current Time in Germany format is:

    <asp:Label ID="lblTime" runat="server"></asp:Label>

    <br />

    The price in Germany format is:

    <asp:Label ID="lblPrice" runat="server"></asp:Label>

    </div>

    </form>

</body>

</html>

 

Default.aspx.cs Code

protected void Page_Load(object sender, EventArgs e)

{

       lblTime.Text = DateTime.Now.ToString();

       lblPrice.Text = (112.33m).ToString("c");

}

 

Here we manually assign the culture to Germany, de-DE.  The output will display as shown below.

 

 

 

You can ask the user to select specific culture before login from list of predefined cultures as shown below. You can store these options in profile object and assign these values in each page after loginning.

 

Login.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title>Save user preferred Culture in profile</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    Select Culture:

    <asp:DropDownList ID="ddlCultures" runat="server" DataSourceID="srcCultures"

     DataValueField="Name" DataTextField="DisplayName"></asp:DropDownList>

 

     <asp:ObjectDataSource ID="srcCultures" runat="server"

      TypeName="System.Globalization.CultureInfo" SelectMethod="GetCultures">

         <SelectParameters>

            <asp:Parameter Name="types" DefaultValue="SpecificCultures" />

        </SelectParameters>

    </asp:ObjectDataSource>

     <asp:Button ID="btnLogin" runat="server" Text="Login" onclick="btnLogin_Click" />

    </div>

    </form>

</body>

</html>

 

Login.aspx.cs code

public partial class Login : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    protected void btnLogin_Click(object sender, EventArgs e)

    {

        Profile.CultureName = ddlCultures.SelectedItem.Text;

        Profile.UserCulture = ddlCultures.SelectedValue;

        Profile.UserUICulture = ddlCultures.SelectedValue;

        Response.Redirect("Default3.aspx");

    }

}

 

 

As shown above, we have dropdown list which displays the list of cultures. User can select any value from the list and we are storing these values in profile object before navigating to post login page Default3.aspx. We defined the Profile object in web.config with CultureName, UserCulture and UserUICulture properties as shown below.

 

web.config

<profile>

      <properties>

        <add name="CultureName" defaultValue="English"/>

        <add name="UserCulture" defaultValue="en-us"/>

        <add name="UserUICulture" defaultValue="en-us"/>

      </properties>     

</profile>

 

We are retrieving these values in Default3.aspx page assigning to the Culture and UICulture properties as shown below.

 

Default3.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

       The Current Time in <%=Profile.CultureName%> format is:

    <asp:Label ID="lblTime" runat="server"></asp:Label>

    <br />

    The price in <%=Profile.CultureName%> format is:

    <asp:Label ID="lblPrice" runat="server"></asp:Label>

    </div>

  </form>

</body>

</html>

 

Default3.aspx.cs

public partial class Default3 : System.Web.UI.Page

{

    protected override void InitializeCulture()

    {

        Culture = Profile.UserCulture;

        UICulture = Profile.UserUICulture;

    }

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    void Page_PreRender()

    {

        lblTime.Text = DateTime.Now.ToString();

         lblPrice.Text = (112.33m).ToString("c");

    }

}

 

We are overriding the predefined method InitializeCulture() and assigning the user selected culture to Culture and UICulture properties.

 

Sometimes we have to detect culture information automatically from user browser and based on that we have to display the data. For that we will use auto keyword in Culture and UICulture properties as shown below.

 

Default.aspx

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

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

   <div>

    The Current Time format based on Browser settings is:

    <asp:Label ID="lblTime" runat="server"></asp:Label>

    <br />

    The price format based on Browser settings is:

    <asp:Label ID="lblPrice" runat="server"></asp:Label>

     </div>

    </form>

</body>

</html>

 

As shown above, we are detecting the culture information from browser. If browser does not return any information, we are setting to default Culture as en-us. We can detect the browser information through web.config file also as shown below.


web.config

<globalization culture="auto:en-us" uiCulture="auto:en-us" />

 

We can change language settings in our browser. To change language settings (culture settings) in IE, firefox, go to Tools -> Options, under Languages tab, select Choose.

 

In my next article I will explain about how to create resource files and how to use resource files in Asp.Net

 

                                                                                                        AspNetLocalization.zip (4.05 mb)

                                                                                              DetectCultureAutomatically.zip (3.09 kb)