我正在从以前的ASP迁移应用程序。. NET版本到ASP。. NET 5(vNext, MVC 6).以前我本地化的形式与DisplayAttribute
附加到ViewModel的属性:
[Required(ErrorMessageResourceName = "FieldIsRequired", ErrorMessageResourceType = typeof(Resources.Validation))]
[Display(Name = "UserName", ResourceType = typeof(Resources.Common))]
public string UserName { get; set; }
我添加了DataAnnotations服务:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddViewLocalization(options => options.ResourcesPath = "Resources/Views")
.AddDataAnnotationsLocalization();
}
当我提交一个无效的表单时,一个错误消息被本地化(在[Required]
属性中指定)。
但是试图显示表单,我得到了一个例外(没有公共属性"UserName"在资源类),直到我注释掉[Display]
属性。
似乎输入标签不能与[DisplayAttribute]
本地化了吗?
谢谢!
确实不见了。根据文档:
运行时不查找非验证属性的本地化字符串。在上面的代码中,"Email"(from [Display(Name = "Email")])不会被本地化。
更新20.03.2017 :
根据更新的文档,新的。net Core SDK重新启用了非验证属性的本地化:
DataAnnotations错误消息使用
IStringLocalizer<T>
进行本地化。使用ResourcesPath = "Resources"
选项,RegisterViewModel
中的错误消息可以存储在以下路径之一:
- 资源/ViewModels.Account.RegisterViewModel.fr.resx
- 资源/视图模型/账户/RegisterViewModel.fr.resx