我正在 Visual Studio 中构建然后运行我的 asp.net CORE 网站,一旦网站启动,我就会加载本地化资源,并将其设置为默认值,即英语。
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("sv-SE"),
new CultureInfo("en-GB"),
new CultureInfo("fr-FR")
};
// Explicitly state numbers/dates etc.
options.SupportedCultures = supportedCultures;
// Explicitly state UI strings (e.g. localised strings)
options.SupportedUICultures = supportedCultures;
// etc...
services.AddMvc().AddViewLocalization();
});
}
但是,当我将其发布到 Azure 并访问站点时,没有使用默认语言,它为我提供了所有 @SharedLocalizer
变量名称。如果我选择一种语言,一切都会按预期进行,所以我剩下两个问题:
1)在 VS 中运行我的网站时,如何/在哪里设置默认区域性?
2) 通过 Azure 发布时,如何让我的网站执行此操作?
我的 home/index Controller 中有这个方法,但是它根据用户选择设置语言 - 当我在 VS 中运行它时它是如何自动发生的?!
public IActionResult SetLanguage(string culture, string returnUrl)
{
CultureInfo ci = new CultureInfo(culture);
CultureInfo.DefaultThreadCurrentCulture = ci;
CultureInfo.DefaultThreadCurrentUICulture = ci;
return Redirect(returnUrl);
}
最佳答案
您没有为 RequestLocalizationOptions
设置 DefaultRequestCulture
。
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("sv-SE"),
new CultureInfo("en-GB"),
new CultureInfo("fr-FR")
};
// State what the default culture for your application is. This will be used if no specific culture
// can be determined for a given request.
options.DefaultRequestCulture = new RequestCulture("sv-SE"),
// Explicitly state numbers/dates etc.
options.SupportedCultures = supportedCultures;
// Explicitly state UI strings (e.g. localised strings)
options.SupportedUICultures = supportedCultures;
}
更新:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
//other middlewares
app.UseMVc();
}
关于c# - 如何在本地选择默认区域性 -(以及为什么不在 Azure 上运行时不选择),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58272176/