ASP.NET Core 2.1本地化选项SupportedCultures始终仅包含英语

时间:2018-06-29 14:35:59

标签: c# asp.net-core localization asp.net-core-localization

我想在ASP.NET Core 2.1中设置本地化。我用6种支持的区域性构建了RequestLocalizationOptions对象并配置了服务,但是在检索这些选项(下面的代码示例中的localizationOptions对象)时,SupportedCultures和SupportedUICultures集合仅包含一种区域性。

public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<RequestLocalizationOptions>(opts => BuildLocalizationOptions());
        services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; });

        services.AddMvc()
            .AddViewLocalization(LanguageViewLocationExpanderFormat.SubFolder)
            .AddJsonOptions(opts => opts.SerializerSettings.ContractResolver = new DefaultContractResolver());
    }

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseStaticFiles();

        var localizationOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
        app.UseRequestLocalization(localizationOptions.Value);

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }

    private RequestLocalizationOptions BuildLocalizationOptions()
    {
        IList<CultureInfo> supportedCultures = new List<CultureInfo>
        {
            new CultureInfo("en-US"),
            new CultureInfo("en"),
            new CultureInfo("fr-FR"),
            new CultureInfo("fr"),
            new CultureInfo("ro-RO"),
            new CultureInfo("ro")
        };

        var options = new RequestLocalizationOptions
        {
            DefaultRequestCulture = new RequestCulture("en-US", "en-US"),
            SupportedCultures = supportedCultures,
            SupportedUICultures = supportedCultures
        };

        return options;
    }

任何帮助将不胜感激,也许我错过了一些东西。

更新:对不起,忘了包含BuildLocalizationOptions()方法。

1 个答案:

答案 0 :(得分:0)

我认为您想做类似的事情:

RequestLocalizationOptions localizationOptions = new RequestLocalizationOptions
{
    SupportedCultures = new List<CultureInfo> { new CultureInfo("en-GB") },
    SupportedUICultures = new List<CultureInfo> { new CultureInfo("en-GB") },
    DefaultRequestCulture = new RequestCulture("en-GB")
};
app.UseRequestLocalization(localizationOptions);