Reputation: 2752
I have a web application that uses localization to show either English or French for our French Canadian Customers.
It is working just fine based on the users regional settings. We have a need however to allow the user to switch back to English if their regional settings are set to French.
Is it possible to override the users regional setting if he so desires? if so...how would I code this? (for example having a link on the layout page that says English, clicking this would then change it back to English or back to French)
Also, I am using resource files to save the text strings and using the same set of views.
Upvotes: 2
Views: 242
Reputation: 5858
Somewhere in your code after they click a button to select a language:
Session["customLocalization"] = "de-DE"; //Or whatever language
In your Global.asax
protected void Application_BeginRequest(object sender, EventArgs e)
{
String sessionOverrideLocale;
if (HttpContext.Current != null && HttpContext.Current.Session != null)
{
sessionOverrideLocal = (String) HttpContext.Current.Session["customLocalization"];
}
if (sessionOverrideLocale != null)
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo(sessionOverrideLocale);
Thread.CurrentThread.CurrentCulture = new CultureInfo(sessionOverrideLocale);
}
}
Upvotes: 1
Reputation: 744
Yes, it is possible but I do not currently have access to the code I most recently worked on where we allowed the user to change their current rendered region.
Here is a blog post that goes into great detail regarding doing this. He has an older post that I believe uses MVC3 and then this newer one is written from the perspective of MVC4, so this should have you covered.
I hope it helps:
http://geekswithblogs.net/shaunxu/archive/2012/09/04/localization-in-asp.net-mvc-ndash-upgraded.aspx
Upvotes: 0