Question

I am implementing a way for a user to toggle the language shown (en || es). I have the following:

<asp:LinkButton ID="Lnk_cultChange" runat="server" Text="<%$Resources:mySource, cultbtn%>" OnClick="cultChange_Click" />

Code behind:

protected void cultChange_Click(object sender, EventArgs e)
{
    if (CultureInfo.CurrentUICulture.TwoLetterISOLanguageName == "en")
    {
        System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-ES");
        System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("es-ES");
        base.InitializeCulture();
    }
    else
    {
        System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
        System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
        base.InitializeCulture();
    }
}

There is a postback, but nothing appears to happen. The English resource file is still displaying and no translation is made. Is my implementation correct?

Was it helpful?

Solution 3

Just like @Umriyaev suggested you have to override the InitializeCulture()and use that to pull the culture after another redirection. See here https://stackoverflow.com/a/10672476/2596756

OTHER TIPS

Toggle the culture on page PreInit event, if you want to toggle the culture only on click of a particular button then use code as following.

protected override void OnPreInit(EventArgs e)
{
    if (Page.IsPostBack)
    {
        if (Request.Form["__EVENTTARGET"] == "Lnk_cultChange")
        {
            if (CultureInfo.CurrentUICulture.TwoLetterISOLanguageName == "en")
            {
                System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-ES");
                System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("es-ES");
                base.InitializeCulture();
            }
            else
            {
                System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
                System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                base.InitializeCulture();
            }
        }

    }

    base.OnPreInit(e);
}

The Request.Form["__EVENTTARGET"] will be the client ID of your LinkButton, Let me know if this will not work

Put your change lang buttons in the site master. Add the click event handlers like following

protected void btnEng_Click(object sender, EventArgs e)
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("en");
    Session.Add("Lang", "en");
    Response.Redirect(Request.Url.PathAndQuery);
}

And in your pages' code behind add the following method:

protected override void InitializeCulture()
    {
        if (Session["Lang"] != null)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Session["Lang"].ToString().Split('-')[0]);
            Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(Session["Lang"].ToString().Split('-')[0]);
        }
        else
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name);
            Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name);
        }
        base.InitializeCulture();
    }

Hope that helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top