【问题标题】:ASP.NET Core 2.1 Localization Options SupportedCultures always contains only EnglishASP.NET Core 2.1 本地化选项 SupportedCultures 始终只包含英文
【发布时间】:2018-06-29 14:35:59
【问题描述】:

我想在我的 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() 方法。

【问题讨论】:

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


    【解决方案1】:

    我认为你想做这样的事情:

    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);
    

    【讨论】:

    • 我已经更新了代码,忘记包含执行该操作的 BuildLocalizationOptions() 方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    相关资源
    最近更新 更多