【发布时间】:2018-06-29 14:35:59
【问题描述】:
我想在我的 ASP.NET Core 2.1 中设置本地化。我使用 6 种支持的文化构建 RequestLocalizationOptions 对象并配置服务,但是在检索这些选项(下面代码示例中的 localizationOptions 对象)时,SupportedCultures 和 SupportedUICultures 集合仅包含一种文化。
public void ConfigureServices(IServiceCollection services)
{
services.Configure<RequestLocalizationOptions>(opts => BuildLocalizationOptions());
services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; });
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.SubFolder)
.AddJsonOptions(opts => opts.SerializerSettings.ContractResolver = new DefaultContractResolver());
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseStaticFiles();
var localizationOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(localizationOptions.Value);
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
private RequestLocalizationOptions BuildLocalizationOptions()
{
IList<CultureInfo> supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-US"),
new CultureInfo("en"),
new CultureInfo("fr-FR"),
new CultureInfo("fr"),
new CultureInfo("ro-RO"),
new CultureInfo("ro")
};
var options = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US", "en-US"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
};
return options;
}
任何帮助将不胜感激,也许我遗漏了一些东西。
更新:抱歉,忘记包含 BuildLocalizationOptions() 方法。
【问题讨论】:
标签: c# asp.net-core localization asp.net-core-localization