我按照Microsoft的博客中的描述设置了本地化,但默认语言始终是英语.这就是我的Startup.cs在本地化方面的样子.
CultureInfo[] supportedCultures = new[] { new CultureInfo("ar"),new CultureInfo("en") };
在ConfigureServices方法中:
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();
在Configure方法中:
app.UseRequestLocalization(new RequestLocalizationOptions() { DefaultRequestCulture = new RequestCulture("ar"),SupportedCultures = supportedCultures,SupportedUICultures = supportedCultures });
谢谢 :)
解决方法
您将“arabic”设置为DefaultRequestCulture,但如果没有内置提供程序可以确定请求文化,则使用DefaultRequestCulture.默认提供者是:
> QueryStringRequestCultureProvider
> CookieRequestCultureProvider
> AcceptLanguageHeaderRequestCultureProvider
最有可能的是,文化是根据浏览器发送的Accept-Language HTTP标头确定的.
您必须删除AcceptLanguageHeaderRequestCultureProvider才能回退到DefaultRequestCulture.为此,我们可以覆盖RequestLocuizationOptions的RequestCultureProviders列表,并仅使用其他两个提供程序.在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.UseRequestLocalization();在app.UseMvc()之前;