IViewLocalizer Localizer .net核心在运行时未更新(未切换)

时间:2019-02-09 12:50:23

标签: asp.net-core .net-core localization razor-pages

我对asp.net核心2.2 Razro Pages应用的本地化感到困惑。

我尝试使用LocalizationCultureCore引入本地化,以将资源文件存储在.json中,而不是.resx。

当我第一次启动Webapp时,它看起来工作正常-所有标签都已本地化,但是如果我在运行时使用cookie更新本地化(更改语言),则不会在.htmlcs或.cs OnPost中更新注入的Localizer方法,使用以前的语言传递旧标签

但是,System.Globalization.CultureInfo.CurrentCulture.ToString()和System.Globalization.CultureInfo.CurrentUICulture.ToString()在更新后返回正确的值。

在我的Startup.cs中有以下内容(摘要):

public void ConfigureServices(IServiceCollection services)
        {
//other stuff

            services.Configure<RequestLocalizationOptions>(
                opts =>
                {
                    var supportedCultures = new List<CultureInfo>
                    {
                new CultureInfo("en-US"),
                new CultureInfo("ru-RU"),
                    };

                    opts.DefaultRequestCulture = new RequestCulture("en-US");
                    // Formatting numbers, dates, etc.
                    opts.SupportedCultures = supportedCultures;
                    // UI strings that we have localized.
                    opts.SupportedUICultures = supportedCultures;
                });

            services.AddJsonLocalization(options => options.ResourcesPath = "Resources");

            services.AddMvc().AddViewLocalization(options => { options.ResourcesPath = "Resources"; })
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

//other stuff
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
//other stuff

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

//other stuff
}

在index.cs的OnPost方法中(当用户在下拉列表中选择另一种语言时调用)

    public IActionResult OnPost(string culture, string returnUrl)
    {
        Response.Cookies.Append(
            CookieRequestCultureProvider.DefaultCookieName,
            CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
            new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
        );
        return LocalRedirect(returnUrl);
    }

可能是什么问题?有没有办法更新Localizer?

0 个答案:

没有答案