title: ASP Net MVC - Localization of Validation Messages
body: <p>I ran into a problem with localization of validation messages in my asp net mvc 5 application.</p>
  
  <p>I use this for localization:</p>
  
  <p>Route Config:</p>
  
  <pre><code>[Internationalization]
  public class RouteConfig
  {
      public static void RegisterRoutes(RouteCollection routes)
      {
          routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  
          routes.MapRoute("DefaultLocalized",
          "{language}-{culture}/{controller}/{action}/{id}",
          new
          {
              controller = "Home",
              action = "Index",
              id = "",
              language = "de",
              culture = "DE"
          });
  
          routes.MapRoute(
              name: "Default",
              url: "{controller}/{action}/{id}",
              defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
          );
      }
  }
  </code></pre>
  
  <p>LocalizationAttribute:</p>
  
  <pre><code> class InternationalizationAttribute : ActionFilterAttribute
  {
      public override void OnActionExecuting(ActionExecutingContext filterContext)
      {
          string language = (string)filterContext.RouteData.Values["language"] ?? "de";
          string culture = (string)filterContext.RouteData.Values["culture"] ?? "DE";
  
          Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(string.Format("{0}-{1}", language, culture));
          Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(string.Format("{0}-{1}", language, culture));
      }
  }
  </code></pre>
  
  <p>Example of model property:</p>
  
  <pre><code>    [Display(Order = 0, ResourceType = typeof(resourceProjectName.ApplicationStrings), Name = "LabelText")] // this works
      [Required(ErrorMessageResourceType = typeof(resourceProjectName.ApplicationStrings), ErrorMessageResourceName = "ValidationText")] //this does not work
      public string Property { get; set; }
  </code></pre>
  
  <p>Html example:</p>
  
  <p>This is inside a Html.BeginForm. The validation messages are shown after a POST request, if something is missing or not valid.</p>
  
  <pre><code>&lt;div&gt;
  &lt;div class="form-group row"&gt;
      &lt;div&gt;
          @Html.LabelFor(x =&gt; x.Property)
      &lt;/div&gt;
      &lt;div class="col-sm-6"&gt;
          @Html.TextBoxFor(x =&gt; x.Property, new { @class = "form-control" })
      &lt;/div&gt;
      @if (ViewData.ModelState.Where(i =&gt; i.Key.Equals("Property")).Any(x =&gt; x.Value.Errors.Any()))
      {
          &lt;div class="col-sm-6"&gt;
              @Html.ValidationMessageFor(x =&gt; x.Property, "", new { @class = "alert-validation alert-danger" })
          &lt;/div&gt;
      }
  &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Web.config:</p>
  
  <pre><code>&lt;globalization culture="auto" uiCulture="auto" enableClientBasedCulture="true"/&gt;
  </code></pre>
  
  <p>Localization works for everything on the website except for validation messages.</p>
  
  <p>It always shows the resource string that matches the <strong>browsers language settings</strong>, although the user navigated to e.g.: site.de/en-EN/test. </p>
  
  <p>Is there a problem with the web.config? I tried to set enableClientBasedCulture to false, but the problem still occured.</p>
  
  <p>Kind regards,
  Martin</p>