Save user selected Language in Cookies in MultiLingual Website ASP.Net

Last Reply on Jul 31, 2015 10:02 AM By dharmendr

Posted on Jul 31, 2015 06:06 AM

i have read your article on aspsnippets.com

Multilingual Website

and i have implemented that i works good .

but there is new problem arrising.

suppose my default language is english set in internet option so the whenever website reloaded or redirected then all content will show in english language but

when i changed it to hindi then current webpage converted to hindi but after that if i pressed any button on current page or i redirected to any other link of particular website then again hindi is converted to english.
so please give me solution for this so that untill i didn't change the language ,, all pages of that website langauge is same.

 

Posted on Jul 31, 2015 10:02 AM Modified on on Aug 03, 2015 03:45 AM

Hi Prasunjeet,

You need to change BasePage Code and Master Page.

BasePage

protected override void InitializeCulture()
{
    string language = "en-ca";
    if (HttpContext.Current.Request.Cookies["Language"] != null)
    {
        HttpCookie cookie = HttpContext.Current.Request.Cookies["Language"];
        if (cookie != null)
        {
            language = cookie.Value.Split('=')[1];
        }
    }

    //Check if PostBack is caused by Language DropDownList.
    if (Request.Form["__EVENTTARGET"] != null && Request.Form["__EVENTTARGET"].Contains("ddlLanguages"))
    {
        //Set the Language.
        language = Request.Form[Request.Form["__EVENTTARGET"]];
    }

    //Set the Culture.
    Thread.CurrentThread.CurrentCulture = new CultureInfo(language);
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
}

MasterPage

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string language = "en-ca";
        if (HttpContext.Current.Request.Cookies["Language"] != null)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["Language"];
            if (cookie != null)
            {
                language = cookie.Value.Split('=')[1];
            }
        }
        this.ddlLanguages.Items.FindByValue(language).Selected = true;
    }
}

protected void ChangeLanguage(object sender, EventArgs e)
{
    HttpCookie languageCookie = new HttpCookie("Language");
    languageCookie.Values["LanguageCode"] = this.ddlLanguages.SelectedValue;
    languageCookie.Expires = DateTime.Now.AddDays(30);
    HttpContext.Current.Response.Cookies.Add(languageCookie);
    this.Response.Redirect(Request.Url.AbsoluteUri);
}

Note : your each page must inherit BasePage.

public partial class _Default : BasePage

I hope this will help you out.

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html