MVC 3设置的UICulture不起作用不起作用、MVC、UICulture

2023-09-05 04:10:34 作者:少女的祈祷

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

I'm using MVC3 in c#. I have added in Web.Config this code, with the goal to set the formatting of in UK format.

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

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

Unfortunately the text are still displayed in US format. Could you tell me what I'm doing wrong here?

推荐答案

这是在后回答的文化变迁。你需要从BaseController继承你的控制器,覆盖BaseController的初始化方法,并使用一个Cookie。这不是我的code,但如果链接中断:

This was answered in the post "Change culture based on a link MVC4". You need to inherit your Controller from a BaseController, override the BaseController's Initialize method, and use a cookie. This is not my code, but in case that link breaks:

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());
}