ASP.NET keeping the selected language in a multilingual website C#?

1.1k Views Asked by At

This is my code, the main problem is when I select the language I want to, it changes to the language, but if I click to another web page it turns to the original one. Example: I select english, then it changes all the page to english but if I click to another link in the page, the language changes to the default language.

here is the code. Hope someone can help me.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Threading;
    using System.Globalization;
    using System.Resources;

    namespace Jaltepec
    {
        public partial class MainMaster : System.Web.UI.MasterPage
        {
            protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cargaComboLenguajes();
            }
        }


        private void cargaComboLenguajes()
        {
            String lenguage = Request.QueryString["lenguaje"] == null ? "" : Request.QueryString["lenguaje"];
            if (lenguage == "en")
            {
                cmbIdioma.SelectedValue = "en-us";
            }
            else
            {
                cmbIdioma.SelectedValue = "es-mx";
            }            

        }
        protected void cmbIdioma_SelectedIndexChanged(object sender, EventArgs e)
        {
            string language = cmbIdioma.SelectedValue;

            if (!string.IsNullOrEmpty(language))
            {
                if (language.EndsWith("en-us"))
                {
                    Response.Redirect("Default.aspx?lenguaje=en");
                }
                else
                {
                    Response.Redirect("Default.aspx?lenguaje=es");
                }
            }
        }

    }
}
2

There are 2 best solutions below

4
On BEST ANSWER
    private void cargaComboLenguajes()
    {
        if (Session["cmbIdioma"].ToString() != "")
        {
            cmbIdioma.SelectedValue = Session["cmbIdioma"].ToString();

        }

        else
        {
            cmbIdioma.SelectedValue = "en-us";
            Session["cmbIdioma"] = "en-us";
        }

    }
    protected void cmbIdioma_SelectedIndexChanged(object sender, EventArgs e)
    {
        string language = cmbIdioma.SelectedValue;

        if (!string.IsNullOrEmpty(language))
        {

            Session["cmbIdioma"] = cmbIdioma.SelectedValue.ToString();
            Response.Redirect("Default.aspx?lenguaje=" + Session["cmbIdioma"].ToString());

        }
    }
1
On

Looks like you are only setting the language for that single request. You'd probably want to store the selected language in the session cookie so that you can evaluate it each page request.

Probably worth having a google of best practices when implementing globalization on an asp.net application, for example:

http://afana.me/archive/2011/01/14/aspnet-mvc-internationalization.aspx/