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
});
谢谢:)
推荐答案
您正在将 "arabic" 设置为 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
文化很可能是由浏览器发送的 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();
and in Configure
method just use app.UseRequestLocalization();
before app.UseMvc();
这篇关于ASP .NET Core 默认语言始终为英语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!