I have an MVC3 application using the razor view engine and twitter-bootsrap. In my _Layout.cshtml file, I have a dropdown with a list of supported languages and have it's onchange wired to the following code:
public ActionResult ChangeLanguage(string langue)
{
CultureInfo ci = new CultureInfo(langue);
System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
System.Threading.Thread.CurrentThread.CurrentCulture = ci;
return View("Index", "_Layout");
}
It works fine, but currently always return the Index view. I would like to return whatever view was currently displayed when the user changed language. I tried:
My question is in 2 parts:
You could use the Request.ServerVariables["HTTP_REFERER"]
to get the page referring page.
public ActionResult ChangeLanguage(string langue)
{
CultureInfo ci = new CultureInfo(langue);
System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
System.Threading.Thread.CurrentThread.CurrentCulture = ci;
var referer = Request.ServerVariables["HTTP_REFERER"];
return Redirect(referer);
}
The way I've handled this in the past was to change the culture using an ActionFilterAttribute
which I would apply to all controllers.
public sealed class CultureActionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
object value;
// get culture
filterContext.RouteData.TryGetValue("culture", out value);
var cultureName = value as string;
if (cultureName == null)
{
cultureName = // resolve your default culture
}
var culture = new CultureInfo(cultureName);
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
}
}
(I had route constraints verify the culture before this actionfilter took affect. If you don't want to do that... because you're using querystring parametrs... the validation should probably go in this method.)