Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
 How to: Set the Culture and UI Cult...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
ASP.NET
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.

NoteNote:

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.

    NoteNote:

    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.

    Visual Basic
    <%@ 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>
    

    C#
    <%@ 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>
    
Community Content   What is Community Content?
Add new content RSS  Annotations
Type error in example      Ppiglets ... Mahiways   |   Edit   |   Show History
In the above example: "<globalization uiculture="es" culture="es-MX" />" needs to be "<globalization uiCulture="es" culture="es-MX" />"
globalization section has to go into system.web      insane.k   |   Edit   |   Show History
Notably, when adding globalization to web.config, it needs
to be within system.web

e.g.

<system.web>

<globalization uiCulture="en" culture="en-GB" />
<!-- more stuff-->
</system.web>
no support for en-IN (english-India) culture ??      prashantJ ... Stanley Roark   |   Edit   |   Show History
Is there no support for en-IN (english-India) culture in ASP.Net ???

I can configure ASP.Net for Hindi, Marathi, Gujrati etc. Indian languages, but not for english-India, which has some special notations for currency, different date format....????

How do I display currency in Indian Rupee format, such as Rs. 1,00,000 which is read as "Rupees One Lakh" ????
locale issue      Mark Huang   |   Edit   |   Show History

When the environment is local OS+local Visutal Studio 2008 (in my example, traditional chinese)

please add

<asp:ListItem Value="zh-TW">Chinese(Traditional)</asp:ListItem>

to the example. And choose this list item.

When use debug(press F5) to debug the code, after the two lines are executed:

UICulture = selectedLanguage ;
Culture = selectedLanguage ;

The debugger indicates that the value of UICulture and Culture are local language strings(in my example, traditional chinese operating system, the string is "中文(繁體)"). But the value of the variable selectedLanguage is "zh-TW". Is this a normal phenomena? Or this is a bug?

Tags What's this?: Add a tag
Flag as ContentBug
Additional steps for it to work.      Alexei Sleur ... Thomas Lee   |   Edit   |   Show History

In order for the Localization (and i think the Globalization ) to work you have to grant access to the "App_LocalResources" folder. Seeing that now a days every website we create will have authentication and authorization. You have do this step to get it to work.
This is done by placing the next block of XML into the Web.config file under the configuration tag:

<configuration>

.......
<location path="App_LocalResources">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
....
</configuration>


Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker