该字段必须是数字.如何将此消息更改为另一种语言? [英] The field must be a number. How to change this message to another language?
问题描述
如何更改所有 int
字段的消息,而不是说:
How can I change that messages for all int
fields so that instead of saying:
该字段必须是英文数字
,它显示:
El campo tiene que ser numerico
西班牙语.
有办法吗?
推荐答案
如果你碰巧在使用 ASP.NET MVC 4 以上版本,请查看这篇文章:
If you happen to be using ASP.NET MVC 4 onwards, check this post:
在 ASP.NET MVC 和 WebForms 中本地化默认错误消息
基本上,您必须在 Global.asax
的 Application_Start()
方法中添加以下代码:
Basically you have to add the following piece of code in your Application_Start()
method in Global.asax
:
ClientDataTypeModelValidatorProvider.ResourceClassKey = "Messages";
DefaultModelBinder.ResourceClassKey = "Messages";
在 Visual Studio 的解决方案资源管理器中右键单击您的 ASP.NET MVC 项目,然后选择 Add =>添加 ASP.NET 文件夹 =>App_GlobalResources
.
Right click your ASP.NET MVC project in Solution Explorer inside Visual Studio and select Add => Add ASP.NET Folder => App_GlobalResources
.
现在在这个名为 Messages.resx
的文件夹中添加一个 .resx
文件.
Now add a .resx
file inside this folder called Messages.resx
.
最后在那个.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.
你应该很高兴.
请注意,您感兴趣的值是 FieldMustBeNumeric
.要将其本地化为 Spanish,您必须添加另一个名为 Messages.es.resx
的资源文件.在这个特定的 .resx
文件中,将资源值替换为:
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.
<小时>
如果您碰巧使用的是 ASP.NET MVC 3 以下版本,此解决方案可以帮助您实现相同的结果:https://stackoverflow.com/a/2551481/114029
这篇关于该字段必须是数字.如何将此消息更改为另一种语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!