ASP.NET CORE 3.1 中数据注释和错误验证消息的本地化

Вал*_*оев 2 globalization validation resources localization asp.net-core

如何在 ASP.NET CORE 3.1 中本地化数据注释和验证错误消息

[Required(ErrorMessage = "something")]
                [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
                [DataType(DataType.Password)]
                [Display(Name = "Password")]
                public string Password { get; set; }
Run Code Online (Sandbox Code Playgroud)

我使用 Microsoft 文档中资源文件的标准实现

services.AddLocalization(option => option.ResourcesPath = ProjectConstants.LanguageResourcesPath);

        services.AddControllersWithViews()
                .AddViewLocalization(
                    LanguageViewLocationExpanderFormat.Suffix,
                      option => option.ResourcesPath = "Resources")
                .AddDataAnnotationsLocalization();
Run Code Online (Sandbox Code Playgroud)

我的资源文件结构

|-资源
|--- 控制器
|--- 视图
|--- 模型

我正在重构身份页面,无法以两种语言显示标准错误消息。Microsoft 文档中的这个示例对我不起作用.. [ https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-3.1#dataannotations-localization] 请有人帮助我

Rya*_*yan 6

InputModel要在默认身份Register页面(位于)中添加 DataAnnotations 本地化 /Areas/Identity/Pages/Account/Register.cshtml.cs,您需要将资源文件命名为

Areas.Identity.Pages.Account.RegisterModel+InputModel.en-US.resx
Run Code Online (Sandbox Code Playgroud)

对于 中的任何嵌套模型PageModel,您需要使用+而不是.