2014-09-23 3 views
0

사용자가 토글 버튼을 클릭 할 때 내 웹 사이트의 문화 및 사용자 문화를 변경하기 위해 쿠키를 설정하려고합니다.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가 업데이트되지 않습니다 ..


어떤 아이디어 : 이것은 내가 무엇을 가지고 ?



고마워요! 사람이 문화 및 UI 문화는 쿠키에 따라 설정, 내 코드를보고 싶어하면

+0

시도 : 그리고 위의 함수는 다음과 같이 페이지 언어를 설정 돌봐셔서, 새로 고침 System.Threading.Thread.CurrentThread.'CurrentUICulture 'insted o f 'UICulture'. 중립 문화를 사용하는 것이 좋습니다. 장래에 문제를 일으킬 수 있습니다. – Svmurvj

답변

0

,이 같은 InitializeCulture()를 오버라이드 (override) 할 필요가 :

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(); 
      } 
     } 
    } 

을 그리고 전환, 새로운 쿠키를 설정 `문화 = "EN-CA"`의 insted;` 및 사용

문화 = GetCultureInfo (your_lang가)`
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); 
     } 

    }