gpt4 book ai didi

asp.net-mvc - 我的应用程序没有记住设置 CurrentUICulture

转载 作者:行者123 更新时间:2023-12-04 06:34:23 24 4
gpt4 key购买 nike

我有一个 asp.net mvc 应用程序,我希望用户能够更改语言。我提供了一系列带有小标志的链接,让用户选择语言。所有这些链接的目标是我的“仪表板”页面,其中 Controller 有以下代码:

[HttpGet]
[Authorize]
public ViewResult Dashboard(string id)
{
if (!string.IsNullOrEmpty(id))
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(id);
}
}

“仪表板”页面以所选语言显示,应该是这样。但是当我浏览我的网站时,文化又改回英语(默认)……我错过了什么吗?不应该更改 CurrentUICulture 将整个应用程序更改为其他语言吗?

最佳答案

System.Threading.Thread.CurrentThread.CurrentUICulture ,通过这种方式,文化只为用户的当前请求设置,并且它将被重置为后续用户请求的默认语言。

您需要在要显示基于语言的数据(本地化数据)的每个 View 上设置 ui 文化。

提示:在 session 中保存用户选择的文化 id 并在每个 View 上使用它来指定 uiculture

像这样,

// save users selected culture id in session
Session["SelectedCultureId"] = id;

在每个 View 上设置当前线程 ui 文化 ID
if (Session["SelectedCultureId"] != null)
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Session["SelectedCultureId"]);
}

关于asp.net-mvc - 我的应用程序没有记住设置 CurrentUICulture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5015525/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com