MVC 3设置的UICulture不起作用 [英] MVC 3 Setting uiCulture does not work

查看:132
本文介绍了MVC 3设置的UICulture不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVC3在C#中。我在Web.Config中添加了此code,其目标是在英国的格式设置的格式。

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?

推荐答案

这是在后回答的>文化变迁。你需要从BaseController继承你的控制器,覆盖BaseController的初始化方法,并使用一个Cookie。这不是我的code,但如果链接中断:

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

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

这篇关于MVC 3设置的UICulture不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆