要实现ASP Core 2.2身份验证输入模型从登录进行本地化,可以按照以下步骤进行:
public void ConfigureServices(IServiceCollection services)
{
// 添加本地化服务
services.AddLocalization(options => options.ResourcesPath = "Resources");
// 添加身份验证
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/Account/Login";
options.AccessDeniedPath = "/Account/AccessDenied";
});
// 添加MVC服务
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
// 设置默认语言
services.Configure(options =>
{
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("zh-CN")
};
options.DefaultRequestCulture = new RequestCulture("zh-CN");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
}
在Resources文件夹下创建对应语言的资源文件,例如"Views.Account.Login.zh-CN.resx"和"Views.Account.Login.en-US.resx"。
在Login.cshtml视图中使用@Localizer来本地化输入模型:
@using Microsoft.Extensions.Localization
@inject IStringLocalizer Localizer
@page
@model LoginModel
@Localizer["Login"]
[Display]特性来本地化输入模型的属性:public class LoginModel
{
[Required(ErrorMessage = "Please enter your email")]
[Display(Name = "Email")]
public string Email { get; set; }
[Required(ErrorMessage = "Please enter your password")]
[Display(Name = "Password")]
public string Password { get; set; }
}
这样就可以实现ASP Core 2.2身份验证输入模型从登录进行本地化了。根据用户所选的语言,系统会自动加载对应的资源文件进行本地化。