如何:为 ASP.NET 网页全球化设置区域性和 UI 区域性

更新:2007 年 11 月

在 ASP.NET 网页中,可以设置两个区域性值,即 CultureUICulture 属性。Culture 值确定与区域性相关的函数的结果,如日期、数字和货币格式等。UICulture 值确定为页加载哪些资源。

bz9tc508.alert_note(zh-cn,VS.90).gif说明:

CultureUICulture 属性是使用标识语言的 Internet 标准字符串(例如,en 代表英语,es 代表西班牙语,de 代表德语)和标识区域性的 Internet 标准字符串(例如,US 代表美国,GB 代表英国,MX 代表墨西哥,DE 代表德国)设置的。一些示例包括:en-US 代表英语/美国,en-GB 代表英语/英国,es-MX 代表西班牙语/墨西哥。有关更多信息,请参见 CultureInfo

这两个区域性设置不需要具有相同的值。根据您的应用程序,分别设置它们可能很重要。Web 拍卖站点就是这样的一个示例。对于每个 Web 浏览器,UICulture 属性可能有所变化,而 Culture 保持不变。因此,价格始终以相同的货币和格式显示。

Culture 值只能设置为特定的区域性,如 en-US 或 en-GB。这样就不必标识用于 en(对于该字符串,en-US 和 en-GB 具有不同的货币符号)的正确的货币符号。

用户可以在他们的浏览器中设置区域性和 UI 区域性。例如,在 Microsoft Internet Explorer 的**“工具”菜单上,用户可以依次单击“Internet 选项”“常规”选项卡、“语言”**,然后设置他们的语言首选项。如果 Web.config 文件中 globalization 元素的 enableClientBasedCulture 属性设置为 true,则 ASP.NET 可以根据由浏览器发送的值自动设置网页的区域性和 UI 区域性。

完全依赖浏览器设置来确定网页的 UI 区域性并不是最佳做法。用户使用的浏览器通常并未设置为他们的首选项(例如在 Internet 咖啡馆中)。您应该为用户提供显式选择页面的语言或语言和区域性(CultureInfo 名称)的方法。

以声明方式设置 ASP.NET 网页的区域性和 UI 区域性

  • 若要设置所有页的区域性和 UI 区域性,请向 Web.config 文件添加一个 globalization 节,然后设置 uiculture 和 culture 属性,如下面的示例所示:

    <globalization uiCulture="es" culture="es-MX" />
    
  • 若要设置单个页的区域性和 UI 区域性,请设置 @ Page 指令的 Culture 和 UICulture 属性,如下面的示例所示:

    <%@ Page UICulture="es" Culture="es-MX" %>
    
  • 若要使 ASP.NET 将区域性和 UI 区域性设置为当前浏览器设置中指定的第一种语言,请将 UICulture 和 Culture 设置为 auto。也可以将该值设置为 auto:culture_info_name,其中 culture_info_name 是区域性名称。有关区域性名称的列表,请参见 CultureInfo。您可以在 @ Page 指令或 Web.config 文件中进行该设置。

以编程方式设置 ASP.NET 网页的区域性和 UI 区域性

  1. 重写该页的 InitializeCulture 方法。

  2. 在重写的方法中,确定要为页设置的语言和区域性。

    bz9tc508.alert_note(zh-cn,VS.90).gif说明:

    InitializeCulture 方法在页生命周期的很早的时期调用,此时还没有为页创建控件,也没有为页设置属性。因此,若要读取从控件传递给页的值,必须使用 Form 集合直接从请求获取这些值。

  3. 以下列方式之一设置区域性和 UI 区域性:

    下面的代码示例显示一个 ASP.NET 网页,该网页允许用户从下拉列表中选择他们的首选语言。该页导入两个命名空间,使得使用线程处理类和全球化类更加方便。

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

请参见

其他资源

ASP.NET 全球化和本地化