views:

298

answers:

3

i have multilingual asp.net site. there is a masterpage and default.aspx.

I put two buttons in masterpage - one to click when i want to change the language to english, second for polish. Of course, I want to change the language after click on these buttons (and all changes should appear automatically on the page)

here is a code for both:

protected void EnglishButton_Click(object sender, ImageClickEventArgs e)
{
    string selectedLanguage = "en-US";
    //Sets the cookie that is to be used by InitializeCulture() in content page
    HttpCookie cookie = new HttpCookie("CultureInfo");
    cookie.Value = selectedLanguage;
    Response.Cookies.Add(cookie);
    Server.Transfer(Request.Path);
}

protected void PolishButton_Click(object sender, ImageClickEventArgs e)
{
    string selectedLanguage = "pl-PL";
    //Sets the cookie that is to be used by InitializeCulture() in content page
    HttpCookie cookie = new HttpCookie("CultureInfo");
    cookie.Value = selectedLanguage;
    Response.Cookies.Add(cookie);
    Server.Transfer(Request.Path);
}

in default.aspx.cs i have InitializeCulture():

protected override void InitializeCulture()
{
    HttpCookie cookie = Request.Cookies["CultureInfo"];

    // if there is some value in cookie
    if (cookie != null && cookie.Value != null)
    {
        Thread.CurrentThread.CurrentCulture =
            CultureInfo.CreateSpecificCulture(cookie.Value);
        Thread.CurrentThread.CurrentUICulture = new
            CultureInfo(cookie.Value);
    }
    else // if none value has been sent by cookie, set default language
    {
        Thread.CurrentThread.CurrentCulture =
            CultureInfo.CreateSpecificCulture("pl-PL");
        Thread.CurrentThread.CurrentUICulture = new
            CultureInfo("pl-PL");
    }

    base.InitializeCulture();
}

i added resource files and in one label i show actual culture:

Welcome.Text = "Culture: " + System.Globalization.CultureInfo.CurrentCulture.ToString();

the problem is that when i run this app and click e.g. english button (default language is polish), there is no effect. if i click it second time or press F5, the changes are applied and in the label the Culture is displayed as en-US. Tsame happens if i want to change language back to polish (it works after second click (or one click and refresh)).

What am i doing wrong?

A: 

When you use Server.Transfer there everything happens within the same web request - it is just an internal server processing redirect. Try to use Response.Redirect instead of Server.Transfer. This will force the client browser to invoke another request and then the cookie will be sent to the web server.

twk
A: 

I suspect the problem might be in your use of Server.Transfer in your button click event. Have you tried using Response.Redirect instead?

Thomas
thanks, it helped!
Bart
A: 

Why are you using Cookies? Why not Session? I think Session is more convenient and you wouldn't have that problem. When InitializeCulture() starts after Server.Transfer it's still the same request still without English Cookie. You put it in Response but Response is still here, on the server side.
Regarding your internationalization solution. It is usually advised to have locale embedded in URL. Consider an English user who clicked on the English button and then sends a link to a friend. His friend opens the link and can't understand a word. He doesn't have the English Cookie, so he is viewing Polish page.
Hope this helps

Art Shayderov