简体   繁体   English

MVC 5中的本地化-如何使用资源文件在视图中呈现多语言文本?

[英]Localization in MVC 5 - How can I render multi-lingual text in my view using a resource file?

I am trying to create a multi-lingual navigation in an MVC 5 application. 我正在尝试在MVC 5应用程序中创建多语言导航。

What I've done: 我所做的:

  1. Set System.Threading.Thread.CurrentThread.CurrentUICulture to either "en-US" or "es-ES" based on cookie value (defaults to English unless user selects Spanish) 根据Cookie值将System.Threading.Thread.CurrentThread.CurrentUICulture设置为“ en-US”或“ es-ES”(默认为英语,除非用户选择西班牙语)
  2. Created three resource files (this is my first time using them, so I'm not certain I fully understand the concept...) Index.resx, Resouce.en-US.resx, Resouce.es-ES.resx. 创建了三个资源文件(这是我第一次使用它们,所以我不确定我是否完全理解这个概念...)Index.resx,Resouce.en-US.resx,Resouce.es-ES.resx。 Each resource file is in a folder called App_GlobalResources folder 每个资源文件都位于名为App_GlobalResources的文件夹中

  3. Added a name/value combination to each .resx file, Home/Home for Index.resx and en-US.resx, and Home/Casa for es-ES.resx 为每个.resx文件添加了名称/值组合,为Index.resx和en-US.resx添加了Home / Home,为es-ES.resx添加了Home / Casa。

  4. Tried using @Resources.Index.Home in my layout file, thinking that when the value of CurrentUICulture changed from en-US to es-ES and visa-versa, the language would change based on the values in my resource files. 在布局文件中使用@Resources.Index.Home进行了尝试,认为当CurrentUICulture的值从en-US更改为es-ES,反之亦然时,该语言将根据我的资源文件中的值而更改。

Could someone please let me know how I can get the Spanish text when the value CurrentUICulture is "es-ES", and the English text when it is "en-US"? 有人可以让我知道当CurrentUICulture值为“ es-ES”时如何获得西班牙语文本,而当值为“ en-US”时如何获得英语文本吗?

_Layout.cshtml _Layout.cshtml

Resource.resx Resource.resx

EDIT 编辑

I should have stated - @Resources.Index.Home does render the text "Home" in the navigation. 我应该说过- @Resources.Index.Home确实在导航中呈现了文本“ Home”。 However, when I switch CurrentUICulture to "es-ES", it still renders "Home", not "Casa" 但是,当我将CurrentUICulture切换为“ es-ES”时,它仍然呈现“主页”,而不是“ Casa”

EDIT 2 编辑2

Here is how I set CurrentUICulture is global.asax 这是我将CurrentUICulture设置为global.asax的方式

   public void Application_AuthenticateRequest(Object sender, EventArgs e)
        {
            if (Request.Cookies["lang"] == null)
            {
                HttpCookie lang = new HttpCookie("lang");
                lang.Value = "english";
                lang.Expires = DateTime.Now.AddDays(30d);
                Response.Cookies.Add(lang);
            }
            else if (Request.Cookies["lang"] != null)
            {
                if (Request.Cookies["lang"].Value != null && Request.Cookies["lang"].Value == "english")
                {
                    System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-us");
                }
                else if (Request.Cookies["lang"].Value != null && Request.Cookies["lang"].Value == "spanish")
                {
                    System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-es");
                }
            }
        }

Just try to replace @Resources.Index.Home with @Resources.Home . 只需尝试将@Resources.Index.Home替换为@Resources.Home
This is ur understanding about localization. 这是您对本地化的了解。
Every app with localization (say N cultures) must have N .resx -files with the same name but different suffixes. 每个具有本地化的应用程序(例如N个文化)必须具有N个.resx文件,这些.resx具有相同的名称但后缀不同。 + Default culture can be used without suffixes. +默认文化可以不带后缀。 So u have 2 cultures - u must use 2 .resx files, not more. 因此,您有2种文化-您必须使用2个.resx文件,不能更多。 So Index.resx is not needed at all. 因此,根本不需要Index.resx
This should work. 这应该工作。 If not, more fixes: 如果没有,请进行以下修复:
- Don't use App_GlobalResources folder. -不要使用App_GlobalResources文件夹。 Just create .resx in common project-folders or in project root folder, just as in desktop .NET-apps. 就像在桌面.NET应用程序中一样,只需在公共项目文件夹或项目根文件夹中创建.resx
This helped me, hope this will help u. 这对我有所帮助,希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM