I usually set the CurrentCulture
and CurrentUICulture
by handling the PreRequestHandlerExecute
event in Global.asax.cs
(you could also handle this event in an IHttpModule
as suggested by barry).
The point is to do this in an event that takes place before the model binding takes place. There are several other events that takes place before this one which you could utilize.
Look at the HttpApplication Class for information about available events, and the order in which they are raised.
public class MvcApplication : HttpApplication
{
protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
var culture = new CultureInfo("en-GB"); // Get the culture name from the route values / request querystring / form / cookie
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
}
}
You could also do it by handling the OnActionExecuting
event on your Controller
(you probably want to create a base controller and do it there, then have all your controllers inherit from this base controller).
public class MyBaseController : Controller
{
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
var culture = new CultureInfo("en-GB"); // Get the culture name from the route values / request querystring / form / cookie
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
base.OnActionExecuting(filterContext);
}
}
public class MyController : MyBaseController
{
public ActionResult Index()
{
return View();
}
}