模型验证多语言翻译在 .net 核心中不起作用
Model Validation multi language translate not working in .net core
我正在使用多语言 .net core 2.1
由于我的模型项目不同,我的模型数据注释验证无法翻译成荷兰语。
当我使用网络项目中的模型时,它工作正常。
所以我的问题是如何从不同的模型项目翻译模型数据注释验证。
我的项目结构如下。
Startup.cs
services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; });
services.AddMvc()
.AddViewLocalization(
LanguageViewLocationExpanderFormat.Suffix,
opts => { opts.ResourcesPath = "Resources"; })
.AddDataAnnotationsLocalization();
services.Configure<RequestLocalizationOptions>(
opts =>
{
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-GB"),
new CultureInfo("nl-NL"),
};
opts.DefaultRequestCulture = new RequestCulture("nl-NL");
// Formatting numbers, dates, etc.
opts.SupportedCultures = supportedCultures;
// UI strings that we have localized.
opts.SupportedUICultures = supportedCultures;
});
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//var options = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
//app.UseRequestLocalization(options.Value);
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "uploads")),
RequestPath = "/uploads"
});
app.UseCookiePolicy();
app.UseSession();
app.UseMvc();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
我如何翻译模型数据注释验证。
提前致谢
如果你想翻译模型数据注释验证和你的模型 Class 库项目不同(模型 class 文件不是 Web 项目的一部分)那么你需要创建相同的 "Resources"模型 class 库项目中的文件夹。
我正在使用多语言 .net core 2.1 由于我的模型项目不同,我的模型数据注释验证无法翻译成荷兰语。
当我使用网络项目中的模型时,它工作正常。
所以我的问题是如何从不同的模型项目翻译模型数据注释验证。
我的项目结构如下。
Startup.cs
services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; });
services.AddMvc()
.AddViewLocalization(
LanguageViewLocationExpanderFormat.Suffix,
opts => { opts.ResourcesPath = "Resources"; })
.AddDataAnnotationsLocalization();
services.Configure<RequestLocalizationOptions>(
opts =>
{
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-GB"),
new CultureInfo("nl-NL"),
};
opts.DefaultRequestCulture = new RequestCulture("nl-NL");
// Formatting numbers, dates, etc.
opts.SupportedCultures = supportedCultures;
// UI strings that we have localized.
opts.SupportedUICultures = supportedCultures;
});
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//var options = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
//app.UseRequestLocalization(options.Value);
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "uploads")),
RequestPath = "/uploads"
});
app.UseCookiePolicy();
app.UseSession();
app.UseMvc();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
我如何翻译模型数据注释验证。 提前致谢
如果你想翻译模型数据注释验证和你的模型 Class 库项目不同(模型 class 文件不是 Web 项目的一部分)那么你需要创建相同的 "Resources"模型 class 库项目中的文件夹。