ASP.NET资源表达式未返回正确的文化值

时间:2009-07-29 09:55:18

标签: c# asp.net resources localization globalization

我在ASP.NET页面中使用Resource表达式指令,该页面包含四个全局资源文件,中性,英国,美国和意大利语。但是,使用表达式语法始终返回US。

ASPX页面的一些代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GlobalisationResources._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2>Current culture: <%= System.Threading.Thread.CurrentThread.CurrentUICulture.Name + " - " + System.Threading.Thread.CurrentThread.CurrentCulture.Name %></h2>
        <h2>At page load: <asp:Literal ID="PageLoadLiteral" runat="server" /></h2>
        <h2>At initialise culture: <asp:Literal ID="InitCultureLiteral" runat="server" /></h2>
        <asp:DropDownList AutoPostBack="true" runat="server" ID="LangDropDown" />
        <br />      
        Using resources processor command: <span><asp:Literal runat="server" Text="<%$ Resources: SomeResources, Banana %>" /></span>
        <br />
        GetGlobalResourceObject: <span><%= GetGlobalResourceObject ("SomeResources", "Banana") %></span>
        <br />
        Typed: <span><%= Resources.SomeResources.Banana %></span>       
        <br />      
        <br />      
        ORANGES! Using resources processor command: <span><asp:Literal runat="server" Text="<%$ Resources: SomeResources, Orange %>" /></span>
        <br />
        ORANGES! GetGlobalResourceObject: <span><%= GetGlobalResourceObject ("SomeResources", "Orange")%></span>
        <br />
        ORANGES! Typed: <span><%= Resources.SomeResources.Orange %></span>      
    </div>
    </form>
</body>
</html>

背后的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Globalization;

namespace GlobalisationResources
{
    public partial class _Default : Page
    {
        string mCulture;

        protected void Page_Load (object sender, EventArgs e)
        {
            LangDropDown.SelectedIndexChanged += LangDropDown_SelectedIndexChanged;

            if (!IsPostBack)
            {
                LangDropDown.Items.Add ("en-US");
                LangDropDown.Items.Add ("en-GB");
                LangDropDown.Items.Add ("it-IT");
            }

            PageLoadLiteral.Text = Thread.CurrentThread.CurrentUICulture.Name + " - " + Thread.CurrentThread.CurrentCulture.Name;
            InitCultureLiteral.Text = mCulture;
        }

        override protected void InitializeCulture ()
        {
            mCulture = Thread.CurrentThread.CurrentUICulture.Name + " - " + Thread.CurrentThread.CurrentCulture.Name;

            base.InitializeCulture ();
        }

        void LangDropDown_SelectedIndexChanged (object sender, EventArgs e)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture (LangDropDown.Text);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo (LangDropDown.Text);
        }
    }
}

如您所见,用户可以随时更改语言,在页面顶部我会在不同阶段打印文化,结果如下:

首先请求:

当前文化:en-US - en-GB
在页面加载时:en-US - en-GB
在初始化文化时:en-US - en-GB

选择GB时:

当前文化:en-GB - en-GB
在页面加载时:en-US - en-GB
在初始化文化时:en-US - en-GB

对于IT:

当前的文化:it-IT - it-IT
在页面加载时:en-US - en-GB
在初始化文化时:en-US - en-GB

我不知道这些结果是否会对使用Resources语法产生任何影响,但有没有人知道为什么当其他所有内容(GetGlobalResourceObject和typed)始终有效时它总会找到错误的值?

P.S。我在英国,我的地区也定在英国。

感谢。

2 个答案:

答案 0 :(得分:2)

我认为你的文化设置得不好 设置使用像...和PLZ让我知道

protected override void InitializeCulture()
{
    Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
    base.InitializeCulture();
}

答案 1 :(得分:0)

也可以以声明的方式解决此问题:

<%@ Page Language="vb" AutoEventWireup="false" Inherits="Timesheet.ProjectPoP" CodeFile="ProjectPoP.aspx.vb" Culture="auto" UICulture="auto" %>

通过设置两个属性

- 文化=&#34;自动&#34;
  - UICulture =&#34; auto&#34;

到&#34; AUTO&#34;或&#34; en-US&#34;或任何其他文化。