作者:手机用户2602898555 | 来源:互联网 | 2023-02-01 15:54
我按照微软博客中的描述设置了本地化,但默认语言始终是英语.这就是我的Startup.cs在本地化方面的样子.
CultureInfo[] supportedCultures = new[]
{
new CultureInfo("ar"),
new CultureInfo("en")
};
在ConfigureServices方法中:
services.Configure(optiOns=>
{
options.DefaultRequestCulture = new RequestCulture("ar", "ar");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
services.AddLocalization(optiOns=>
{
options.ResourcesPath = "Resources";
});
services.AddMvc()
.AddViewLocalization()
.AddDataAnnotationsLocalization();
在Configure方法中:
app.UseRequestLocalization(new RequestLocalizationOptions()
{
DefaultRequestCulture = new RequestCulture("ar"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
});
谢谢 :)
1> tmg..:
您将"阿拉伯语"设置为,DefaultRequestCulture
但DefaultRequestCulture
如果没有内置提供程序可以确定请求文化,则使用"阿拉伯语" .默认提供者是:
QueryStringRequestCultureProvider
COOKIERequestCultureProvider
AcceptLanguageHeaderRequestCultureProvider
最有可能的是,文化是根据浏览器发送的Accept-Language HTTP标头确定的.
您必须删除AcceptLanguageHeaderRequestCultureProvider
以便回退DefaultRequestCulture
.为此,我们可以覆盖RequestCultureProviders
列表RequestLocalizationOptions
并仅使用其他两个提供程序.在Startup.cs
:
public void ConfigureServices(IServiceCollection services)
{
CultureInfo[] supportedCultures = new[]
{
new CultureInfo("ar"),
new CultureInfo("en")
};
services.Configure(optiOns=>
{
options.DefaultRequestCulture = new RequestCulture("ar");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.RequestCultureProviders = new List
{
new QueryStringRequestCultureProvider(),
new COOKIERequestCultureProvider()
};
});
}
并且在之前Configure
使用的方法app.UseRequestLocalization();
app.UseMvc();