title: ASP.NET MVC5 Change Language using Route
body: <p>I'm new using MVC5 and I try to do a website with different language.
  So far I do some configuration and when I click a different language it changes the url but it doesn't change the text in view.
  Here is my code if anyone have idea:</p>
  
  <p>In App_Data folder I have 2 files: CultureConstraint and CultureFilter</p>
  
  <pre><code>public class CultureFilter : IAuthorizationFilter
  {
  private readonly string defaultCulture;
  
  public CultureFilter(string defaultCulture)
  {
      this.defaultCulture = defaultCulture;
  }
  
  public void OnAuthorization(AuthorizationContext filterContext)
  {
      var values = filterContext.RouteData.Values;
  
      string culture = (string)values["culture"] ?? this.defaultCulture;
  
      CultureInfo ci = new CultureInfo(culture);
  
      Thread.CurrentThread.CurrentCulture = ci;
      Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(ci.Name);
  }
  }
  
  
  public class CultureConstraint : IRouteConstraint
  {
  private readonly string defaultCulture;
  private readonly string pattern;
  
  public CultureConstraint(string defaultCulture, string pattern)
  {
      this.defaultCulture = defaultCulture;
      this.pattern = pattern;
  }
  
  public bool Match(
      HttpContextBase httpContext,
      Route route,
      string parameterName,
      RouteValueDictionary values,
      RouteDirection routeDirection)
  {
      if (routeDirection == RouteDirection.UrlGeneration &amp;&amp;
          this.defaultCulture.Equals(values[parameterName]))
      {
          return false;
      }
      else
      {
          return Regex.IsMatch((string)values[parameterName], "^" + pattern + "$");
      }
  }
  }
  </code></pre>
  
  <p>In RouteConfig I add:</p>
  
  <pre><code>routes.MapRoute(
              name: "DefaultWithCulture",
              url: "{culture}/{controller}/{action}/{id}",
              defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
  constraints: new { culture = new CultureConstraint(defaultCulture: "en", pattern: "[a-z]{2}") }
  );
  </code></pre>
  
  <p>In filterConfig:</p>
  
  <pre><code>filters.Add(new CultureFilter(defaultCulture: "en"));
  </code></pre>
  
  <p>In the _Layout View Page:</p>
  
  <pre><code>@using Test.App_LocalResources;
  @{
    var routeValues = this.ViewContext.RouteData.Values;
    var controller = routeValues["controller"] as string;
    var action = routeValues["action"] as string;
  }
  
     &lt;ul&gt;
              &lt;li&gt;@Html.ActionLink("Russia", @action, @controller, new { culture = "ru" }, new { rel = "alternate", hreflang = "ru" })&lt;/li&gt;
              &lt;li&gt;@Html.ActionLink("English", @action, @controller, new { culture = "en" }, new { rel = "alternate", hreflang = "en" })&lt;/li&gt;
          &lt;/ul&gt;
          &lt;div style="background: silver"&gt;
              @GlobalRes.Money:  &lt;br /&gt;
              @GlobalRes.DateAndTime: &lt;br /&gt;
          &lt;/div&gt;
  </code></pre>
  
  <p>And Also in the View Page:</p>
  
  <pre><code>@using Test.App_LocalResources;
  @{
      ViewBag.Title = GlobalRes.Title;
  }
  </code></pre>
  
  <p>I put the language file in App_LocalResources with the name Global.resx and Global.ru.resx.</p>
  
  <p>So Title, Money and DateTime always get the english text never the russia one.
  Did I miss anything?</p>