.NET CORE不加载用于不同语言的资源文件
问题描述:
我无法在ASP.NET CORE MVC中加载翻译。.NET CORE不加载用于不同语言的资源文件
我stratup样子:
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc()
.AddViewLocalization(
Microsoft.AspNetCore.Mvc.Razor.LanguageViewLocationExpanderFormat.Suffix,
opts => { opts.ResourcesPath = "Resources"; });
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("sk-SK"),
new CultureInfo("hu-HU"),
};
services.Configure<RequestLocalizationOptions>(options =>
{
options.DefaultRequestCulture = new RequestCulture(culture: "sk-SK", uiCulture: "sk-SK");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<UserData, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
services.AddTransient<IProductService, ProductService>();
// Repository
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
services.AddScoped<IProductRepository, ProductRepository>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory,
ApplicationDbContext context)
{
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("sk-SK"),
new CultureInfo("hu-HU"),
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("sk-SK"),
// Formatting numbers, dates, etc.
SupportedCultures = supportedCultures,
// UI strings that we have localized.
SupportedUICultures = supportedCultures
});
//app.UseRequestCulture();
Mapper.Initialize(config =>
{
config.CreateMap<ProductData, ProductDetailViewModel>();
});
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseIdentity();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
DbInitializer.Initialize(context);
}
和我有
/Resources/Views/Product/Details.resx
/Resources/Views/Product/Details.sk.resx
/Resources/Views/Product/Details.sk-SK.resx
资源文件和我的Details.cshtml
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer
@{
ViewData["Title"] = Localizer["Manufacturer"];
}
问题是,我总是从结果Details.resx,而不是Details.sk.resx或Details.sk-SK.resx
我做错了什么?谢谢
答
您需要在路由配置中映射新的路由。
routes.MapRoute(
name: "cultureRoute",
template: "{culture}/{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" },
constraints: new {
culture = new RegexRouteConstraint("^[a-z]{2}(?:-[A-Z]{2})?$") });
因此,路径路径如下所示。 -/
/EN
/家
/ES-ES /家
/首页/指数
/ES /首页/指数
/家庭/索引/ 123
/EN-US /家庭/索引/ 123