简体   繁体   中英

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.

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)
  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. Each resource file is in a folder called App_GlobalResources folder

  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

  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.

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"?

_Layout.cshtml

Resource.resx

EDIT

I should have stated - @Resources.Index.Home does render the text "Home" in the navigation. However, when I switch CurrentUICulture to "es-ES", it still renders "Home", not "Casa"

EDIT 2

Here is how I set CurrentUICulture is 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 .
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. + Default culture can be used without suffixes. So u have 2 cultures - u must use 2 .resx files, not more. So Index.resx is not needed at all.
This should work. If not, more fixes:
- Don't use App_GlobalResources folder. Just create .resx in common project-folders or in project root folder, just as in desktop .NET-apps.
This helped me, hope this will help u.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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