title: How create a dynamic DataAnnotations Attribute for validation on MVC5 (globalization)?
body: <p>I'm using <strong>ef6</strong> with <strong>mvc5</strong>.
  My project need <strong>multiple language</strong>. So I need create a DataAnnotations Attribute for validation these field.</p>
  
  <p>For example: I have a <strong>Id</strong> property:</p>
  
  <pre><code>public int Id { get; set; }
  </code></pre>
  
  <p>For validation I need add annotations like</p>
  
  <pre><code>[Display("User Id")]
  Required(ErrorMessage = "Please input the Id")
  public int Id { get; set; }
  </code></pre>
  
  <p>But I need use multiple language , So I create a new DataAnnotations Attribute(<a href="https://stackoverflow.com/a/2432520/1900498">https://stackoverflow.com/a/2432520/1900498</a>):</p>
  
  <pre><code>public class LocalizedDisplayAttribute : DisplayNameAttribute
      {
          public LocalizedDisplayAttribute(string resourceId)
              : base(GetMessageFromResource(resourceId))
          { }
  
          private static string GetMessageFromResource(string resourceId)
          {
              // TODO: Return the string from the resource file
          }
      }
  </code></pre>
  
  <p>It works fine , but it will <strong>cache</strong> the result, then when session changed(I'm use session save the user website page language mode. like 1 mean English, 0 mean other language), it still not change, this is a problem for me.</p>
  
  <p>second problem is: I don't know how to rewrite <strong>RequiredAttribute</strong> for let user know , some field can't empty. </p>
  
  <p>but I also find there is another problem , looks like I need to rewrite the message about numeric field......(xx field must be numeric)</p>
  
  <p>So Is there any way can rewrite the validation rule, let me decide the error message for Required, Range, Numeric...... and it will cache it but when session changed, it will read again?</p>
  
  <p>For example:</p>
  
  <pre><code>// if session changed rewrite rule message for current language 
  if (session["language"].ToString() != LastLanguage) 
  {    
      if (session["language"].ToString() == "1")
           //English
      {
            RequiredMessage = "the field {0} must be required";
            NumericMessage = "the field {0} must be a number";
            LastLanguage = 1;
  
      }    else{
            // other language
            RequiredMessage = "xx xx {0} xxxxxxxxx";
            NumericMessage = "xx xx {0} xxxxxxxxxx";
            LastLanguage = 0;
      }
  }
  </code></pre>
  
  <p>Of course, <strong>not only the validation message</strong>, I need globalization the <strong>field display name</strong> too.</p>