views:

34

answers:

2

i have 2 links for switching culture. if i click one link, the program call a method with this code:

        Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(ln);
        Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(ln);
        return RedirectToAction("index");

If i look with debug the culture is correctly changed, but in the Index redirection, the cultureUI is not changed.

I'm trying locally with visual studio web server(Cassini).

thank you

A: 

We do the same thing from links on the view. However we do not put the code in action but rather a method on a base controller class. This is the action:

public ActionResult SetCulture(string cultureCode)
    {
        string returnUrl = string.Empty;
        if (Request != null)
        {
            returnUrl = Request.UrlReferrer.ToString();
            //Write to the cookie
            Response.Cookies.Add(new HttpCookie("Language", cultureCode));
        }

        if (!String.IsNullOrEmpty(returnUrl))
        {
            return this.Redirect(returnUrl);
        }
        else
        {
            return RedirectToAction("List");
        }
    }

And this is the method in the base Controller class:

 protected override void ExecuteCore()
    {
        //Retrieve from cookies
        HttpCookie cultureCookie = Request.Cookies["Language"];
        if (cultureCookie != null)
        {
            string culture = cultureCookie.Value;
            if (!String.IsNullOrEmpty(culture))
            {
                Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(culture);
                Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(culture);
            }
        }
        base.ExecuteCore();
    }
abarr
A: 

I've resolved symply adding this method in the global.asax:

   void Application_PostAcquireRequestState(object sender, EventArgs e)
 {
   Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(languageFromSession);
   Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(languageFromSession);
}

This method is called everytime i call an action in a controller.

In this way i don't use cookies and I do not need to inherit any controller from a base controller.

I think it is the better solution.

Andrea