C# 如何在 ASP .NET MVC 中使用 Resources 和 CultureInfo 更改站点语言?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16879618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:01:41  来源:igfitidea点击:

How to change site language using Resources and CultureInfo in ASP .NET MVC?

c#asp.net-mvc

提问by Dmytro

I have several resource files for each language I want to support, named like below:

对于我想要支持的每种语言,我都有几个资源文件,命名如下:

NavigationMenu.en-US.resx
NavigationMenu.ru-RU.resx
NavigationMenu.uk-UA.resx

Files are located in MySolution/Resources/NavigationMenufolder.

文件位于MySolution/Resources/NavigationMenu文件夹中。

I have action which sets CurrentCultureand CurrentUICulturelike below

我有如下设置CurrentCultureCurrentUICulture喜欢的动作

public ActionResult SetLanguage(string lang)
{
    try
    {
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
        Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(lang);
        return Redirect(Request.UrlReferrer.AbsoluteUri);
    }
    catch(Exception)
    {
        return RedirectToAction("Index");
    }
}

langparameter values are uk-UA, ru-RUor en-USdepending on what link in my view was clicked. Also I have web config globalization defined section:

lang参数值是uk-UAru-RU或者en-US取决于我的视图中的哪个链接被点击。我也有 web 配置全球化定义部分:

<globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="ru-RU" uiCulture="ru-RU" />

When my application starts I have Russian language as expected but when I try to change my language with SetLanguageaction to English I get no language changes in my views. NavigationMenu.SomePropertyis still Russian. What am I missing?

当我的应用程序启动时,我按预期使用俄语,但是当我尝试通过SetLanguage操作将语言更改为英语时,我的视图中没有语言更改。NavigationMenu.SomeProperty仍然是俄罗斯人。我错过了什么?

采纳答案by Jerad Rose

You are updating the culture for the current thread only.

您仅更新当前线程的区域性。

Most sites support localization by including this as part of their URL (in all pages). And with MVC, you can implement this approach by processing the culture using an action filter. This answerhas a good solution for this.

大多数站点通过将其作为其 URL 的一部分(在所有页面中)来支持本地化。使用 MVC,您可以通过使用操作过滤器处理文化来实现这种方法。这个答案对此有一个很好的解决方案。

Aside from this, you would need to implement this by either persisting the culture to the session or a cookie, and then updating the thread's culture on every request, again by implementing an action filter, or during an application event that contains the request context, such as AquireRequestState.

除此之外,您需要通过将文化持久化到会话或 cookie 来实现这一点,然后在每个请求上更新线程的文化,再次通过实现操作过滤器,或在包含请求上下文的应用程序事件期间,例如 AquireRequestState。

回答by Idrees Khan

Persist your language in e.g cookie in your SetLanguage()and then in the BaseControlleror ActionFilter(recommended) get the values from cookie and the update the threads accordingly.
if this doesn't make any sense, take a look at the following nice articles;
CodeProject: http://www.codeproject.com/Articles/526827/MVC-Basic-Site-Step-1-Multilingual-Site-Skeleton
And this One:http://afana.me/post/aspnet-mvc-internationalization.aspx
e.g;

将您的语言保留在例如 cookie 中SetLanguage(),然后在BaseControllerActionFilter(推荐)中从 cookie 中获取值并相应地更新线程。
如果这没有任何意义,请查看以下不错的文章;
CodeProjecthttp: //www.codeproject.com/Articles/526827/MVC-Basic-Site-Step-1-Multilingual-Site-Skeleton
这一个http: //afana.me/post/aspnet-mvc-internationalization .aspx
例如;

// Save culture in a cookie
        HttpCookie cookie = Request.Cookies["_culture"];
        if (cookie != null)
            cookie.Value = culture;   // update cookie value
        else
        {

            cookie = new HttpCookie("_culture");
            cookie.HttpOnly = false; // Not accessible by JS.
            cookie.Value = culture;
            cookie.Expires = DateTime.Now.AddYears(1);
        }
        Response.Cookies.Add(cookie);