货币、日历更改为所选语言,但 ASP.NET 中的标签未更改?
我有一个带有日历的网页、一个用于保存货币值的标签和一个用于打招呼的标签。 当我从下拉列表中选择一种语言时,它会更改货币标签、日历,但 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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望标签本地化,您需要考虑使用字符串的本地化资源文件(这就是整个“不要使用字符串文字”最佳实践的来源。
您需要手动翻译您希望本地化的文本,并将这些字符串编译成特定于语言的资源文件,然后可以通过 GetString 系统中的“ rel="nofollow noreferrer">ResourceManager 对象.资源。
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.
呃...你究竟期望发生什么? 货币和日期具有基于区域设置的内置格式。 您希望 ASP.NET 为您进行语言翻译吗?!? 抱歉,你在这方面运气不好。 :) 我错过了你的意图吗?
一些进一步的建议...避免这样的代码:
这不好...这只是 Request 对象功能的副作用,并且一旦将此代码放入命名容器(例如内容页。 改为这样做:
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:
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: