MVC 3设置uiCulture不起作用

时间:2013-01-16 15:19:41

标签: .net asp.net-mvc globalization

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

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

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

1 个答案:

答案 0 :(得分:0)

在“Change culture based on a link 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());
}