2017-01-09 23 views
0

我有一個使用框架4的網站。我用全球資源改變了語言。在我使用這些代碼後面的按鈕點擊代碼。按鈕點擊時改變文化(語言)

protected void Button2_Click(object sender, EventArgs e) 
{ 
    dil = "en-US"; 
    var ci = new CultureInfo(dil); //TO_DO Route culture 
    Thread.CurrentThread.CurrentUICulture = ci; 
    Thread.CurrentThread.CurrentCulture = ci; 
    Session["culture"] = ci; 

} 

的也是我的resx文件:

-PB.resx

-PB.en-US.resx

-PB.ru-RU.resx

默認語言工作正常,但我怎樣才能改變爲英語和俄語?我的錯誤在哪裏?

回答

0

我解決這個問題經過長時間的搜尋後更改語言。這是答案和您需要的所有代碼。我爲Visual Studio 2010中的母版頁做好了準備。

您可以在頁面加載中使用ispostback。

protected void Page_Load(object sender, EventArgs e) 
{ 

    //only does it on non-postback because otherwise the selected 
    //value will not reach event handler correctly 
    if (!Page.IsPostBack) 
    { 
     dil = Thread.CurrentThread.CurrentCulture.Name; 
    } 


} 

那麼之後我們可以添加按鈕,點擊餅乾

protected void Button2_Click(object sender, EventArgs e) 
{ 


    dil = "en-US"; 
    //var ci = new CultureInfo(dil); //TO_DO Route culture 
    //Thread.CurrentThread.CurrentUICulture = ci; 
    //Thread.CurrentThread.CurrentCulture = ci; 
    //Session["culture"] = ci; 

    //Sets the cookie that is to be used by Global.asax 
    HttpCookie cookie = new HttpCookie("CultureInfo"); 
    cookie.Value = dil; 
    Response.Cookies.Add(cookie); 

    //Set the culture and reload the page for immediate effect. 
    //Future effects are handled by Global.asax 
    Thread.CurrentThread.CurrentCulture = 
        new CultureInfo(dil); 
    Thread.CurrentThread.CurrentUICulture = 
        new CultureInfo(dil); 
    Server.Transfer(Request.Path); 

} 

和最後一個Global.asax文件可以幫助解決這個問題。

//* 
Public void Application_BeginRequest(Object sender, EventArgs e) 
{  
// Code that runs on application startup                
HttpCookie cookie = HttpContext.Current.Request.Cookies["CultureInfo"]; 
if (cookie != null && cookie.Value != null) 
{ 
System.Threading.Thread.CurrentThread.CurrentUICulture = new 
System.Globalization.CultureInfo(cookie.Value); 
System.Threading.Thread.CurrentThread.CurrentCulture = new  
System.Globalization.CultureInfo(cookie.Value); 
} 
else 
{ 
System.Threading.Thread.CurrentThread.CurrentUICulture = new 
System.Globalization.CultureInfo("tr-TR"); 
System.Threading.Thread.CurrentThread.CurrentCulture = new 
System.Globalization.CultureInfo("tr-TR"); 
} 
} 
//* 

如果您使用的是html標籤而不是.net標籤,則可以使用這些標籤來添加文本控件。

<a><asp:Literal ID="Literal1" runat="server" Text="<%$Resources: PB, Home %>" /></a> 
0

起初,您應該將語言數據存儲在cookie中。要設置頁面語言,請重寫InitializeCulture方法。

protected override void InitializeCulture() 
    { 
     var currentLanguage= HttpContext.Current.Request.Cookies["dil"]; 
     string defaultLanguage="tr"; 
     if(currentLanguage==null) 
     { 
     //set cookie to defaultLanguage 
     } 
     else{ 
     Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(currentLanguage.Value); 
     Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; 
     } 
    } 

要通過點擊按鈕

protected void Button2_Click(object sender, EventArgs e) 
{ 
    dil = "en-US"; 
    Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(dil); 
    Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; 
    HttpCookie hc = new HttpCookie("dil"); 
    hc.Expires=DateTime.Now.AddDays(30); 
    hc.Value=dil; 
    HttpContext.Current.Response.Cookies.Add(hc); 
} 
+0

我該如何在Cookie中設置默認語言。我不知道使用cookie! – Handelika

+0

//將cookie設置爲defaultLanguage HttpCookie hc = new HttpCookie(「dil」); hc.Expires = DateTime.Now.AddDays(30); hc.Value =「tr」; HttpContext.Current.Response.Cookies.Add(hc); –

+0

不幸的是我的c#不支持。我使用C#第4版=( – Handelika