Silverlight 4:内置数据注释验证异常的本地化
我想使用数据注释来处理我的 Silverlight 应用程序中的验证。内置的验证属性(主要是 StringLength 和Required)非常棒,并且让生活变得非常轻松。然而,它们似乎有一个严重缺陷。例如,如果我的语言环境设置为 fr-CA,则验证异常仍为英语 - '名称字段是必需的'、'字段名称必须是最大长度为 20 的字符串'等。
这是主要问题。这意味着,如果我想要内置验证属性的本地化错误消息,我必须手动将 ErrorMessage/ErrorMessageResourceType 添加到业务层中每个可验证属性上的每个验证属性,并手动添加翻译后的字符串对于每条错误消息。
那么...我在这里错过了什么吗?有没有办法自动本地化内置验证属性?或者其他更简单的方法可以做到这一点?或者我只是完全不走运,并坚持手动路线?
任何评论或想法将不胜感激。
I would like use data annotations to handle validation in my Silverlight app. The built-in validation attributes (primarily StringLength and Required) are great, and make life very easy. However, they seem to have one critical flaw. If my locale is set to fr-CA, for example, the validation exceptions are still in English - 'The Name field is required', 'The field Name must be a string with a maximum length of 20', etc.
This is a major problem. It means that if I want localized error messages for the built-in validation attributes, I have to manually add ErrorMessage/ErrorMessageResourceType to every validation attribute on every validatable property in my business layer, and manually add translated strings for every error message.
So... am I missing something here? Is there a way to automatically have the built-in validation attributes localized? Or some other easier way of doing this? Or am I just completely out of luck, and stuck with the manual route?
Any comments or thoughts would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我通过简单地子类化内置验证属性来解决这个问题。问题解决了!
Ok, I got around this by simply subclassing the built-in validation attributes. Problem solved!