货币,日历更改所选的语言,但没有标注在asp.net? [英] Currency, Calendar changes to selected language, but not label in asp.net?

查看:111
本文介绍了货币,日历更改所选的语言,但没有标注在asp.net?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日历,标签持有货币值和标签打招呼的网页。当我从下​​拉列表中选择一种语言,它改变了货币的标签,日历,但招呼不会改变。这里是剥离下来code的aspx页面和CS文件:

ASPX:

 < ASP:标签ID =lblLanguageSelection=服务器
           文本=选择语言:>< / ASP:标签>
    < ASP:DropDownList的ID =ddlLanguages​​=服务器的AutoPostBack =真>
    < ASP:ListItem的值=自动>自动< / ASP:ListItem的>
    < ASP:ListItem的值=EN-US>英语(美国)LT; / ASP:ListItem的>
    < ASP:ListItem的值=EN-GB>英语(GB)LT; / ASP:ListItem的>
    < ASP:ListItem的值=德>德语< / ASP:ListItem的>
    < ASP:ListItem的值=FR>法语< / ASP:ListItem的>
    < ASP:ListItem的值=FR-CA>法语(加拿大)LT; / ASP:ListItem的>
    < ASP:ListItem的值=喜>与印地文LT; / ASP:ListItem的>
    < ASP:ListItem的值=TH>泰国< / ASP:ListItem的>
    < / ASP:DropDownList的>
    < BR />< BR />
    < ASP:日历ID =CALENDAR1=服务器>< / ASP:日历>
    < BR />< BR />
    < ASP:标签ID =lblCurrency=服务器>< / ASP:标签>
    < BR />< BR />
    < ASP:标签ID =lblHello=服务器>< / ASP:标签>

CS:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    十进制货币= 65542.43M;
    字符串Hello =你好;    lblCurrency.Text =的String.Format({0:C},货币);
    lblHello.Text =的String.Format({0}你好);
}保护覆盖无效InitializeCulture()
{
    String语言=请求[ddlLanguages​​];    如果(语言!= NULL)
    {
        Thread.CurrentThread.CurrentUICulture =新的CultureInfo(语言);
        Thread.CurrentThread.CurrentCulture =
                             CultureInfo.CreateSpecificCulture(语言);
    }
}


解决方案

如果您想进行本地化的标签,你需要考虑使用本地化的资源文件的字符串(这是整个不使用字符串最佳实践得来的。

您需要到要进行本地化文本翻译手动,最多编译这些字符串放到特定语言的资源文件,然后可以通过的的GetString 方法=http://msdn.microsoft.com/en-us /library/system.resources.resourcemanager.aspx相对=nofollow> ResourceManager的中的在System.Resources

  //创建一个资源管理器来获取资源。
ResourceManager的RM =新的ResourceManager(项目,
        Assembly.GetExecutingAssembly());//检索一个名为你好的字符串资源的价值。
//资源管理器将获取的价值
//使用调用者的当前区域性设置本地化资源。
字符串Hello = rm.GetString(你好);
lblHello.Text =您好;

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

解决方案

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;

这篇关于货币,日历更改所选的语言,但没有标注在asp.net?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆