MVC Validation message internationalization

混江龙づ霸主 提交于 2019-12-20 01:07:10

问题


For example, I would like this default ASP.NET MVC 4 validation message: The value 'qsdqsdqs' is not valid for Montant to be displayed in french.

I found this package http://nuget.org/packages/Microsoft.AspNet.Mvc.fr/ and installed it but how do I get it to work ?

I added <globalization culture="fr-FR" uiCulture="auto:fr" /> to web.config and referenced the dll but the message is still in english


回答1:


First of all you should keep your messages in Resources files like that:

Resources/ErrorMessages.resx // default messages
Resources/ErrorMessages.fr.resx // for french 

On server side it's vary easy, and you can do it by adding an attribute to your model. Do it like that:

[Required(ErrorMessageResourceType = typeof(Resources.ErrorMessages), ErrorMessageResourceName = "FieldRequired")]

where "FieldRequired" is one of the fields in Resources.ErrorMessages

The tricky part is when you want the client side validation to work as well. Than you have to create your own attribute class which extends one of the attributes and also implements IClientValidatable.

You to it like that:

public class CustomRequiredAttribute : RequiredAttribute, IClientValidatable
    {
        public override string FormatErrorMessage(string name)
        {
            return String.Format(ErrorMessages.FieldRequired, name);
        } 

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var rule = new ModelClientValidationRequiredRule(String.Format(ErrorMessages.FieldRequired, metadata.DisplayName));
            return new[] { rule };
        }
    }

From now on you use CustomRequired instead of Required in your models. You also don't have to specify the message each time.

EDIT

Now I've seen your comment on SynerCoder's answer - that you don't want to translate messages yourself. Well I don't think it's the right approach. Even if you find something that will translate the standard messages for you it won't translate any custom messages, so you'll probably end up mixing 2 approaches. This usually leads to magic errors you don't know how to bite. I strongly suggest you do the translations yourself (it's not much to do - something like 20 or so?). The benefit will be a flexible solution with no unexpected errors.




回答2:


Actually using ResX Manager. Visual Studio menu Tools -> Extensions and Updates -> search for "resx" in Online tools.

With its helpers all my strings are accessed like "Res.SomeTranslatedString". Now with credits to everybody above, let's translate a registration viewmodel message for a boolean property to check if user accepted terms and conditions. With the above tool i have put the string into Res.YouMustAcceptTermsAndConditions. Then we modify the viewmodel code:

Was:

public class RegisterViewModel
{
    [Required]
    [Range(typeof(bool), "true", "true", ErrorMessage = "You must accept terms and conditions.")]
    [Display(Name = "Agree to terms.")]
    public bool AgreeTerms { get; set; }

Became:

public class RegisterViewModel
{
    [Required]
    [Range(typeof(bool), "true", "true", ErrorMessageResourceType = typeof(Res), ErrorMessageResourceName = "YouMustAcceptTermsAndConditions")]
    [Display(Name = "Agree to terms.")]
    public bool AgreeTerms { get; set; }

Now you see that we have the [Display] still untranslated.. The solution is here: https://stackoverflow.com/a/3877154/7149454



来源:https://stackoverflow.com/questions/14709342/mvc-validation-message-internationalization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!