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?