我应该在哪里设置语言(CurrentThread.CurrentCulture)?

5

在旧版asp.net项目中,我们通常会在Application_BeginRequest处理程序(Global.asax)中设置语言,类似于以下内容:

System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cookie.Lang)

现在我正在切换到MVC 2,并决定将语言作为URL中的固定路由。 URL看起来像这样:{lang}/{controller}/{action}

我应该在哪里读取URL中的语言并设置当前区域性?如何以MVC的方式最好地完成这项工作?

谢谢任何提示!


这是我的做法:http://www.eworldui.net/blog/post/2008/05/ASPNET-MVC---Localization.aspx - rboarman
1个回答

5
在 global.asax 中类似这样的代码应该可以解决问题。
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;
}

我将用户的首选文化存储在会话状态中,该状态在此事件处理程序中首次可用 - 完美! - Dean

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接