货币、日历更改为所选语言,但 ASP.NET 中的标签未更改?

发布于 2024-07-15 00:46:36 字数 1815 浏览 7 评论 0 原文

我有一个带有日历的网页、一个用于保存货币值的标签和一个用于打招呼的标签。 当我从下拉列表中选择一种语言时,它会更改货币标签、日历,但 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);  
    }
}

I have an webpage with a calendar, a label to hold a currency value, and a label to say hello. When I select a language from the dropdown, it changes the currency label, the calendar, but hello does not change. Here is the stripped down code for the aspx page and the cs file:

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);  
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨夜星沙 2024-07-22 00:46:36

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

您需要手动翻译您希望本地化的文本,并将这些字符串编译成特定于语言的资源文件,然后可以通过 GetString 系统中的“ rel="nofollow noreferrer">ResourceManager 对象.资源。

// 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;

If you want the label to be localised, you'll need to look into using localised resource files for the strings (which is where the whole "Don't use string literals" best practice comes from.

You'll need to manually translate the text you wish to be localised, and compile these strings up into a language specific resource file, which can then be accessed through the GetString method of the ResourceManager object in System.Resources.

// 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;
抠脚大汉 2024-07-22 00:46:36

呃...你究竟期望发生什么? 货币和日期具有基于区域设置的内置格式。 您希望 ASP.NET 为您进行语言翻译吗?!? 抱歉,你在这方面运气不好。 :) 我错过了你的意图吗?

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

string language = Request["ddlLanguages"];

这不好...这只是 Request 对象功能的副作用,并且一旦将此代码放入命名容器(例如内容页。 改为这样做:

string language = ddlLanguages.SelectedValue;

Er... what exactly are you expecting to happen? Currency and dates have built-in formats based on locale. You want ASP.NET to do language translation for you?!? Sorry, you're out of luck on that one. :) Am I missing your intent?

Some further advice... Avoid code like this:

string language = Request["ddlLanguages"];

This is no good... this only works as a side-effect of the Request object features, and will quickly break as soon as you put this code in a naming container such as a content page. Do this instead:

string language = ddlLanguages.SelectedValue;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文