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