2013-01-16 55 views
3

我在c#中使用MVC3。我已經在Web.Config中添加了這個代碼,目標是設置英國格式的格式。MVC 3設置uiCulture不起作用

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

不幸的是,文本仍然以美國格式顯示。 你能告訴我我在做什麼錯嗎?

+0

你可以檢查Thread.CurrentThread.CurrentUICulture? – free4ride

+0

你如何輸出你的字符串? – Remy

回答

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