asp.net-core – ASP .NET Core默认语言总是英文

前端之家收集整理的这篇文章主要介绍了asp.net-core – ASP .NET Core默认语言总是英文前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我按照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()之前;

猜你在找的.NET Core相关文章