改变语言不要"触发"资源文件

时间:2016-06-07 07:20:37

标签: c# asp.net-mvc

我现在花了几个小时,查看stackoverflow,我找不到解决问题的答案。

我试图为网站添加多种语言支持,这就是我的代码。

<a class="heading-line" href="/">@ISmile.Resources.HomeText.Home</a>
<a class="heading-line" href="/shop">@ISmile.Resources.HomeText.All_brands</a>

我的资源文件名为HomeText.resxHomeText.dk.resx

到目前为止,它们包含了这个:

Name     -     Value
Home           Home
All_brands     AllBrands

HomeText.dk.resx相同,但是丹麦语中的值字段。

当我点击我的下拉菜单并选择一种语言时,它会在我的控制器中触发一个动作。

<ul class="dropdown-menu dropdown-menu-left select_Language" role="menu">
    <li>
        <a href="/Language/Change/?LanguageAbbrevation=en&returnurl=@Request.Url"><span class="flag-icon flag-icon-gb"></span> ENG</a>
    </li>
    <li class="li-last">
        <a href="/Language/Change/?LanguageAbbrevation=dk&returnurl=@Request.Url"><span class="flag-icon flag-icon-dk"></span> DK</a>
    </li>
</ul>

语言控制器

public ActionResult Change(string LanguageAbbrevation, string returnurl)
    {
        if (!string.IsNullOrEmpty(LanguageAbbrevation))
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(LanguageAbbrevation);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(LanguageAbbrevation);
        }

        HttpCookie cookie = new HttpCookie("Language");
        cookie.Value = LanguageAbbrevation;
        Response.Cookies.Add(cookie);

        return Redirect(returnurl);

    }

最后我的Application_BeginRequest Global.asax.cs

protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["Language"];
            if (cookie != null && cookie.Value != null)
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cookie.Value);
                System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(cookie.Value);
            }
            else
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en");
                System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en");
            }
        }

当我调试并更改语言时,它会将cookie设置为正确的值(dk或en),但它永远不会更改资源值。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

尝试将bower install angular-google-maps --save 更改为LanguageAbbrevation=dk并将LanguageAbbrevation=da-DK重命名为HomeText.dk.resx