1

I'm using ef6 with mvc5. My project need multiple language. So I need create a DataAnnotations Attribute for validation these field.

For example: I have a Id property:

public int Id { get; set; }

For validation I need add annotations like

[Display("User Id")]
Required(ErrorMessage = "Please input the Id")
public int Id { get; set; }

But I need use multiple language , So I create a new DataAnnotations Attribute(https://stackoverflow.com/a/2432520/1900498):

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
        }
    }

It works fine , but it will cache 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.

second problem is: I don't know how to rewrite RequiredAttribute for let user know , some field can't empty.

but I also find there is another problem , looks like I need to rewrite the message about numeric field......(xx field must be numeric)

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?

For example:

// 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;
    }
}

Of course, not only the validation message, I need globalization the field display name too.

Community
  • 1
  • 1
qakmak
  • 1,287
  • 9
  • 31
  • 62

1 Answers1

2

DataAnnotation already provides globalization support:

[Display(ResourceType = typeof(Resource), Name = "Test")]
[Required(ErrorMessageResourceType = typeof(Resource), ErrorMessageResourceName = "TestRequired")]
public string Test { get; set; }

To change the current culture in the global.asax

private void Application_AcquireRequestState(object sender, EventArgs e)
{
    if (Context != null && Context.Session != null)
    {
        string language = Context.Session["language"] as string;
        if (language != null)
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo(language);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
        }
    }
}
meziantou
  • 20,589
  • 7
  • 64
  • 83
  • the problem is that's looks like not based on session, it's based on the browser. but I don't want use browser do it. I need based ` session["language"] ` value. – qakmak Oct 09 '14 at 14:26
  • In this case, you may change the current culture based on `session["language"]` in the Global.asax so you code will use the right culture to access resources. – meziantou Oct 09 '14 at 14:36
  • Sorry, Is there a little demo? I still not very clearly understand. If you can give a sample that will be very helpful. – qakmak Oct 09 '14 at 14:39
  • But how do I customize the default validation message? like : "XX field must required, XX field must a number". Is there any way I can change the default message too? thanks – qakmak Oct 09 '14 at 16:42
  • You cannot change the default message. Instead you can set the resource type and resource name each time you declare the validation attribute. Another option is to create a custom attribute, for instance `CustomRequiredAttribute`, which inherits from the `RequiredAttribute` and set the default resource type name and resource name in the constructor – meziantou Oct 09 '14 at 16:54