单击按钮时改变文化(语言)

changing culture(language) when button click

我有一个使用 framework 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

默认语言可以正常工作,但我如何更改为英语和俄语?我的错误在哪里?

首先你应该在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);
}

找了半天终于解决了。这是答案和您需要的所有代码。我在 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;
    }


}

之后我们可以添加按钮点击和 cookies

 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>