Search code examples
c#asp.netresourcesglobalizationculture

changing culture(language) when button click


I have a website using framework 4. I made language change with global resources. on the button click code behind i use these codes.

 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;

}

an also my resx files:

-PB.resx

-PB.en-US.resx

-PB.ru-RU.resx

default language is work fine but how can i change to english and russian? where is my mistake?


Solution

  • I solve it after a long search. this is answer and all codes you need. I make this for the master page in visual studio 2010.

    You can use ispostback in page load.

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

    after then we can add button click and 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);
    
    }
    

    and last global.asax file helps solving this problem.

      //*
     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");
     }
     }
     //*
    

    If you are using html tags instead of .net tags you can use these for adding text control.

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