ASP.NET Localization Part 1: CultureInfo
By Doug Seven
Published: 2/27/2001
Reader Level: Intermediate
Rated: 3.50 by 4 member(s).
Tell a Friend
Rate this Article
Printable Version
Discuss in the Forums

Introduction

The .NET Framework provides built-in mechanisms for localizing applications. ASP.NET internally uses Unicode and the String class of the .NET Framework also uses Unicode. The use of Unicode enables you to easily specify different encoding types for the response data sent to the client. While the .NET Framework will not translate your documents from the language they are written in to a different language, the built-in mechanisms will reconfigure output such as DateTime objects to their appropriate format.

In this tutorial you will learn how to use the System.Globalization namespace and the CultureInfo class to localize .NET objects in your Web applications. You will build the Web Form seen in Figure 1.1.

Downloadable sample code is provided in Visual Basic.NET and C#

Figure 1.1 - Localizing Web Applications

[ Run Sample ]

Get it On

ASP.NET localization is managed by the System.Globalization namespace. The globalization functions work with the culture encoding properties of the browser request. The browser is set to use a default culture setting, such as English USA (en-US) in the USA and German (de-DE) in Germany. You can set the culture encoding of a Web application in a number of ways.

Config.Web

You can set the default culture encoding for an entire web application in the configuration file (config.web). This is useful if you know the entire site needs a specified encoding type.

Listing 1.1 : Setting Culture Encoding in the Config.Web File
<configuration>
 <system.web>
  <globalization
    requestencoding="utf-8"
    responseencoding=" utf-8"
    fileencoding=" utf-8"
    culture="de-DE"
    uiculture="en" />
 </system.web>
</configuration>

In Listing 1.1 you set the default culture encoding to German (de-DE). Additionally you set the entire Web application to use UTF8 character encoding (you want umlauts to render!!). The default encoding is US-ASCII. You may have noticed that there is a uiencoding attribute. This specifies the default culture for processing locale-dependent resource searches, while the culture attribute specifies the default culture for processing incoming Web requests. In Listing 1.1 you set the uiculture to en (English).

Any subdirectory below the root directory can have a config.web file with a <globalization> section. The subdirectory's encoding values will override those set in the root config.web. This allows you to have locale dependent directories, each with a config.web that specifies the culture encoding for the subdirectory and all its child directories.

Page Level

Culture encoding can also be set at the page level in the @ Page directive.

Listing 1.2 : Setting Culture Encoding at the Page Level

<%@ Page Culture="ja-JP" UICulture="ja" ResponseEncoding="utf-8"%>

In Listing 1.2 you use the @ Page directive to override the config.web values. In this case you set the culture encoding to ja-JP (Japan).

Got It, Now Show Me How to Make that Page!

The culture encoding can also be set programmatically. This requires the use of both the System.Globalization.CultureInfo class and the System.Threading.Thread class. The CultureInfo class is used to specify the culture settings, and the Thread class is used to apply the new culture settings to the current thread (which then passes them to the client).

Break It Down

In Listing 1.3 you create the page shown above.

Listing 1.3 - Setting Culture Encoding Programmatically

<%@ Page ResponseEncoding="utf-8" %>
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="System.Threading" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Sql" %>

<script language="VB" runat="server">
 Public Sub Page_Load(Sender As Object, E As EventArgs)
  Dim newCulture As CultureInfo
  Dim myDataSetCommand As SqlDataSetCommand
  Dim myDataSet As New DataSet

  If Not Page.IsPostBack Then
   newCulture = CultureInfo.CurrentCulture
  Else
   newCulture = New CultureInfo(EncodeType.SelectedItem.Value)
   Thread.CurrentThread.CurrentCulture = newCulture
  End If

  CultureType.Text = newCulture.EnglishName

  myDataSetCommand = New SqlDataSetCommand("Select LastName, FirstName, BirthDate, HireDate From Employees", "server=[server name];database=Northwind;uid=[user name];pwd=[password];")
  myDataSetCommand.FillDataSet(myDataSet, "Employees")

  myDataGrid.DataSource = myDataSet.Tables("Employees").DefaultView
  myDataGrid.DataBind()
 End Sub
</script>

<html>
<head>
<title>Using Localization in ASP.NET</title>
</head>
<body style="font: x-small Verdana, Arial, sans-serif;">
<form runat="server" method="post">
<b>The Current Culture is:</b> <asp:Label runat="server" id="CultureType" />

<p><b>Choose your encoding:</b>
<asp:DropDownList runat="server" id="EncodeType" AutoPostBack="True">
 <asp:ListItem Value="en-US" Text="English" />
 <asp:ListItem Value="fr-FR" Text="French" />
 <asp:ListItem Value="de-DE" Text="German" />
 <asp:ListItem Value="he-IL" Text="Hebrew" />
 <asp:ListItem Value="ja-JP" Text="Japanese" />
 <asp:ListItem Value="es-MX" Text="Spanish (Mexican)" />
 <asp:ListItem Value="uk-UA" Text="Ukrainian (Ukraine)" />
</asp:DropDownList>
<br>
<span style="font:xx-small"><i>(Note: If some characters show up as empty rectangles, you have to install the additional language support for Japanese and Hebrew. On Windows 2000 open the Regional Options on the Control Panel and add the required language support.)</i></span>
</p>

<asp:DataGrid runat="server" id="myDataGrid"
  Width="740"
  BorderColor="Tan"
  ShowFooter="false"
  Font-Name="Verdana"
  Font-Size="8pt"
  HeaderStyle-Font-Bold="True"
  HeaderStyle-BackColor="Maroon"
  HeaderStyle-ForeColor="Tan"
  AutoGenerateColumns="False">

  <property name="Columns">
    <asp:BoundColumn DataField="FirstName" HeaderText="First Name" />
    <asp:BoundColumn DataField="LastName" HeaderText="Last Name" />
    <asp:BoundColumn DataField="BirthDate" DataFormatString="{0:D}" HeaderText="Birth Date" />
    <asp:BoundColumn DataField="HireDate" DataFormatString="{0:D}" HeaderText="Hire Date" />
  </property>
</asp:DataGrid>
</form>
</body>
</html>

In Listing 1.3 you create a Web Form with a Label (CultureType) to display the current culture information. There is a DropDownList (EncodeType) that provides a set of encoding options. The DropDownList is set to post back to the server when the value is changed (AutoPostback="True").

In the Page_Load() event handler you create a new CultureInfo instance (newCulture). If the request is the first call to the page (If Not Page.IsPostBack Then...) then the CultureInfo object is set to the culture of the requesting browser by using the CurrentCulture property of the CultureInfo class (CultureInfo.CurentCulture). If the request is the result of a postback then you set the newCulture object to the value of the selected item in the DropDownList. Once you have a culture object that is different than the requesting culture you need to set the CurrentCulture property of the current thread to the newCulture object (Thread.CurrentThread.CurrentCulture = newCulture).

For this sample application you then go on to populate a DataGrid with values from the Employees table of the Northwind sample database shipped with Microsoft's SQL Server and Access databases. The Employees table contains DateTime values that will be affected by the culture settings.

When the page is rendered the DateTime objects are reformatted from their original styles to the style appropriate for the culture settings. Play with the page a bit and see how the culture settings impact the rendering of the DateTime objects.

[ Run Sample ]

Additional Resources



Marketplace
(Sponsored Links)
What are the green links?
   



 
Copyright © 2007 CMP Tech LLC |
Privacy Policy (4/10/06) | Your California Privacy Rights (4/10/06) | Terms of Service | Advertising Info | About Us | Help