0

I have a problem with the way I have set multi langugae in my MVC project. Right now I have a function which sets the selected language :

public void SetLanguage(string lang)
{
        CultureInfo ci = new CultureInfo(lang);
        System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
}

This is OK and sets the language but I need to do this in all the actions of my controller.

The language is obtained from the URL containing an id as parameter. This id is encrypted and contains some info among the language.

So the first time the user login in the app, the id is created and encrypted and is passed as parameter in the url.

Then the parameter is recuperated from the URL, decrypted and the language is set using the above code.

But I need to do this, set the language each time, in each action. Any idea why is this so? How and to set the language only once so that I don't need to call the SetLanguage method in each action.

refresh
  • 1,319
  • 2
  • 20
  • 71
  • You are updating the culture for the current thread only. One way to solve this is create custom action filter to process the culture. – penleychan May 30 '18 at 05:06
  • 2
    can you use BaseClass Controller? if so you can call it inside `OnActionExecuting` https://learn.microsoft.com/en-us/previous-versions/aspnet/web-frameworks/mt150319(v=vs.118) – RizkiDPrast May 30 '18 at 05:09
  • @penleychan : how do we do that? – refresh May 30 '18 at 05:11
  • @tabby, here's a [question on SO](https://stackoverflow.com/questions/1560796/set-culture-in-an-asp-net-mvc-app/1561583#1561583) that shows that – penleychan May 30 '18 at 05:13
  • @RizkiDPrast : I don't understand how OnActionExecuting works. Do you have an example? – refresh May 30 '18 at 05:20
  • check this out: https://mycodepad.wordpress.com/2014/11/25/asp-net-mvc-5-custom-authentication-with-onactionexecuting/ – RizkiDPrast May 30 '18 at 05:23
  • You can create a custom ActionFilter and write your logic in OnActionExecuting method of it. – Sahil Sharma May 30 '18 at 05:38

0 Answers0