【发布时间】:2020-12-22 15:21:12
【问题描述】:
到目前为止,我已经能够翻译 ASP.Net Core 2.1 Web 应用程序中的所有内容。
事实证明这是一个小挑战,因为脚手架的帐户页面需要一些设置。
但我找不到翻译密码验证消息的方法。此外,翻译模型绑定消息是一个小挑战(感谢 stackoverflow)。
有什么想法吗?
我包含了我的Startup.cs 文件的相关部分:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddMvc(options =>
{
var type = typeof(SharedResources);
var assemblyName = new AssemblyName(type.GetTypeInfo().Assembly.FullName);
var factory = services.BuildServiceProvider().GetService<IStringLocalizerFactory>();
var L = factory.Create("SharedResources", assemblyName.Name);
options.ModelBindingMessageProvider.SetValueIsInvalidAccessor(x => L["The value '{0}' is invalid.", x]);
options.ModelBindingMessageProvider.SetValueMustNotBeNullAccessor(x => L["The value '{0}' is invalid.", x]);
options.ModelBindingMessageProvider.SetValueMustBeANumberAccessor(x => L["The field {0} must be a number.", x]);
options.ModelBindingMessageProvider.SetMissingBindRequiredValueAccessor(x => L["A value for the '{0}' property was not provided.", x]);
options.ModelBindingMessageProvider.SetAttemptedValueIsInvalidAccessor((x, y) => L["The value '{0}' is not valid for {1}.", x, y]);
options.ModelBindingMessageProvider.SetMissingKeyOrValueAccessor(() => L["A value is required."]);
options.ModelBindingMessageProvider.SetUnknownValueIsInvalidAccessor(x => L["The supplied value is invalid for {0}.", x]);
options.ModelBindingMessageProvider.SetMissingRequestBodyRequiredValueAccessor(() => L["A non-empty request body is required."]);
options.ModelBindingMessageProvider.SetNonPropertyAttemptedValueIsInvalidAccessor(x => L["The value '{0}' is not valid.", x]);
options.ModelBindingMessageProvider.SetNonPropertyUnknownValueIsInvalidAccessor(() => L["The supplied value is invalid."]);
options.ModelBindingMessageProvider.SetNonPropertyValueMustBeANumberAccessor(() => L["NonPropertyValueMustBeNumber"]);
})
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization(options =>
{
options.DataAnnotationLocalizerProvider = (type, factory) =>
{
// This is for Account scaffolded pages data annotations
return factory.Create(typeof(SharedResources));
};
});
...
}
我不能在Register.cshtml.cs 的InputModel 中添加这样的内容,因为ErrorMessage 会被忽略(反正我不会这样做,因为我不想硬编码密码策略描述):
[Required(ErrorMessage = "The {0} field is required.")]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
[DataType(DataType.Password, ErrorMessage = "Password must be nice")]
[Display(Name = "Password")]
public string Password { get; set; }
【问题讨论】:
标签: c# asp.net-core asp.net-core-2.1 asp.net-core-identity