始终相同的文化:美国 [英] Always same culture: en-US

查看:82
本文介绍了始终相同的文化:美国的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net核心项目,我想将默认语言设置为nl-BE.由于某种原因,它总是采用美国英语

I have an asp.net core project where I want to set the default language to nl-BE. For some reason it always take the language en-US

请参见下面的代码(ps:我创建了自己的ApplicationLocalizer,后者从数据库中获取了资源=>可以正常工作).

See code below (ps: I created my own ApplicationLocalizer who fetched the resources from a database => works fine).

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
            //Add MVC
            services.AddMvc()
                .AddViewLocalization();

            //Localization factory
            services.AddSingleton<IStringLocalizerFactory, ApplicationLocalizerFactory>();

            ...
}


public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, ApplicationDbContext context)
{
            ...

            //Localization
            var supportedCultures = new[]
                {
                    new CultureInfo("en-US"),
                    new CultureInfo("nl-BE")
                };
            var options = new RequestLocalizationOptions {
                DefaultRequestCulture = new RequestCulture("nl-BE"),
                SupportedCultures = supportedCultures,
                SupportedUICultures = supportedCultures
            };
            app.UseRequestLocalization(options);

            ...
}

控制器

public string Index()
{
    return CultureInfo.CurrentCulture.Name;
}

此代码段始终返回 zh-CN

有人可以帮我解决这个问题吗?

Can someone help me solve this problem?

推荐答案

经过数小时的无结果搜索解决方案后,我进行了大量调试,并意识到中间件服务是按顺序执行的 Configure(IApplicationBuilder app ...)中声明.因此,在执行控制器操作后, 会调用本地化中间件.

After hours of searching for solution with no result, I went for heavy debugging and realized that the middleware services are executed in an order as declared in the Configure(IApplicationBuilder app...). Therefore the localization middleware was invoked after the controller action was executed.

当我一开始移动 app.UseRequestLocalization(options)调用时,我就在控制器操作中正确地本地化了Thread/CultureInfo.

When I moved the app.UseRequestLocalization(options) invocation at the very beginning I got properly localized Thread/CultureInfo in controller actions.

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory,
        IApplicationLifetime appLifetime)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        // THIS HAS TO BE A VERY FIRST MIDDLEWARE REGISTRATION
        SetUpLocalization(app);

        app.UseCors("AllowAllOrigins");

        app.UseMvc();

        appLifetime.ApplicationStopped.Register(() => ApplicationContainer.Dispose());
    }

    private static void SetUpLocalization(IApplicationBuilder app)
    {
        var supportedCultures = new[]
        {
            new CultureInfo("en-US"),
            new CultureInfo("pl-PL"),
            new CultureInfo("pl")
        };

        var options = new RequestLocalizationOptions
        {
            DefaultRequestCulture = new RequestCulture("en-US", "en-US"),
            SupportedCultures = supportedCultures,
            SupportedUICultures = supportedCultures
        };
        // Configure the Localization middleware
        app.UseRequestLocalization(options);
    }

这篇关于始终相同的文化:美国的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆