2014-09-23 57 views
0

我想設置一個cookie來更改我的網站的文化和用戶界面,當用戶單擊切換按鈕時。這是我有:設置語言cookie在resx頁面之間切換

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!Page.IsPostBack) 
    { 
     HttpCookie cookie = Request.Cookies["CurrentLanguage"]; 
     if (!IsPostBack && cookie != null && cookie.Value != null) 
     { 
      if (cookie.Value.IndexOf("en-") >= 0) 
      { 
       // currently english 
       Culture = "fr-CA"; 
       UICulture = "fr-CA"; 
      } 
      else 
      { 
      //currently french 
      Culture = "en-CA"; 
      UICulture = "en-CA"; 
      } 
     } 
     ... 
    } 
} 

protected void toggle_lang(object sender, DirectEventArgs e) 
    { 
     if (Culture.ToString() == "English (Canada)") 
     { 
      HttpCookie cookie = new HttpCookie("CurrentLanguage"); 
      cookie.Value = "fr-CA"; 
      Response.SetCookie(cookie); 
      Response.Redirect(Request.RawUrl); 
     } 
     else 
     { 
      HttpCookie cookie = new HttpCookie("CurrentLanguage"); 
      cookie.Value = "en-CA"; 
      Response.SetCookie(cookie); 
      Response.Redirect(Request.RawUrl); 
     } 
    } 

當執行切換功能,頁面刷新,但文化和cultureui沒有更新..


任何想法?



謝謝!

+0

試'文化= GetCultureInfo(your_lang);'insted的的'文化= 「EN-CA」;' 和 使用System.Threading.Thread.CurrentThread.'CurrentUICulture'insted o f'UICulture'。 我建議你使用中性文化,這可以在將來重新解決問題。 – Svmurvj 2014-09-23 14:16:48

回答

0

如果有人想看看我的代碼,根據cookie中的文化和UI文化設置,你需要重寫InitializeCulture()這樣的:

protected override void InitializeCulture() 
    { 

     HttpCookie cookie = Request.Cookies["CurrentLanguage"]; 
     if (!IsPostBack && cookie != null && cookie.Value != null) 
     { 
      if (cookie.Value.ToString() == "en-CA") 
      { 
       // currently english 
       System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-CA"); 
       System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-CA"); 
       base.InitializeCulture(); 
      } 
      else 
      { 
       //currently french 
       System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-CA"); 
       System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr-CA"); 
       base.InitializeCulture(); 
      } 
     } 
    } 

還可以切換,設置新的cookie和刷新,讓上述功能拍攝設置頁面的語言,這樣的護理:

protected void toggle_lang(object sender, DirectEventArgs e) 
    { 
     if (Culture.ToString() == "English (Canada)") 
     { 
      HttpCookie cookie = new HttpCookie("CurrentLanguage"); 
      cookie.Value = "fr-CA"; 
      Response.SetCookie(cookie); 
      Response.Redirect(Request.RawUrl); 

     } 
     else 
     { 
      HttpCookie cookie = new HttpCookie("CurrentLanguage"); 
      cookie.Value = "en-CA"; 
      Response.SetCookie(cookie); 
      Response.Redirect(Request.RawUrl); 
     } 

    }