3

我在 C# 中使用 MVC3。我在 Web.Config 中添加了这段代码,目的是设置英国格式的格式。

....
        <globalization uiCulture="en-GB" culture="en-GB"/>
    </system.web>

不幸的是,文本仍然以美国格式显示。你能告诉我我在这里做错了什么吗?

4

1 回答 1

1

这在“基于链接 MVC4 改变文化”一文中得到了回答。您需要从 BaseController 继承您的 Controller,覆盖 BaseController 的 Initialize 方法,并使用 cookie。这不是我的代码,但如果链接中断:

protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
    HttpCookie languageCookie = System.Web.HttpContext.Current.Request.Cookies["Language"];
    if (languageCookie != null)
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCookie.Value);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(languageCookie.Value);
    }
    else
    {
    //other code here
    }

    base.Initialize(requestContext);
}

<li>@Html.ActionLink("Eng", "ChangeCulture", "Home", new { lang="en-US"}, new { @class = "languageSelectorEnglish" })</li>

public void ChangeCulture(string lang)
{
    Response.Cookies.Remove("Language");

    HttpCookie languageCookie = System.Web.HttpContext.Current.Request.Cookies["Language"];

    if (languageCookie == null) languageCookie = new HttpCookie("Language");

    languageCookie.Value = lang;

    languageCookie.Expires = DateTime.Now.AddDays(10);

    Response.SetCookie(languageCookie);

    Response.Redirect(Request.UrlReferrer.ToString());
}
于 2015-02-03T15:20:43.440 回答