12

我对 MVC 中的文化变化有一种好奇心。我尝试了两种方式,但显然我在某个地方错了。

在我的 Web.config 中,我有:

 <globalization uiCulture="auto" culture="auto" />

这就是我试图改变线程文化的方式:

 <li>@Html.ActionLink("Eng", "ChangeCulture", "Home", new { lang="en-US"}, new { @class = "languageSelectorEnglish" })</li>

第一种方法

我有以下控制器:

    public void ChangeCulture(string lang)
    {
        Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(lang);
        Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang);

        Response.Cookies.Remove("Language");
        var langCookie = new HttpCookie("Language");
        langCookie["Language"] = lang;
        langCookie.Value = lang;
        langCookie.Expires = System.DateTime.Now.AddDays(21);
        Response.Cookies.Add(langCookie);

        Response.Redirect(Request.UrlReferrer.ToString());
    }

第二种方法

    public ActionResult ChangeCulture(string lang)
    {
        Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(lang);
        Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang);

        // Split the url to url + query string
        var fullUrl= Request.UrlReferrer.ToString();
        var questionMarkIndex = fullUrl.IndexOf('?');
        string queryString = null;
        string url = fullUrl;
        if (questionMarkIndex != -1) // There is a QueryString
        {
            url = fullUrl.Substring(0, questionMarkIndex);
            queryString = fullUrl.Substring(questionMarkIndex + 1);
        }

        // Arranges
        var request = new HttpRequest(null, url, queryString);
        var response = new HttpResponse(new StringWriter());
        var httpContext = new HttpContext(request, response);

        var routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext));

        // Extract the data    
        var values = routeData.Values;
        var controllerName = values["controller"];
        var actionName = values["action"];
        var areaName = values["area"];

        return RedirectToAction(actionName.ToString());
    }

我哪里错了?谢谢你。

4

2 回答 2

8

看来我需要覆盖culturefor myresource strings而不是thread. 所以我的最终方法是这样的:

    public void ChangeCulture(string lang)
    {
         Resources.Resources.Culture = new CultureInfo(lang);

         Response.Redirect(Request.UrlReferrer.ToString());
    }

希望这可以帮助。

更新 :

当您的应用程序被多个用户使用时,上面的代码并不好,因为它为每个用户设置了相同的文化,无论他们使用什么浏览器。

这样做的好方法是创建一个cookie在浏览器中设置 a 的方法:

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

在此之后(棘手的方法),您需要使每个控制器都从一个 BaseController 继承。这很棘手,因为您需要覆盖Initialize.

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);
    }
于 2013-02-27T10:20:17.740 回答
4

删除 web.config 中的行:

<globalization uiCulture="auto" culture="auto" />

将这些设置为 auto 会将语言默认为浏览器上设置的用户语言。这是覆盖您的线程设置。

于 2013-02-27T09:41:57.397 回答