我的应用程序没有记住设置 CurrentUICulture

发布于 2024-10-17 18:58:06 字数 440 浏览 8 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守护在此方 2024-10-24 18:58:06

System.Threading.Thread.CurrentThread.CurrentUICulture中,这种方式仅为用户当前请求设置区域性,并且在后续用户请求时将重置为默认语言。

您需要在要显示基于语言的数据(本地化数据)的每个视图上设置 ui 区域性。

提示:将用户选择的文化ID保存在会话中,并在每个视图上使用它来指定UI文化,

如下所示,

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

在每个视图上设置当前线程UI文化ID

if (Session["SelectedCultureId"] != null)
{
    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Session["SelectedCultureId"]);
}

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,

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

on each view set current thread ui culture id

if (Session["SelectedCultureId"] != null)
{
    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Session["SelectedCultureId"]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文