[英]How is a default culture selected locally - (and why not when running on Azure)
I'm building then running my asp.net CORE site in visual studio, and I get my localised resources loading as soon as the site launches, set to the default ie the English.我正在构建然后在 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();
});
}
However, when I publish this to Azure and visit site, there is no default language engaged, it gives me all the @SharedLocalizer
variable names.但是,当我将其发布到 Azure 并访问站点时,没有使用默认语言,它为我提供了所有@SharedLocalizer
变量名称。 If I then select a language, everything works as intended so I'm left with two questions:如果我然后 select 一种语言,一切都按预期工作,所以我有两个问题:
1) When running my site in VS how / where is it setting the default culture? 1)在VS中运行我的网站时,它如何/在哪里设置默认文化?
2) How can I get my site to do this when published via Azure? 2) 通过 Azure 发布时,如何让我的网站执行此操作?
I've got this method in my home/index controller but this sets language based on user selection - how is it happening automatically when I'm running it in VS?!我在我的家/索引 controller 中有这个方法,但这会根据用户选择设置语言 - 当我在 VS 中运行它时它是如何自动发生的?!
public IActionResult SetLanguage(string culture, string returnUrl)
{
CultureInfo ci = new CultureInfo(culture);
CultureInfo.DefaultThreadCurrentCulture = ci;
CultureInfo.DefaultThreadCurrentUICulture = ci;
return Redirect(returnUrl);
}
You do not set DefaultRequestCulture
for RequestLocalizationOptions
.您没有为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;
}
Refer to https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-3.0#localization-middleware参考https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-3.0#localization-middleware
Update:更新:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
//other middlewares
app.UseMVc();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.