开发者

Where should I set the language (CurrentThread.CurrentCulture)?

开发者 https://www.devze.com 2023-02-09 20:59 出处:网络
In older asp.net - projects we used to set the language usually within the Application_BeginRequest - Handler (Global.asax), something like this:

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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消