【发布时间】:2022-06-16 10:15:44
【问题描述】:
我有一个 ASP.NET Core MVC (.NET 6) Web 应用程序。我的主要 Web 应用程序的名称是 MyApp.Web,其中包含所有控制器、视图和视图模型。解决方案中的另一个核心类库是 MyApp.DAL,其中包含 MyApp.Web 的模型。
我必须本地化 Web 应用程序。 MyApp.Web 中的所有控制器、视图和 ViewModel 工作正常。 ViewModels 中的 DataAnnotations 也在 MyApp.Web 中存在的 ViewModels 中本地化,没有任何问题。
我无法本地化 MyApp.DAL 类库中的模型。是否可以本地化除主 Web 应用程序之外的另一个类库的模型?如何在 Program.cs 中进行配置,以便 MyApp.Web 中的 ViewModel 和 MyApp.DAL 中的模型都适用于 DataAnnotations?
我的 .NET 6 相关 Program.cs 如下所示:
builder.Services.AddLocalization(opt => { opt.ResourcesPath = "Resources"; });
builder.Services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-GB"),
new CultureInfo("da-DK"),
};
options.DefaultRequestCulture = new RequestCulture(culture: "en-GB", uiCulture: "en-GB");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
builder.Services.AddControllersWithViews()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix) //For localization
.AddDataAnnotationsLocalization();
builder.Services.AddRazorPages()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix) //For localization
.AddDataAnnotationsLocalization();
var app = builder.Build();
//For localization
app.UseRequestLocalization();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapRazorPages();
app.Run();
【问题讨论】:
标签: asp.net-core asp.net-core-mvc .net-6.0 asp.net-core-localization