我目前正在从 Accept-Language-HTTP-Header 获取首选文化并将其存储在AuthUserSession中。
在 AppHost.Configure 中:
PreRequestFilters.Add((httpReq, httpResp) =>
{
var session = httpReq.GetSession();
if (session is AuthUserSession)
{
var auths = ((AuthUserSession)session);
if (auths.Culture == null)
{
//parse languages
var languages = httpReq.Headers["Accept-Language"];
//auths.Culture = Helpers.CultureHelper.GetBestAcceptLanguageMatch(languages);
auths.Culture = "en-US";
httpReq.SaveSession(session, new TimeSpan(0, 20, 0));
}
}
});
我目前在用户偏好的文化中呈现视图的解决方案是从 Razor 视图中更改当前的线程 UICulture:
@inherits ViewPage<LandingPage.ServiceModel.Operations.AskQuestions>
@{
var session = GetSession<ServiceStack.ServiceInterface.Auth.AuthUserSession>();
var prevCulture = System.Threading.Thread.CurrentThread.CurrentUICulture;
System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo(session.Culture);
//access a Resource
ViewBag.Title = Resources.AskTitle;
}
Views Content
@{
System.Threading.Thread.CurrentThread.CurrentUICulture = prevCulture;
}
这看起来不优雅和笨拙。有什么更好的方法来做到这一点?
*编辑:我正在寻找两个挂钩点:一个在视图被调用之前,一个在它被渲染之后。这些应该保持对其他请求的干扰为零。