MVC 3 Setting uiCulture does not work

我的未来我决定 提交于 2019-12-10 17:52:49

问题


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?


回答1:


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

and

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

with

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


来源:https://stackoverflow.com/questions/14361709/mvc-3-setting-uiculture-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!