Setting CurrentUICulture is not being remembered by my application
I have an asp.net mvc application where i want the user to be able to change language. I have provided a series of links with small flags on to let the user choose language. The target of all these links is my "dashboard" page, in which controller i have this code:
[HttpGet]
[Authorize]
public ViewResult Dashboard(string id)
{
if (!string.IsNullOrEmpty(id))
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Gl开发者_JAVA技巧obalization.CultureInfo(id);
}
}
The "Dashboard" page is displayed in the chosen language, as it should be. But when i navigate on through my website, the culture is changed back to english (default)... am i missing something? Shouldnt changing the CurrentUICulture change the entire application to the other language?
In System.Threading.Thread.CurrentThread.CurrentUICulture, in this way the culture is set for only current request of the user, and it will be reset to default language for subsequent user requests.
You need to set the ui culture on each view where you want to show language based data (Localized data).
Tip: save the user's selected culture id in session and use it on each view to specify the uiculture
like this,
// save users selected culture id in session
Session["SelectedCultureId"] = id;
on each view set current thread ui culture id
if (Session["SelectedCultureId"] != null)
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Session["SelectedCultureId"]);
}
精彩评论