Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
818 views
in Technique[技术] by (71.8m points)

asp.net mvc - The field must be a number. How to change this message to another language?

How can I change that messages for all int fields so that instead of saying:

The field must be a number in English, it shows:

El campo tiene que ser numerico in Spanish.

Is there are a way?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you happen to be using ASP.NET MVC 4 onwards, check this post:

Localizing Default Error Messages in ASP.NET MVC and WebForms

Basically you have to add the following piece of code in your Application_Start() method in Global.asax:

 ClientDataTypeModelValidatorProvider.ResourceClassKey = "Messages";
 DefaultModelBinder.ResourceClassKey = "Messages";

Right click your ASP.NET MVC project in Solution Explorer inside Visual Studio and select Add => Add ASP.NET Folder => App_GlobalResources.

Now add a .resx file inside this folder called Messages.resx.

Finally add the following string resources in that .resx file:

Name                   Value
====                   =====
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.

You should be good to go.

Note that the value you're interested in is the FieldMustBeNumeric. To localize it to Spanish, you have to add another resource file named Messages.es.resx. In this specific .resx file replace the resource value with:

Name                Value
====                =====
FieldMustBeNumeric  El campo {0} tiene que ser numerico.

If you happen to be using ASP.NET MVC 3 downwards, this solution can help you achieve the same result: https://stackoverflow.com/a/2551481/114029


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...