我已經通過以下實現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