如何解决脚手架身份模型的本地化数据注释
ASP.NET core 5.0 mvc 项目,我想本地化脚手架标识页面。
视图本地化工作(在脚手架剃刀文件中使用 IViewLocalizer
)但 dataAnnotations 本地化使用共享资源文件失败。
按照MS docs我有:
DataAnnotationLocalizerProvider
中的 Startup.cs
集
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization(options => {
options.DataAnnotationLocalizerProvider = (type,factory) =>
factory.Create(typeof(MyApp.SharedResources));
});
我在 Startup.cs
public void ConfigureServices(IServiceCollection services) {
...
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("en-US"),new CultureInfo("fr")
};
options.DefaultRequestCulture = new RequestCulture(culture: "en-US",uiCulture: "en-US");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
...
}
public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
{
...
var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
...
}
一个带有
的Resources
文件夹
- 我的应用命名空间中的一个空类
SharedResources.cs
- 关联的资源文件
SharedResources.fr.resx
我的脚手架身份页面,即 Login.cshtml.cs
,带有 dataAnnotations:
public class InputModel
{
[Required(ErrorMessage = "The Email field is required.")]
[EmailAddress(ErrorMessage = "The Email field is not a valid email address.")]
[Display(Name = "Email")]
public string Email { get; set; }
[Required(ErrorMessage = "The Password field is required.")]
[Display(Name = "Password")]
[DataType(DataType.Password)]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
尽管如此,我的标签和错误消息仍未翻译。
我想念什么?脚手架文件有什么限制吗?
解决方法
好的,多亏了 Ryan answer,我可以让它与一个以 resx
命名的专用 +
文件一起工作,因为身份模型具有嵌套类。
这里的记录是脚手架 mvc 登录模型的文件名:
Areas.Identity.Pages.Account.LoginModel+InputModel.fr.resx
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。