I have an ASP.NET MVC site that it’s in two languages using Resources. To allow the server to present the site in the apropiate language (depending on the one that’s configured in the user’s browser) I put the following in my web.config:
<globalization culture="es-es" uiCulture="auto" />
How can I add a link to change the uiCulture? I want to store the selection in a cookie and if it’s not present, then fall back to the browser configuration… Is it possible?
You may take a look at the following guide. It uses Session to store the current user language preference but the code could be very easily tweaked in order to use a cookie. The idea is that you will have a controller action:
and then in
Global.asaxyou could subscribe for theApplication_AcquireRequestStateevent in order to set the current thread culture based on the value of the cookie:Now this being said using cookies and session to store current language is not SEO friendly. What I prefer doing when I need a localized application is to use a special route which will contain the language:
and then prefix all my urls with the language. This provides unique urls for different languages so that robots can properly index all content. Now all that’s left is to modify the
Application_AcquireRequestStatemethod so that it uses thelangtoken of the url and based on its value set the properThread.CurrentThread.CurrentUICultureandThread.CurrentThread.CurrentCulture.And now when you wanted to change the language you would simply generate the proper link: