我已经通过以下实现RequestLocalization
为es-ES
与单个MVC视图(注:此代码冷凝,只有最相关的片段):.NET核心MVC RequestLocalization忽略DefaultRequestCulture
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix,
opts =>
{
opts.ResourcesPath = "Resources";
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
var english = "en-US";
var englishRequestCulture = new RequestCulture(culture: english, uiCulture: english);
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-US"),
new CultureInfo("es-ES")
};
var options = new RequestLocalizationOptions
{
DefaultRequestCulture = englishRequestCulture,
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
};
app.UseRequestLocalization(options);
app.UseMvc();
}
当通过culture=en-US
或culture=es-ES
作为查询字符串参数时,这可以很好地工作。我的期望是,当没有文化提供时,默认文化应该是en-US
。但是,当我不提供文化参数时,我的观点默认为es-ES
。我已确认所有其他本地化提供商也默认为en-US
。
我也应该注意到,我通过ConfigureServices()
试图本地化,但未能得到这所有功能:
services.Configure<RequestLocalizationOptions>(
options =>
{
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-US"),
new CultureInfo("es-ES")
};
options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
这是不是这样对我,我证实了没文化的值都设置在管道的任何一点。 – alan