Currency, Calendar changes to selected language, but not label in asp.net?

僤鯓⒐⒋嵵緔 提交于 2019-12-24 10:44:36

问题


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

回答1:


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;



回答2:


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;


来源:https://stackoverflow.com/questions/608226/currency-calendar-changes-to-selected-language-but-not-label-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!