Search code examples
.netasp.net-mvcglobalization

MVC 3 Setting uiCulture does not work


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?


Solution

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