ASP .NET Core默认语言始终为英语 [英] ASP .NET Core default language is always English
问题描述
我按照Microsoft博客中的说明设置了本地化,但是默认语言始终是英语.这是我的Startup.cs在本地化方面的样子.
I set the localization as described in Microsoft's blog, but the default language is always English. This is how my Startup.cs looks like with regards to the localization.
CultureInfo[] supportedCultures = new[]
{
new CultureInfo("ar"),
new CultureInfo("en")
};
在ConfigureServices方法中:
In ConfigureServices method:
services.Configure<RequestLocalizationOptions>(options =>
{
options.DefaultRequestCulture = new RequestCulture("ar", "ar");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
services.AddLocalization(options =>
{
options.ResourcesPath = "Resources";
});
services.AddMvc()
.AddViewLocalization()
.AddDataAnnotationsLocalization();
在配置"方法中:
app.UseRequestLocalization(new RequestLocalizationOptions()
{
DefaultRequestCulture = new RequestCulture("ar"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
});
谢谢:)
推荐答案
您将阿拉伯语"设置为DefaultRequestCulture
,但是如果没有任何内置提供程序可以确定请求区域性,则使用DefaultRequestCulture
.默认提供程序为:
You are setting "arabic" as DefaultRequestCulture
but DefaultRequestCulture
is used if none of the built-in providers can determine the request culture. The default providers are:
-
QueryStringRequestCultureProvider
-
CookieRequestCultureProvider
-
AcceptLanguageHeaderRequestCultureProvider
QueryStringRequestCultureProvider
CookieRequestCultureProvider
AcceptLanguageHeaderRequestCultureProvider
很有可能是根据浏览器正在发送的Accept-Language HTTP标头确定区域性的.
Most likely the culture is determined from the Accept-Language HTTP header that the browser is sending.
您必须删除AcceptLanguageHeaderRequestCultureProvider
才能回退到DefaultRequestCulture
.为此,我们可以覆盖RequestLocalizationOptions
的RequestCultureProviders
列表,并仅使用其他两个提供程序.在Startup.cs
:
You have to remove the AcceptLanguageHeaderRequestCultureProvider
in order to fallback to DefaultRequestCulture
. To do that, we can overwrite the RequestCultureProviders
list of RequestLocalizationOptions
and use only the other two providers. In Startup.cs
:
public void ConfigureServices(IServiceCollection services)
{
CultureInfo[] supportedCultures = new[]
{
new CultureInfo("ar"),
new CultureInfo("en")
};
services.Configure<RequestLocalizationOptions>(options =>
{
options.DefaultRequestCulture = new RequestCulture("ar");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.RequestCultureProviders = new List<IRequestCultureProvider>
{
new QueryStringRequestCultureProvider(),
new CookieRequestCultureProvider()
};
});
}
和Configure
方法中,只需在app.UseMvc();
这篇关于ASP .NET Core默认语言始终为英语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!