The Language does not change in the ASP.NET Core Web application

前端 未结 2 486
悲哀的现实
悲哀的现实 2021-01-14 07:49

I follow the Globalization and localization and Building simple multilingual ASP.NET Core website tutorials to add a language switch for my application.

So, I create

2条回答
  •  佛祖请我去吃肉
    2021-01-14 08:05

    Did you add javascript to make the language selector postback?

    (function () {
    $("#selectLanguage select").change(function () {
        $(this).parent().submit();
    });
    }());
    

    Did you wire up therequest localization middleware in Configure?

    var locOptions= app.ApplicationServices.GetService>();
    app.UseRequestLocalization(locOptions.Value);
    

    Did you add a method in HomeController like this:

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

提交回复
热议问题