我有一个web应用程序,它使用本地化来显示英语或法语为我们的法国加拿大客户。
根据用户的区域设置,它工作得很好。但是,如果用户的区域设置设置为法语,我们需要允许用户切换回英语。
如果用户愿意,是否可以覆盖用户的区域设置?如果是so...how,我会对此进行编码吗?(例如,在布局页面上有一个显示为英语的链接,单击此链接会将其更改回英语或法语)
另外,我使用资源文件保存文本字符串,并使用相同的视图集。
发布于 2012-11-14 17:35:22
在代码中的某处,当他们单击一个按钮来选择一种语言时:
Session["customLocalization"] = "de-DE"; //Or whatever language
在你的Global.asax中
protected void Application_BeginRequest(object sender, EventArgs e)
{
String sessionOverrideLocale;
if (HttpContext.Current != null && HttpContext.Current.Session != null)
{
sessionOverrideLocal = (String) HttpContext.Current.Session["customLocalization"];
}
if (sessionOverrideLocale != null)
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo(sessionOverrideLocale);
Thread.CurrentThread.CurrentCulture = new CultureInfo(sessionOverrideLocale);
}
}
发布于 2012-11-14 17:36:04
是的,这是可能的,但我目前不能访问我最近工作的代码,我们允许用户更改他们当前呈现的区域。
这里有一篇博客文章,详细介绍了如何做到这一点。他有一个更老的帖子,我相信它使用的是MVC3,而这个新的帖子是从MVC4的角度编写的,所以这篇文章应该已经涵盖了。
我希望它能有所帮助:
http://geekswithblogs.net/shaunxu/archive/2012/09/04/localization-in-asp.net-mvc-ndash-upgraded.aspx
https://stackoverflow.com/questions/13383618
复制