我的应用程序没有记住设置 CurrentUICulture
我有一个 ASP.NET MVC 应用程序,我希望用户能够更改语言。我提供了一系列带有小标志的链接,让用户选择语言。所有这些链接的目标是我的“仪表板”页面,其中控制器我有以下代码:
[HttpGet]
[Authorize]
public ViewResult Dashboard(string id)
{
if (!string.IsNullOrEmpty(id))
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(id);
}
}
“仪表板”页面以所选语言显示,因为它应该是。但是当我浏览我的网站时,文化会变回英语(默认)......我错过了什么吗?更改 CurrentUICulture 是否应该将整个应用程序更改为其他语言?
I have an asp.net mvc application where i want the user to be able to change language. I have provided a series of links with small flags on to let the user choose language. The target of all these links is my "dashboard" page, in which controller i have this code:
[HttpGet]
[Authorize]
public ViewResult Dashboard(string id)
{
if (!string.IsNullOrEmpty(id))
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(id);
}
}
The "Dashboard" page is displayed in the chosen language, as it should be. But when i navigate on through my website, the culture is changed back to english (default)... am i missing something? Shouldnt changing the CurrentUICulture change the entire application to the other language?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在System.Threading.Thread.CurrentThread.CurrentUICulture中,这种方式仅为用户当前请求设置区域性,并且在后续用户请求时将重置为默认语言。
您需要在要显示基于语言的数据(本地化数据)的每个视图上设置 ui 区域性。
提示:将用户选择的文化ID保存在会话中,并在每个视图上使用它来指定UI文化,
如下所示,
在每个视图上设置当前线程UI文化ID
In System.Threading.Thread.CurrentThread.CurrentUICulture, in this way the culture is set for only current request of the user, and it will be reset to default language for subsequent user requests.
You need to set the ui culture on each view where you want to show language based data (Localized data).
Tip: save the user's selected culture id in session and use it on each view to specify the uiculture
like this,
on each view set current thread ui culture id