Where should I set the language (CurrentThread.CurrentCulture)?
In older asp.net - projects we used to set the language usually within the Application_BeginRequest
- Handler (Global.asax), something like this:
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cookie.开发者_StackOverflowLang)
Now I am switching to MVC 2 and decided to keep the language as a fix route within the URL. The URL looks like this: {lang}/{controller}/{action}
How and where should I read the language from the URL and set the CurrentCulture? How is it best done the MVC - way?
Thx for any tipps!
Something like this in global.asax should work
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
MvcHandler handler = Context.Handler as MvcHandler;
if (handler == null)
return;
string lang = handler.RequestContext.RouteData.Values["lang"] as string;
CultureInfo culture = CultureInfo.GetCultureInfo(lang);
Thread.CurrentThread.CurrentUICulture = culture;
Thread.CurrentThread.CurrentCulture = culture;
}
精彩评论