I have a masterpage with a language selector dropdownlist
it has multiple subpages using the masterpage but, in the subpages (i created a basePage class which i then let the pages inherit from) i override the initializeCulture. like this:
protected override void InitializeCulture()
{
String selectedLanguage = Common.SessionManager.Language;
if (selectedLanguage == "")
{
selectedLanguage = ConfigurationManager.AppSettings.Get("defaultLanguage");
}
if (selectedLanguage == "")
{
selectedLanguage = "nl-BE";
}
UICulture = selectedLanguage;
Culture = selectedLanguage;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
base.InitializeCulture();
}
on the SelectedIndexChanged event of the dropdownlist, i set the new language in the session like this:
protected void LanguageSelectorSelectedIndexChanged(object sender, EventArgs e)
{
string sCulture = LanguageSelector.SelectedValue;
Common.SessionManager.Language = sCulture;
}
but the initializeCulture has then already been fired.
so i have sort of a delay effect, page loads with previous language, and on the next postback its translated correctly.
i cannot call the initializeC开发者_运维百科ulture again, because i'm on a masterpage and i have no access to the subpage's basePage class.
anyone got an idea how to tackle this?
You could try to get the selected language by form posted values:
protected override void InitializeCulture()
{
String selectedLanguage = Common.SessionManager.Language;
if (Request.Form.ContainsKey(myLanguageDropDown.ClientID)
selectedLanguage = Request.Form[myLanguageDropDown.ClientID];
if (selectedLanguage == "")
{
...
You can't use the event handler for the dropdownlist because that happens after InitializeCulture()
.
InitializeCulture()
happens before the request values are loaded into the form controls.
So the correct way to obtain the value from the dropdownlist is to NOT use the event handler, and use Request.Form["yourddlid"]
inside InitializeCulture()
to get the selected value.
My solution in this case is to redirect the page to itself after changing the language.
In the same vein of the "Redirect to itself" answer you could use Server.Transfer() instead of Redirect, avoiding a round-trip to the client. Something like this (consider it's in the Default.aspx page):
protected override void InitializeCulture()
{
if (Session["LCID"] != null)
{
int lcid = (int)Session["LCID"];
CultureInfo c = new CultureInfo(lcid);
Thread.CurrentThread.CurrentCulture = c;
}
base.InitializeCulture();
}
protected void comboCultures_SelectedIndexChanged(object sender, EventArgs e)
{
CultureInfo c = new CultureInfo(Thread.CurrentThread.CurrentCulture.LCID);
if (comboCultures.SelectedItem != null)
c = CultureInfo.GetCultureInfo(Convert.ToInt32(comboCultures.SelectedItem.Value));
Session["LCID"] = c.LCID;
Server.Transfer("Default.aspx");
}
I've stored the LCID of the culture in the combo box values, but this is not important. The heart of the technique is to user Server.Transer(pagename) so that the page workflow is reinitiated and the Page.InitializeCulture has a chance to get the "current" values from the Session.
protected override void InitializeCulture(){
Page.UICulture = Request.Form["ddlLanguage"];
}
精彩评论