locked
Unable to change the language RRS feed

  • Question

  • User-1140746197 posted

    Hi, I have built a drop down list in the /Views/Shared/_LoginPartial.cshtml for changing the website language. However, after clicking the save button, the page seems not responding....

    I would like to ask if I have made anything wrong in my code below

    My whole project is also placed in https://github.com/thgtang3/project-for-study

          <form class="form-inline" asp-controller="SetLanguage" asp-action="SetLanguage" asp-route-returnUrl="@Context.Request.Path" method="post" role="form">
                        <label class="form-label" style="padding-right:5px">@SharedLocalizer.GetLocalizedHtmlString("LbLanguage"): </label>
                        <div class="input-group">
    
                            <select class="custom-select" name="culture" asp-for="@requestCulture.RequestCulture.UICulture.Name" asp-items="cultureItems"></select>
                            <div class="input-group-append">
                                <button type="submit" class="btn btn-outline-secondary">@SharedLocalizer.GetLocalizedHtmlString("BtnSave")</button>
                            </div>
                        </div>
    </form>

    Thanks for your help.

    Wednesday, November 6, 2019 7:18 AM

All replies

  • User-17257777 posted

    Hi thtang,

    I download your project, but it seems not to be complete, the controllers are missing. 

    Besides, It doesn't  respond since the resource files are not matched. The .resx files in the Resources folder are named incorrectly. They must begin with SharedResource

    After adding the HomeController and SetLanguageController, I made a test:

    public class SetLanguageController : Controller
        {
            [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);
            }
        }

    Result:

    For more details, you can refer to

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-3.0#implement-a-strategy-to-select-the-languageculture-for-each-request

    Best Regards,

    Jiadong Meng

    Thursday, November 7, 2019 8:21 AM