Colonel_Custard
Colonel_Custard

Reputation: 1390

How to change my MVC Website's Current Culture at Runtime

I have a problem with my MVC Project where I need users to be able to change the localization of the site at runtime, my code below is what I've tried so far, and surely enough the current culture does change, but I'm seeing no changes in my site.

Strangely, setting the culture in Web.Config works fine!

my code is below, any ideas at all?

[AllowAnonymous]
        [HttpPost]
        public ActionResult SelectLanguage(LoginViewModel model)
        {
            switch (model.SelectedLanguage)
            {
                case "French":
                    CultureInfo.CurrentCulture=new CultureInfo("fr-fr");
                    CultureInfo.CurrentUICulture = new CultureInfo("fr-fr");
                    break;
            }

            return RedirectToAction("Index");

        }

Upvotes: 0

Views: 5858

Answers (2)

Rajan Kali
Rajan Kali

Reputation: 12953

The good way to do it is to make a method which sets a cookie in your browser :

public void ChangeCulture(string lang)
{
    Response.Cookies.Remove("Language");

    HttpCookie languageCookie = System.Web.HttpContext.Current.Request.Cookies["Language"];

    if (languageCookie == null) languageCookie = new HttpCookie("Language");

    languageCookie.Value = lang;

    languageCookie.Expires = DateTime.Now.AddDays(10);

    Response.SetCookie(languageCookie);

    Response.Redirect(Request.UrlReferrer.ToString());
}

After this ( the tricky way ) you need to make every controller to inherit from one BaseController. It is tricky because you need to override Initialize.

protected override void Initialize(System.Web.Routing.RequestContext requestContext)
    {


        HttpCookie languageCookie = System.Web.HttpContext.Current.Request.Cookies["Language"];
        if (languageCookie != null)
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCookie.Value);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(languageCookie.Value);
        }
        else
        {
        //other code here
        }


        base.Initialize(requestContext);
    }

and in your Method call ChangeCulture() with lang

[AllowAnonymous]
        [HttpPost]
        public ActionResult SelectLanguage(LoginViewModel model)
        {
            switch (model.SelectedLanguage)
            {
                case "French":
                    ChangeCulture("fr-Fr");
                    break;
            }

            return RedirectToAction("Index");

        }

Upvotes: 2

Colonel_Custard
Colonel_Custard

Reputation: 1390

this question Change culture based on a link MVC4 helped a lot!

Basically I needed to override the culture for my resource strings, not the actual application's Culture!

my code has been changed to this;

 [AllowAnonymous]
        [HttpPost]
        public ActionResult SelectLanguage(LoginViewModel model)
        {
            switch (model.SelectedLanguage)
            {
                case "French":
                    LanguageStrings.Culture = new CultureInfo("fr-fr");
                    break;
            }

            return RedirectToAction("Index");

        }

and works like a Charm! =D

Upvotes: 0

Related Questions