【问题标题】:changing culture(language) when button click单击按钮时更改文化(语言)
【发布时间】:2017-01-09 19:47:23
【问题描述】:

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

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

【问题讨论】:

    标签: c# asp.net resources globalization culture


    【解决方案1】:

    首先您应该将语言数据存储在 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);
    }
    

    【讨论】:

    • 如何在 cookie 中设置默认语言。我不知道使用cookie!
    • //将cookie设置为defaultLanguage HttpCookie hc = new HttpCookie("dil"); hc.Expires=DateTime.Now.AddDays(30); hc.Value="tr"; HttpContext.Current.Response.Cookies.Add(hc);
    • 不幸的是我的 c# 不支持。我使用 c# 第 4 版 =(
    • 我也想在母版页中制作这个。我不能使用 InitializeCulture()。它在页面中工作
    • 您可以在 Global.asax 文件中使用 Application_BeginRequest 方法代替 InitializeCulture
    【解决方案2】:

    经过长时间的搜索,我解决了它。这是答案和您需要的所有代码。我为 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      相关资源
      最近更新 更多