【问题标题】:Using multiple languages in asp.net mvc website在 asp.net mvc 网站中使用多种语言
【发布时间】:2016-11-29 20:30:08
【问题描述】:

我正在尝试为用户提供更改我的 asp.net mvc 网站的 UI 语言的选项。我计划向用户提供 2 种语言,即:英语和荷兰语

为此,我编辑了我的 web.config 文件并添加了:

<globalization enableClientBasedCulture="true" culture="auto" uiCulture="auto"/>

现在我添加了两个文件Resources.resx(默认文件)和Resources.nl-NL.resx(荷兰语的资源文件)。

我认为资源文件是&lt;title&gt;@WebApplication9.App_GlobalResources.Resources.RequestTitle&lt;/title&gt;。

当语言改变时,我正在像这样改变文化:

Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-NL");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("nl-NL");

但即使在语言更改为荷兰语并将文化值更改为上述指定的值后,Resources.resx 的英文值始终会显示,而不是从Resources.nl-NL.resx 获取值。

你能帮我找出我在这里缺少什么吗?

【问题讨论】:

  • 只是预感...尝试将资源文件重命名为Resources.nl.resx
  • @GeorgPatscheider :我最初尝试过,但没有奏效。
  • 初步故障排除证明,当我在浏览器中更改语言时,UI 语言正在更改。所以问题显然出在Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-NL"); 和/或Thread.CurrentThread.CurrentUICulture = new CultureInfo("nl-NL"); 我猜

标签: c# asp.net asp.net-mvc


【解决方案1】:

这可能会有所帮助;

您的 POST 方法:

public ActionResult SetCultureToNL()
    {
        SetCulture("nl-BE");

        return View("index");
    }

private void SetCulture(String culture)
    {
        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(culture);
        Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

        if (httpContext.Request.Cookies["_Culture"] != null)
        {
            HttpCookie cultureCookie = new HttpCookie("_Culture", culture);

            httpContext.Response.SetCookie(cultureCookie);
        }
        else
        {
            HttpCookie cookie = new HttpCookie("_Culture");
            cookie.Value = culture;

            httpContext.Response.Cookies.Add(cookie);
        }
    }

还有一个 GET 方法:

public ActionResult Index()
    {
        string cultureName = GetCultureCookieValue(this.ControllerContext.HttpContext.ApplicationInstance.Context);

            // Modify current thread's cultures            
            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultureName);
            Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
      // ...
   }

public static String GetCultureCookieValue(HttpContext httpContext)
    {
        HttpCookie cultureCookie = httpContext.Request.Cookies["_Culture"];

        if (cultureCookie != null)
        {
            return CultureHelper.GetImplementedCulture(cultureCookie.Value);
        }
        else
        {
            return CultureHelper.GetImplementedCulture(httpContext.Request.UserLanguages[0]); // obtain it from HTTP header AcceptLanguages
        }
    }

【讨论】:

  • 我们什么时候应该打电话给POST?
  • 当你设置文化时
【解决方案2】:

我已通过在每次调用控制器操作方法时为当前设置区域性来解决此问题。 这个问题是因为当我改变文化时,它只为当前线程设置的。当用户请求另一个视图时,线程可能会有所不同。所以我需要为每个新请求/新线程设置文化。

我让用户只有两种语言,即英语(en-US)和荷兰语(nl-NL)。所以我创建了 2 个 resx 文件,名称为 Resources.resx 用于英语,Resources.nl.resx 用于荷兰语。当用户更改当前语言时,我将语言保留在 Session 中并存储在数据库中,以便在进一步登录时记住它。

if(selectedLang == Dutch)
{
    Session["Language"] = "nl-NL";
}
else
{
    Session["Language"] = "en-US";
}

现在Session["Language"] 包含用户的首选语言。现在在每个控制器操作方法中:

[HttpGet]
public ActionResult ActionMethodName(DataClass dataObj)
{
    Sessions.ChangeLanguage(Session["Language"].ToString());
}

Sessions.ChangeLanguage(string language) 是 Sessions 类中的静态方法,如下所示:

public static void ChangeLanguage(string lang)
{
    Thread.CurrentThread.CurrentCulture = new CultureInfo(lang);
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
}

【讨论】:

  • 但是设置 CurrentCulture 不会改变所有会话的语言,而不仅仅是当前用户?
  • @userSteve:不,它仅适用于当前用户会话
猜你喜欢
  • 2010-09-22
  • 1970-01-01
  • 2010-09-16
  • 1970-01-01
  • 2012-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-20
相关资源
最近更新 更多