【发布时间】:2022-07-21 13:05:50
【问题描述】:
我有一个在 ASP.NET Core 6 MVC 上运行的多语言网站。
数据标注应基于用户语言;我可以使用sharedResource 类使网站成为双语。
问题是如何使模型数据标注错误双语;目前,我只得到了数据注解ErrorMessage。
程序.cs
builder.Services.AddControllersWithViews()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
//.AddDataAnnotationsLocalization();// <--- for ERROR MSG -----
.AddDataAnnotationsLocalization(
options => {
options.DataAnnotationLocalizerProvider = (type, factory) =>
factory.Create(typeof(DataAnnotationResource));
});// <---------- For ERROR MSG -----
工厂数据模型
public class FactoryData
{
[Required(ErrorMessage = "General.RequiresMessageOOO")]
public string NameInAr { get; set; }
[Required(ErrorMessage = "General.RequiresMessageOOO")]
[MaxLength(2, ErrorMessage = "General.MaxlengthExceededOOO")]
public string NameInEn { get; set; }
[Required]
[Range(1,3)]
public string Age { get; set; }
}
这是localizationResource 文件夹:
当前代码的输出
【问题讨论】:
标签: c# asp.net-core-mvc