某些视图中未检测到语言更改

时间:2017-02-04 13:17:44

标签: c# asp.net-mvc asp.net-mvc-5 localization globalization

我开始开发多语言网站并通过查看Youtube视频我编码了。在我的索引页面它工作得很好。但我不知道如何使用它关于我们页面(我的第二个视图)。你能不能请帮我解决这个问题。 /这是MVC网站。

我的网站可以像这样访问
http://localhost:10228< - 对于英语
http://localhost:10228/Home/ChangeLanguage?lang=hi< - 印地语

  
    

问题是当我转到另一个像AboutUs这样的视图时它没有检测到语言。(总是以英文显示)

  

Global.asax中

  protected void Application_AquireRequestState(object sender, EventArgs e)
    {
        HttpContext context = HttpContext.Current;
        if (context != null && context.Request.Cookies != null)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["_lang"];
            if (cookie != null && cookie.Value != null)
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cookie.Value);
                Thread.CurrentThread.CurrentCulture = new CultureInfo(cookie.Value);
            }
        }
    }

_Layout.cshtml

@using InNews.App_GlobalResources


@Html.ActionLink(multilanguage.English, "ChangeLanguage", "Home", new { lang = "en" }, null)
@Html.ActionLink(multilanguage.Hindi, "ChangeLanguage", "Home", new { lang = "hi" }, null)
@Html.ActionLink(multilanguage.Japanese, "ChangeLanguage", "Home", new { lang = "jp" }, null)

_MenuPartial.cshtml(这将呈现给布局页面)

 @using InNews.App_GlobalResources
 <li>@Html.ActionLink(multilanguage.Home, "Index", "Home")</li>
 <li>@Html.ActionLink(multilanguage.AboutUs, "AboutUs", "Home")</li>

的HomeController

  public ActionResult Index()
    {
        return View();
    }

   public ActionResult ChangeLanguage(string lang)
    {
        if (lang != null)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
        }
        else
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en");
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");
        }

        HttpCookie cookie = new HttpCookie("_lang");
        cookie.Value = lang;
        Response.Cookies.Add(cookie);

        return View("Index");
    }



public ActionResult AboutUs()
        {
            return View();
        }

请你帮我解决这个问题。

0 个答案:

没有答案