Change language based on resource files in ASP.NET MVC 4

I have 2 resource files: Resources.resx (has several lines in Romanian) and Resources.en-US.resx (has the same lines in English).

I want to change (in the drop-down list, list, ...) the witch resource resource to use. It could be in _Layout.cshtml. I do not need to define the culture of the user.

Q: How to select a resource file from a page?

Edit: can this be done without changing MapRoute by default?

+7
source share
2 answers

One of the ways you can do this is to simply drag the page into a specific language (which is pretty nice since you can send links to specific languages), and then set the locale Thread parameter in the base class on your controller.

This blog post describes what I am talking about in more detail: Localization in ASP.NET MVC - 3-day investigation, 1-day work

+6
source

Check Blog . No change to MapRoute by default.

_Layout.cshtml page:

@using Resources; <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title></title> </head> <body> <div> <form method="post"> @TestResource.SelectLanguage <select name="lang"> <option></option> <option value="en-GB" @(Culture == "en-GB" ? "selected=\"selected\"" : "")>English</option> <option value="fr-FR" @(Culture == "fr-FR" ? "selected=\"selected\"" : "")>French</option> <option value="de-DE" @(Culture == "de-DE" ? "selected=\"selected\"" : "")>German</option> </select> <input type="submit" value="@TestResource.Submit" /> </form> </div> @RenderBody() </body> </html> 

The culture is installed in the _PageStart.cshtml file:

 @{ Layout = "~/_Layout.cshtml"; if(!Request["lang"].IsEmpty()){ Culture = UICulture = Request["lang"]; } } 

The last page is the default page itself:

 @using Resources; <h1>@TestResource.Welcome</h1> <p><img src="images/@TestResource.FlagImage" /></p> 

http://www.mikesdotnetting.com/Article/183/Globalization-And-Localization-With-Razor-Web-Pages

+4
source

All Articles