Search code examples
asp.net-mvc-4localizationglobalization

Change Locale in MVC application on click


How can I apply new locale in MVC application ?

I've created an Action that sets

Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

then redirects to Index.chtml , but this is not working.

How to make this thing work please ?


Solution

  • Setting the culture is only valid until the thread terminates, which happens after all page processing has finished. In this case, after you issue the redirect the server will send the Location HTTP header to the new address and close the response. The browser will then initiate a new request to the new location and the value you set in Thread.CurrentThread.CurrentCulture will reset to the default one.

    You have to persist the language selection (session, cookie...) and then apply it at start of your page logic.