views:

581

answers:

2

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  A: 

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;
Bryan
Thanks, I took the sample from a video I watched. I never actually have used Request before. I believe the person said they used Request because the value may not be initialized at runtime, and SelectetdValue would throw an error.
Xaisoft
+1  A: 

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;
Zhaph - Ben Duguid