Question

I am learning Asp.Net globalisation and localisation, and I found this example.

The example is working fine, but when I change the month in the calendar, the calendar text is automatically changing to English.

I tried it with

<asp:Calendar ID="Calendar1" runat="server" AutoPostBack="True" 
    OnSelectedIndexChanged="language_Drp_SelectedIndexChanged">
</asp:Calendar>

But I am still having a problem. Can anyone help me?

Was it helpful?

Solution

The example you found is not a very good one. The correct place to set the culture in ASP.NET is to override the method InitializeCulture. I normally implement that method in a common base class for all my web forms.

You could implement something like this:

protected override void InitializeCulture()
{
    if (Session["locale"] != null)
    {
        string selectedLanguage = Session["locale"];

        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
    }
    base.InitializeCulture();
}

Somewhere you have to store the selected language in the Session variable, e.g. like this:

Session["locale"] = langDropdown.SelectedValue;

OTHER TIPS

I don't think the example is correct. This

protected void language_Drp_SelectedIndexChanged(object sender, EventArgs e)
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(this.language_Drp.SelectedValue);
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(this.language_Drp.SelectedValue);
Label1.Text = System.DateTime.Now.ToString();
}

should be changed to this

protected override void InitializeCulture()
{
    if (this.language_Drp.SelectedValue != null)
    {
        UICulture = this.language_Drp.SelectedValue ;
        Culture = this.language_Drp.SelectedValue ;

        Thread.CurrentThread.CurrentCulture = 
            CultureInfo.CreateSpecificCulture(selectedLanguage);
        Thread.CurrentThread.CurrentUICulture = new 
            CultureInfo(selectedLanguage);
    }
    base.InitializeCulture();
}

This code came from here. I haven't tested it and may require some tweaking but it should at least get you on your way.

it seems to be a problem of postback treatment. HTTP connections are by nature stateless.


Sorry for the previous response, it was just a test. I´ve just recalled the postback issue.

By using slfan´s code and changing the drop box event interception method to:

protected void language_Drp_SelectedIndexChanged(object sender, EventArgs e)
{
    Session["locale"] = this.language_Drp.SelectedValue;
    InitializeCulture();
}

It seems to work fine, keeping the selected culture even when selecting a specific date.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top