1

我有一个带有日历的网页,一个保存货币值的标签和一个打招呼的标签。当我从下拉列表中选择一种语言时,它会更改货币标签、日历,但 hello 不会更改。这是 aspx 页面和 cs 文件的精简代码:

ASPX:

<asp:Label ID="lblLanguageSelection" runat="server" 
           Text="Select a language: "></asp:Label>
    <asp:DropDownList ID="ddlLanguages" runat="server" AutoPostBack="true">
    <asp:ListItem Value="auto">Auto</asp:ListItem>
    <asp:ListItem Value="en-US">English (US)</asp:ListItem>
    <asp:ListItem Value="en-GB">English (GB)</asp:ListItem>
    <asp:ListItem Value="de">German</asp:ListItem>
    <asp:ListItem Value="fr">French</asp:ListItem>
    <asp:ListItem Value="fr-CA">French (Canada)</asp:ListItem>
    <asp:ListItem Value="hi">Hindi</asp:ListItem>
    <asp:ListItem Value="th">Thai</asp:ListItem>
    </asp:DropDownList>
    <br /><br />
    <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
    <br /><br />
    <asp:Label ID="lblCurrency" runat="server"></asp:Label>
    <br /><br />
    <asp:Label ID="lblHello" runat="server"></asp:Label>

CS:

protected void Page_Load(object sender, EventArgs e)
{
    decimal currency = 65542.43M;
    string hello = "Hello";

    lblCurrency.Text = string.Format("{0:c}", currency);
    lblHello.Text = string.Format("{0}",hello);
}

protected override void InitializeCulture()
{
    string language = Request["ddlLanguages"];

    if (language != null)
    {
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
        Thread.CurrentThread.CurrentCulture = 
                             CultureInfo.CreateSpecificCulture(language);  
    }
}
4

2 回答 2

1

呃……你到底希望发生什么?货币和日期具有基于区域设置的内置格式。你想让 ASP.NET 为你做语言翻译?!?对不起,你在那个方面不走运。:) 我错过了你的意图吗?

一些进一步的建议......避免这样的代码:

string language = Request["ddlLanguages"];

这不好...这仅作为 Request 对象功能的副作用,并且一旦您将此代码放入命名容器(例如内容页面)中就会迅速中断。改为这样做:

string language = ddlLanguages.SelectedValue;
于 2009-03-03T21:27:51.977 回答
1

如果您希望标签本地化,您需要考虑使用本地化的资源文件来处理字符串(这是整个“不使用字符串文字”最佳实践的来源。

您需要手动翻译您希望本地化的文本,并将这些字符串编译成特定语言的资源文件,然后可以通过System.Resources中ResourceManager对象的GetString方法访问该文件。

// Create a resource manager to retrieve resources.
ResourceManager rm = new ResourceManager("items", 
        Assembly.GetExecutingAssembly());

// Retrieve the value of the string resource named "hello".
// The resource manager will retrieve the value of the  
// localized resource using the caller's current culture setting.
String hello = rm.GetString("hello");
lblHello.Text = hello;
于 2009-03-04T12:22:28.517 回答