Partilhar via


How to: Set the Culture and UI Culture for ASP.NET Web Page Globalization

In an ASP.NET Web page, you can set to two culture values, the Culture and UICulture properties. The Culture value determines the results of culture-dependent functions, such as the date, number, and currency formatting, and so on. The UICulture value determines which resources are loaded for the page.

Note

The Culture and UICulture properties are set using Internet-standard strings that identify the language (for example, en for English, es for Spanish, and de for German) and culture (for example, US for the United States, GB for Great Britain, MX for Mexico, and DE for Germany). Some examples are en-US for English/United States, en-GB for English/Great Britain, and es-MX for Spanish/Mexico. For more information, see CultureInfo.

The two culture settings do not have to have the same value. Depending on your application, it might be important to set them separately. An example is a Web auction site. The UICulture property might change for each Web browser, whereas the Culture stays constant. Therefore, prices are always displayed in the same currency and formatting.

The Culture value can be set to specific cultures only, such as en-US or en-GB. This prevents the requirement to identify the correct currency symbol to use for en, where en-US and en-GB have different currency symbols.

Users can set the UI culture and culture in their browsers. For example, in Microsoft Internet Explorer, on the Tools menu, users can click Internet Options, on the General tab, click Language, and then set their language preference. If the enableClientBasedCulture attribute of the globalization element in the Web.config file is set to true, ASP.NET can set the UI culture and culture for a Web page automatically, based on the values that are sent by a browser.

It is not a best practice to rely exclusively on browser settings to determine the UI culture for a page. Users frequently use browsers that are not set to their preferences (for example, in an Internet cafe). You should provide a method for users to explicitly choose a language or language and culture (CultureInfo name) for the page.

To set the culture and UI culture for an ASP.NET Web page declaratively

  • To set the UI culture and culture for all pages, add a globalization section to the Web.config file, and then set the uiculture and culture attributes, as shown in the following example:

    <globalization uiCulture="es" culture="es-MX" />
    
  • To set the UI culture and culture for an individual page, set the Culture and UICulture attributes of the @ Page directive, as shown in the following example:

    <%@ Page UICulture="es" Culture="es-MX" %>
    
  • To have ASP.NET set the UI culture and culture to the first language that is specified in the current browser settings, set UICulture and Culture to auto. Alternatively, you can set this value to auto:culture_info_name, where culture_info_name is a culture name. For a list of culture names, see CultureInfo. You can make this setting either in the @ Page directive or Web.config file.

To set the culture and UI culture for an ASP.NET Web page programmatically

  1. Override the InitializeCulture method for the page.

  2. In the overridden method, determine which language and culture to set the page to.

    Note

    The InitializeCulture method is called very early in the page life cycle, before controls are created or properties are set for the page. Therefore, to read values that are passed to the page from controls, you must get them directly from the request using the Form collection.

  3. Set the UI culture and culture in one of the following ways:

    The following code example shows an ASP.NET Web page that lets users select their preferred language from a drop-down list. The page imports two namespaces to make it more convenient to work with threading and globalization classes.

    <%@ Page Language="VB" uiculture="auto" %>
    <%@ Import Namespace="System.Threading" %>
    <%@ Import Namespace="System.Globalization" %>
    
    <!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()
            If Request.Form("ListBox1") IsNot Nothing Then
                Dim selectedLanguage As String = _
                    Request.Form("ListBox1")
                UICulture = Request.Form("ListBox1")
                Culture = Request.Form("ListBox1")
                Thread.CurrentThread.CurrentCulture = _
                    CultureInfo.CreateSpecificCulture(selectedLanguage)
                Thread.CurrentThread.CurrentUICulture = New _
                    CultureInfo(selectedLanguage)
            End If
            MyBase.InitializeCulture()
        End Sub
    </script>
    <html>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ListBox ID="ListBox1" runat="server">
                <asp:ListItem Value="en-US" 
                    Selected="True">English</asp:ListItem>
                <asp:ListItem Value="es-MX">Español</asp:ListItem>
                <asp:ListItem Value="de-DE">Deutsch</asp:ListItem>
            </asp:ListBox><br />
            <asp:Button ID="Button1" runat="server" 
                Text="Set Language" 
                meta:resourcekey="Button1" />
            <br />
            <asp:Label ID="Label1" runat="server" 
                Text="" 
                meta:resourcekey="Label1" />
            </div>
        </form>
    </body>
    </html>
    
    <%@ Page Language="C#" uiculture="auto" %>
    <%@ Import Namespace="System.Threading" %>
    <%@ Import Namespace="System.Globalization" %>
    <script runat="server">
    protected override void InitializeCulture()
    {
        if (Request.Form["ListBox1"] != null)
        {
            String selectedLanguage = Request.Form["ListBox1"];
            UICulture = selectedLanguage ;
            Culture = selectedLanguage ;
    
            Thread.CurrentThread.CurrentCulture = 
                CultureInfo.CreateSpecificCulture(selectedLanguage);
            Thread.CurrentThread.CurrentUICulture = new 
                CultureInfo(selectedLanguage);
        }
        base.InitializeCulture();
    }
    </script>
    <html>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ListBox ID="ListBox1" runat="server">
                <asp:ListItem Value="en-US" 
                    Selected="True">English</asp:ListItem>
                <asp:ListItem Value="es-MX">Español</asp:ListItem>
                <asp:ListItem Value="de-DE">Deutsch</asp:ListItem>
            </asp:ListBox><br />
            <asp:Button ID="Button1" runat="server" 
                Text="Set Language" 
                meta:resourcekey="Button1" />
            <br />
            <asp:Label ID="Label1" runat="server" 
                Text="" 
                meta:resourcekey="Label1" />
            </div>
        </form>
    </body>
    </html>
    

See Also

Other Resources

ASP.NET Globalization and Localization