我在C#中使用MVC3。我在Web.Config中添加了此代码,目的是设置UK格式的格式。
....
<globalization uiCulture="en-GB" culture="en-GB"/>
</system.web>
不幸的是,文本仍以美国格式显示。
你能告诉我我在做什么错吗?
最佳答案
在“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());
}