ASP.NET CORE 3.1中数据注释和错误验证消息的本地化 [英] Localization on data annotation and error validation messages in ASP.NET CORE 3.1

查看:55
本文介绍了ASP.NET CORE 3.1中数据注释和错误验证消息的本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to localize data annotation and validation error messages in 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; }

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

I use the standard implementation of resource files from the Microsoft documentation

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

        services.AddControllersWithViews()
                .AddViewLocalization(
                    LanguageViewLocationExpanderFormat.Suffix,
                      option => option.ResourcesPath = "Resources")
                .AddDataAnnotationsLocalization();

我的资源文件结构

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

|-Resoures
|---Controllers
|---Views
|---Models

我正在重构身份页面,并且无法以两种语言显示标准错误消息.Microsoft文档中的此示例对我不起作用.[

I am refactoring Identity Pages and can not showing standard error messages in two languages. This example in Microsoft docs does not work for me.. [https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-3.1#dataannotations-localization] Please someone help me

推荐答案

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

For adding DataAnnotations localization on InputModel in default Identity Register Pages(located in /Areas/Identity/Pages/Account/Register.cshtml.cs),you need to name your resource file like

Areas.Identity.Pages.Account.RegisterModel+InputModel.en-US.resx

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

For any nested model in PageModel, you need to use + instead of .

这篇关于ASP.NET CORE 3.1中数据注释和错误验证消息的本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆