Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET: Changing a site's culture programmatically

Tags:

asp.net

i'm trying to set my website's culture programmatically, so when a user clicks a button they can change the text on the page from english to spanish. here's my code:

protected void btnChangeLanguage(object sender, EventArgs e)
{
    Thread.CurrentThread.CurrentCulture = new CultureInfo("es");
    Thread.CurrentThread.CurrentUICulture = new CultureInfo("es);
}

<asp:Label ID="lblDisplay" runat="server" meta:ResourceKey="lblDisplay" />

<asp:Button ID="btnChangeLanguage" runat="server" Text="Change Language"
        OnClick="btnChangeLanguage_Click" />

i have a Default.aspx.resx file with a key/value of: lblDisplay.text/English and a Default.aspx.es.resx file with a key/value of: lblDisplay.text/Espanol

i can't get my Label's text to change from "English" to "Spanish". anyone see what i'm doing wrong?

like image 898
400_the_cat Avatar asked Jul 09 '10 00:07

400_the_cat


2 Answers

ASP.Net threads are used for the lifetime of one request, not a user's entire session. Worse, sometimes the framework will recycle the same thread to handle additional requests rather than return it to the pool and get a new one (it's not that big a deal because the next request will initialize the culture again, but still).

Instead, you need to override the InitializeCulture() method for your page. See this link for more detail:
http://msdn.microsoft.com/en-us/library/bz9tc508.aspx

like image 84
Joel Coehoorn Avatar answered Nov 15 '22 23:11

Joel Coehoorn


  1. Create Session variable called "CurrentUI". and change it on link buttons event eg:

    Here i have two link buttons for each language

    protected void EnglishLinkButton_Click(object sender, EventArgs e) {
       Session["CurrentUI"] = "en-US";
       Response.Redirect(Request.Url.OriginalString);
    
    }
    
    protected void SinhalaLinkButton_Click(object sender, EventArgs e) {
       // සිංහල (ශ්‍රී ලංකා)
       Session["CurrentUI"] = "si-LK";
       Response.Redirect(Request.Url.OriginalString);
    
    }
    
  2. Now you need to override the InitializeCulture() in the base class of page

     protected override void InitializeCulture() {
         if (Session["CurrentUI"] != null) {
             String selectedLanguage = (string)Session["CurrentUI"];
             UICulture = selectedLanguage;
             Culture = selectedLanguage;
    
             Thread.CurrentThread.CurrentCulture =
                 CultureInfo.CreateSpecificCulture(selectedLanguage);
             Thread.CurrentThread.CurrentUICulture = new
                 CultureInfo(selectedLanguage);
         }
    
         base.InitializeCulture();
    }
    
  3. Note that I used

    //Response.Redirect(Request.Url.OriginalString);
    

    after assigning culture key into the session in order to create a second post back to the page. Because InitializeCulture() happens before the event and change will be applicable in the next request only.

like image 25
Ruwan Jayalath Avatar answered Nov 15 '22 22:11

Ruwan Jayalath