Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You may take a look at the <a href="http://adamyan.blogspot.com/2010/02/aspnet-mvc-2-localization-complete.html">following guide</a>. 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:</p> <pre><code>public ActionResult ChangeCulture(string lang, string returnUrl) { var langCookie = new HttpCookie("lang", lang) { HttpOnly = true }; Response.AppendCookie(langCookie); return Redirect(returnUrl); } </code></pre> <p>and then in <code>Global.asax</code> you could subscribe for the <code>Application_AcquireRequestState</code> event in order to set the current thread culture based on the value of the cookie:</p> <pre><code>protected void Application_AcquireRequestState(object sender, EventArgs e) { var langCookie = HttpContext.Current.Request.Cookies["lang"]; if (langCookie != null) { var ci = new CultureInfo(langCookie.Value); //Checking first if there is no value in session //and set default language //this can happen for first user's request if (ci == null) { //Sets default culture to english invariant string langName = "en"; //Try to get values from Accept lang HTTP header if (HttpContext.Current.Request.UserLanguages != null &amp;&amp; HttpContext.Current.Request.UserLanguages.Length != 0) { //Gets accepted list langName = HttpContext.Current.Request.UserLanguages[0].Substring(0, 2); } langCookie = new HttpCookie("lang", langName) { HttpOnly = true }; HttpContext.Current.Response.AppendCookie(langCookie); } //Finally setting culture for each request Thread.CurrentThread.CurrentUICulture = ci; Thread.CurrentThread.CurrentCulture = ci; //The line below creates issue when using default culture values for other //cultures for ex: NumericSepratore. //Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name); } } </code></pre> <p>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:</p> <pre><code>routes.MapRoute( "Default", "{lang}/{controller}/{action}/{id}", new { lang = "en-US", controller = "Home", action = "Index", id = UrlParameter.Optional } ); </code></pre> <p>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 <code>Application_AcquireRequestState</code> method so that it uses the <code>lang</code> token of the url and based on its value set the proper <code>Thread.CurrentThread.CurrentUICulture</code> and <code>Thread.CurrentThread.CurrentCulture</code>.</p> <p>And now when you wanted to change the language you would simply generate the proper link:</p> <pre><code>@Html.ActionLink("Page index en français", "index", new { lang = "fr-FR" }) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload