Setting the Current Culture


Two main properties of the Page class have an effect on localization:

  • UICulture

  • 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. This property is discussed in detail during the discussion of using local and global resources later in this chapter.

The Culture property, on the other hand, determines 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).

Both the UICulture and Culture properties accept standard culture names for their values. Culture names follow the RFC 1766 and RFC 3066 standards maintained by the Internet Engineering Task Force (IETF). The IETF website is located at http://www.IETF.org.

Here are some common culture names:

  • de-DE = German (Germany)

  • en-US = English (United States)

  • en-GB = English (United Kingdom)

  • es-MX = Spanish (Mexico)

  • id-ID = Indonesian (Indonesia)

  • zh-CN = Chinese (China)

Notice that each culture name consists of two parts. The first part represents the language code and the second part represents the country/region code. If you specify a culture name and do not provide a country/region codefor example, enthen you have specified something called a neutral culture. If you provide both a language code and a country/region codefor example, en-USthen you have specified something called a specific culture.

Note

You can view the culture names supported by the .NET Framework by looking up the entry for the CultureInfo class in the Microsoft .NET Framework SDK 2.0 documentation. It's a really long list.


The Culture property must always be set to a specific culture. This makes sense because, for example, different English speakers use different currency symbols. The UICulture property, on the other hand, can be set to either a neutral or specific culture name. Text written in Canadian English is pretty much the same as text written in U.S. English.

You can set the UICulture and Culture properties to the same value or different values. For example, if you are creating an online store then you might want to set the UICulture property to the value de-DE to display German product descriptions. However, you might want to set the Culture property to the value en-US to display product prices in United State currency amounts.

Setting a Culture Manually

You can set either the UICulture or Culture properties by using the <%@ Page %> directive. For example, the page in Listing 24.1 sets both properties to the value id-ID (Indonesian).

Listing 24.1. Bagus.aspx

<%@ Page Language="VB" Culture="id-ID" UICulture="id-ID" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server">     Sub Page_Load()         lblDate.Text = DateTime.Now.ToString("D")         lblPrice.Text = (512.33D).ToString("c")     End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <title>Bagus</title> </head> <body>     <form  runat="server">     <div>     Today's date is:     <br />     <asp:Label                  Runat="server" />     <hr />     The price of the product is:     <br />     <asp:Label                  Runat="server" />     </div>     </form> </body> </html> 

The page in Listing 24.1 displays a date and a currency amount. Because the Culture property is set to the value id-ID in the <%@ Page %> directive, both the date and currency amount are formatted with Indonesian cultural conventions (see Figure 24.1).

Figure 24.1. Displaying a localized date and price.


The date is displayed like this:

05 Nopember 2005

The currency amount is displayed as an Indonesian Rupiah amount like this:

Rp512

Note

Setting the Culture does not actually convert a currency amount. Setting a particular culture only formats the currency as appropriate for a particular culture. If you need to convert currency amounts, then you need to use a Web service: Conversion rates change minute by minute. See, for example, http://www.xmethods.com/.


Instead of using the <%@ Page %> directive to set the Culture or UICulture properties, you can set these properties programmatically. For example, the page in Listing 24.2 enables you to select a particular culture from a drop-down list of cultures (see Figure 24.2).

Figure 24.2. Selecting a culture from a DropDownList control.


Listing 24.2. SelectCulture.aspx

<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server">     Sub btnSelect_Click(ByVal sender As Object, ByVal e As EventArgs)         Culture = ddlCulture.SelectedValue     End Sub     Sub Page_PreRender()         lblDate.Text = DateTime.Now.ToString("D")         lblPrice.Text = (512.33D).ToString("c")     End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <title>Select Culture</title> </head> <body>     <form  runat="server">     <div>     <asp:Label                  Text="Culture:"         AssociatedControl         Runat="server" />     <asp:DropDownList                  DataTextField="DisplayName"         DataValueField="Name"         DataSource         Runat="server" />     <asp:Button                  Text="Select"         Runat="server" OnClick="btnSelect_Click" />     <asp:ObjectDataSource                  TypeName="System.Globalization.CultureInfo"         SelectMethod="GetCultures"         Runat="server">         <SelectParameters>             <asp:Parameter Name="types"                 DefaultValue="SpecificCultures" />         </SelectParameters>     </asp:ObjectDataSource>     <hr />     Today's date is:     <br />     <asp:Label                  Runat="server" />     <br /><br />     The price of the product is:     <br />     <asp:Label                  Runat="server" />     </div>     </form> </body> </html> 

The DropDownList control in Listing 24.2 is bound to an ObjectDataSource control, which retrieves a list of all the culture names supported by the .NET Framework. The culture names are retrieved during a call to the GetCultures() method of the CultureInfo class.

When you click the button to select a culture, the btnSelect_Click() method executes and assigns the name of the selected culture to the page's Culture property. When you select a new culture, the formatting applied to the date and currency amount changes.

Several websites on the Internet display a page that requires the user to select a language before entering the main website. For example, the Federal Express website (www.FedEx.com) requires you to select a country before entering the website.

You can take advantage of the Profile object to store a user's preferred culture. That way, a user needs to select a culture only once and the culture is then used any time the user returns to your website in the future. The page in Listing 24.3 illustrates this approach.

Listing 24.3. SelectCultureProfile.aspx

<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server">     Protected Overrides Sub InitializeCulture()         Culture = Profile.UserCulture         UICulture = Profile.UserUICulture     End Sub     Protected Sub btnSelect_Click(ByVal sender As Object, ByVal e As EventArgs)         Profile.UserCulture = ddlCulture.SelectedValue         Profile.UserUICulture = ddlCulture.SelectedValue         Response.Redirect(Request.Path)     End Sub     Private Sub Page_PreRender()         lblDate.Text = DateTime.Now.ToString("D")         lblPrice.Text = (512.33D).ToString("c")     End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <title>Select Culture Profile</title> </head> <body>     <form  runat="server">     <div>     <asp:Label                  Text="Culture:"         AssociatedControl         Runat="server" />     <asp:DropDownList                  DataTextField="DisplayName"         DataValueField="Name"         DataSource         Runat="server" />     <asp:Button                  Text="Select"         Runat="server" OnClick="btnSelect_Click" />     <asp:ObjectDataSource                  TypeName="System.Globalization.CultureInfo"         SelectMethod="GetCultures"         Runat="server">         <SelectParameters>             <asp:Parameter Name="types" DefaultValue="SpecificCultures" />         </SelectParameters>     </asp:ObjectDataSource>     <hr />     Today's date is:     <br />     <asp:Label                  Runat="server" />     <br /><br />     The price of the product is:     <br />     <asp:Label                  Runat="server" />     </div>     </form> </body> </html> 

You should notice two things about the page in Listing 24.3. First, notice that the culture is set in the InitializeCulture() method. This method overrides the InitializeCulture() method of the base Page class and sets the UICulture and Culture properties by using the Profile object.

Second, notice that the btnSelect_Click() handler updates the properties of the Profile object and redirects the page back to itself. This is done so that the InitializeCulture() method executes after a user changes the selected culture.

The page in Listing 24.3 uses the Profile defined in the web configuration file contained in Listing 24.4.

Listing 24.4. Web.Config

<?xml version="1.0"?> <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">   <system.web>     <anonymousIdentification enabled="true"/>     <profile>       <properties>         <add           name="UserCulture"           defaultValue="en-US" />         <add           name="UserUICulture"           defaultValue="en"/>       </properties>     </profile>   </system.web> </configuration> 

Notice that the web configuration file in Listing 24.4 includes a anonymousIdentification element. Including this element causes a profile to be created for a user even if the user has not been authenticated.

Automatically Detecting a Culture

In the previous section, you learned how to set the UICulture and Culture properties by allowing the user to select a particular culture from a DropDownList control. Instead of requiring users to select their culture, you can automatically detect users' cultures through their browser settings.

Whenever a browser makes a request for a web page, the browser sends an Accept-Language header. The Accept-Language header contains a list of the user's preferred languages.

You can set your preferred languages when using Microsoft Internet Explorer or Mozilla Firefox by selecting the menu option Tools, Internet Options and clicking the Languages button. You can then create an ordered list of your preferred languages (see Figure 24.3). When using Opera, select the menu option Tools, Preferences and click the Details button (see Figure 24.4).

Figure 24.3. Setting your preferred language with Internet Explorer.


Figure 24.4. Setting your preferred language with Opera.


You can retrieve the value of the Accept-Language header by using the Request.UserLanguages property. For example, the page in Listing 24.5 displays a list of the languages retrieved from a browser's Accept-Language header (see Figure 24.5).

Figure 24.5. Displaying a browser's language settings.


Listing 24.5. ShowAcceptLanguages.aspx

<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server">     Sub Page_Load()         bltAcceptLanguages.DataSource = Request.UserLanguages         bltAcceptLanguages.DataBind()     End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <title>Show Accept Languages</title> </head> <body>     <form  runat="server">     <div>     <asp:BulletedList                  Runat="server" />     </div>     </form> </body> </html> 

If you want to set the Culture or UICulture properties automatically by detecting the browser's Accept-Language header, then you can set either of these properties to the value auto. For example, the page in Listing 24.6 automatically displays the date and currency amount according to the user's preferred language.

Listing 24.6. SelectCultureAuto.aspx

<%@ Page Language="VB" Culture="auto:en-US" UICulture="auto:en-US"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server">     Sub Page_PreRender()         lblDate.Text = DateTime.Now.ToString("D")         lblPrice.Text = (512.33D).ToString("c")     End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <title>Select Culture Auto</title> </head> <body>     <form  runat="server">     <div>     Today's date is:     <br />     <asp:Label                  Runat="server" />     <br /><br />     The price of the product is:     <br />     <asp:Label                  Runat="server" />     </div>     </form> </body> </html> 

In the <%@ Page %> directive in Listing 24.6, both the Culture and UICulture attributes are set to the value auto:en-US. The culture name that appears after the colon enables you to specify a default culture when a language preference cannot be detected from the browser.

Warning

Don't assume that all values of the Accept-Language header retrieved from a browser are valid culture names. Most browsers enable users to enter a "user-defined" language, which may or may not be valid.


Setting the Culture in the Web Configuration File

Rather than set the Culture and UICulture properties in each page, you can set these properties once in the web configuration file. Typically, you should take this approach because it makes your website easier to maintain.

The web configuration file in Listing 24.7 sets both the Culture and UICulture properties to the value de-DE (German).

Listing 24.7. Web.Config

<?xml version="1.0"?> <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> <system.web>   <globalization     culture="de-DE"     uiCulture="de-DE" /> </system.web> </configuration> 

The web configuration file in Listing 24.7 sets the Culture and UICulture for all pages to the value de-DE (German).

If you prefer, you can use the value auto in the web configuration file if you want the culture to be automatically detected based on the value of the browser Accept-Language header. If you need to override the configuration settings in the web configuration file in a particular page, then you can simply set the Culture and UICulture properties in the page.

Culture and ASP.NET Controls

The value of the Culture property automatically has an effect on the rendering behavior of ASP.NET controls such as the Calendar control. For example, Listing 24.8 uses the ASP.NET Calendar controll to display a calendar (see Figure 24.6).

Figure 24.6. Displaying a localized Calendar control.


Listing 24.8. ShowCalendar.aspx

<%@ Page Language="VB" Culture="id-ID" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <title>Show Calendar</title> </head> <body>     <form  runat="server">     <div>     <asp:Calendar                  Runat="server" />     </div>     </form> </body> </html> 

The Culture attribute in the <%@ Page %> directive is set to the value id-ID (Indonesian). When the calendar is rendered, Indonesian month names are displayed in the calendar.




ASP. NET 2.0 Unleashed
ASP.NET 2.0 Unleashed
ISBN: 0672328232
EAN: 2147483647
Year: 2006
Pages: 276

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net