Jos*_*cht 5 c# validation data-annotations asp.net-core asp.net-core-localization
我正在开发一个ASP.Net Core Web应用程序,需要本地化我们在应用程序中使用的所有静态字符串.
我通过IStringLocalizerFactory在Startup.cs中实现并将其添加为服务来对Controller和View字符串进行本地化.
我现在正在尝试在使用注释时向模型验证添加本地化.在我的用户模型中,我有
[Required(ErrorMessageResourceType = typeof(UserModelErrorMessages), ErrorMessageResourceName = "FullNameRequired")]
[MinLength(2, ErrorMessageResourceType = typeof(UserModelErrorMessages), ErrorMessageResourceName ="FullNameMinLength")]
[MaxLength(100, ErrorMessageResourceType =typeof(UserModelErrorMessages), ErrorMessageResourceName ="FullNameMaxLength")]
public string FullName { get; set; }
Run Code Online (Sandbox Code Playgroud)
我已经把 "FullNameRequired", "FullNameMinLength" 在UserModelErrorMessage.resx文件, "FullNameMaxLength".我还有一个UserModelErrorMessage.fr.resx文件,其中包含相同的值但法语等效消息.当浏览器设置为英语时,会显示正确的错误消息.当我将浏览器切换为使用法语('fr')语言时,我在视图和控制器中定义的静态字符串以法语显示,但数据验证消息仍以英语显示.我已经尝试过Models.User.fr.resx并且手动生成.resource文件也无济于事.
在Startup.cs ConfigureServices中:
services.AddMvc()
.AddViewLocalization(options=>options.ResourcesPath="Resources")
.AddDataAnnotationsLocalization();
Run Code Online (Sandbox Code Playgroud)
在Startup.cs中 - 配置
app.UseRequestLocalization(new RequestLocalizationOptions
{
SupportedCultures = new List<CultureInfo>
{
new CultureInfo("fr"),
new CultureInfo("zh"),
new CultureInfo("tr"),
new CultureInfo("en"),
new CultureInfo("en-US"),
},
SupportedUICultures = new List<CultureInfo>
{
new CultureInfo("fr"),
new CultureInfo("zh"),
new CultureInfo("tr"),
new CultureInfo("en"),
new CultureInfo("en-US"),
}
}, new RequestCulture("en-US")
);
Run Code Online (Sandbox Code Playgroud)
用法语获取数据注释验证消息我错过了什么?
| 归档时间: |
|
| 查看次数: |
885 次 |
| 最近记录: |