How to constantly change the language?

I have a question. I read a post here on how to change the language by sending a parameter to the controller, and the controller can handle this change.

here is what i read:

First: add a route

routes.MapRoute( "Default", "{language}/{controller}/{action}/{id}", new { language = "en", controller = "Home", action = "Index", id = "" } ); 

Second: use ActionLink to send the parameter

 <li><%= Html.ActionLink( "Spanish", ViewContext.RouteData.Values["action"].ToString(), new { language = "es" })%></li> <li><%= Html.ActionLink( "French", ViewContext.RouteData.Values["action"].ToString(), new { language = "fr" })%></li> <li><%= Html.ActionLink( "English", ViewContext.RouteData.Values["action"].ToString(), new { language = "en" })%></li> 

Now I tried this solution and it works fine. However, it only works once. I mean, if you click on French, it will display the page in French. But the rest of the links on the page still point to the default value of "en".

How do I change the default “language” to the selected language so that all links use the selected new language?

+4
source share
2 answers

If you want the selection to be “remembered”, you will need to use the approach in which the original selection is stored.

Thus, your main settings use a cookie or save the selection in a session variable.

+1
source

Using resource files seems to be the most efficient way to do this. You need to save all the texts that will be displayed on the page in the resource file, and you need to create separate resource files for all languages ​​that you want to support. The content on your page will be transferred from the resource file based on the language settings used in the browser.

The following codeproject article demonstrates this very well:

http://www.codeproject.com/Articles/181738/Creating-a-Bilingual-ASP-NET-MVC3-Application-Part

0
source

Source: https://habr.com/ru/post/1447648/


All Articles