Internationalizing MVC Confirmation Messages

For example, I would like this ASP.NET MVC 4 default assertion message: The value 'qsdqsdqs' is not valid for Montant

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 dll, but the post is still in English

+3


source to share


2 answers


First of all, you should store your messages in resource files like this:

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

      

On the server side, this is easy to change, and you can do it by adding an attribute to your model. Do it like this:

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

      

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

The tricky part is when you want client side validation to work too. Then you need to create your own attribute class that extends one of the attributes and also implements IClientValidatable .



Do you like it:

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

      

You are now using CustomRequired instead of Required in your models. You also don't have to provide a message every time.

EDIT

Now I saw your comment on SynerCoder's answer - that you don't want to translate posts yourself. Well, I don't think this is 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 will probably end up taking two approaches. This usually leads to magic bugs that you don't know how to bite. I highly recommend that you do the translations yourself (this is not much - about 20 or so). The advantage will be a flexible solution without any unexpected errors.

+9


source


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

With his helpers all my strings are available, such as Res.SomeTranslatedString. Now with credits for all of the above, let's translate the registration viewmodel message to a boolean property to check if users have accepted the terms. With the above tool, I put a string in 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 can see that we have [Display] untranslated yet. The solution is here: fooobar.com/questions/51767 / ...

0


source







All Articles