Tag Archives: localize

Localizing Default Error Messages in MVC

Recently in a (non-english) project i’m working on, I was surprised with the following message “The value ‘150000.00’ is not valid for Preço.”.  I thought that my project was already localized to Portuguese, but apparently no.


The problem is related with some messages that can’t be localized using validation attributes Like the RequiredAttribute found in System.ComponentModel.DataAnnotations namespace.


[Required(ErrorMessageResourceName = "ValidacaoObrigatoria", ErrorMessageResourceType = typeof(MyProject.Properties.Resources))]
public int Prop1 { get; set; }

Messages like “The value ‘{0}’ is not valid for {1}” need a different approach.
For localize default error messages in MVC we will need to add the following code to the Global.asax Application_Start method.

DefaultModelBinder.ResourceClassKey = "ValidationResources";
ClientDataTypeModelValidatorProvider.ResourceClassKey = "ValidationResources";

Where “ValidationResources” is the the name of the resource file (class key) that contains localized string values.
Next create a resource file name ValidationResources.resx or ValidationResources.fr.resx or whatever culture you need to support, mark it embeded inside App and add the following:

FieldMustBeDate The field {0} must be a date.
FieldMustBeNumeric The field {0} must be a number.
PropertyValueInvalid The value ‘{0}’ is not valid for {1}.
PropertyValueRequired A value is required.

My translation to Portuguese will be something like this:

PropertyValueInvalid O valor ‘{0}’ não é válido para o campo ‘{1}’.
FieldMustBeDate O campo ‘{0}’ deve ser uma data válida.
FieldMustBeNumeric O campo ‘{0}’ deve ser um número válido.
PropertyValueRequired O preenchimento deste campo é obrigatório.

The resource file should be added to the App_GlobalResources folder.

Digg thisBuffer this pageShare on FacebookShare on Google+Share on LinkedInPin on PinterestShare on RedditShare on StumbleUponShare on TumblrTweet about this on Twitter