Base solution for your next web application
Open Closed

Cannot change Languages through Language select drop down #2843


User avatar
0
jcompagnon created

Hello,

I'm currently running into a problem where attempting to change a user's culture from the language dropdown list is failing.

I can see that the call to the AbpLocalizationController's 'ChangeCulture' is going through, but the request reloads the dashboard page in the same language, rather than updating to the new selection.

This issue only appeared after a Clean & Rebuild operation - prior to this, it worked, so I'm not sure why the this would be behaving any differently.

I believe the issue is coming from the setting of the Abp.Localization.CultureName cookie - When inspecting the cookies in Chrome, I can see that the a cookie for the new language is added; however, the previous cookie still exists, and has a "Path" set to the application directory - causing the old language to still be used as the culture. I've tried clearing my cache, removing the cookies, and

I'm not sure how to correct this - from my project, the only reference I see to adding this cookie is done in the web project's Global.asax "RestoreUserLanguage" method:

private void RestoreUserLanguage()
        {
            var settingManager = AbpBootstrapper.IocManager.Resolve<ISettingManager>();
            var defaultLanguage = settingManager.GetSettingValue(LocalizationSettingNames.DefaultLanguage);

            if (defaultLanguage.IsNullOrEmpty())
            {
                return;
            }

            try
            {
                CultureInfo.GetCultureInfo(defaultLanguage);
                Response.Cookies.Add(new HttpCookie("Abp.Localization.CultureName", defaultLanguage) { Expires = Clock.Now.AddYears(2)});
            }
            catch (CultureNotFoundException exception)
            {
                LogHelper.Logger.Warn(exception.Message, exception);
            }
        }

I've registered the languages to be used in the EntityFramework's DefaultLanguagesCreator() method, and re-ran update-database in that project to ensure the DB was seeded appropriately.

Is there something I'm missing to allow for swapping languages? Thanks in advance for any help/advice!


9 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Are you using AspNet Core version ?

  • User Avatar
    0
    jcompagnon created

    Hello!

    No, currently we're using the MVC w/ JQuery setup.

  • User Avatar
    0
    jcompagnon created

    My apologies - I'm using ASP.NET Zero configured for MPA when running into this issue.

    Separately, I've tried installing the base MVC 5.X w/jQuery to see if I could replicate the issue, and in that project, I also see duplicate culture cookies after trying to switch languages unsuccessfully.

  • User Avatar
    0
    jcompagnon created

    Hello,

    Are there any updates on this? I'm circling back to the issue every few days to see if I can approach it another way, but I'm still stuck with duplicating cookies. Any help would be greatly appreciated!

  • User Avatar
    0
    jcompagnon created

    Hello again,

    I believe I've got a lead on why I'm receiving duplicated cookies for the current culture, but I'm not sure where to go from here.

    While looking over the source code on Github for the Abp.Web & Abp.Web.Mvc modules, I see 2 places that a 'CultureName' cookie is set - one in Abp.Web.Localization.CurrentCultureSetter, and the other in the AbpLocalizationController's ChangeCulture call inside Abp.Web.Mvc.

    I think the issue I'm facing is due to the fact that the CurrentCultureSetter's method specifies "Path = [application path]", whereas the ChangeCulture call does not specify a path - but then again, if this is the issue, I don't understand how it was worknig for me previously.

    I'd appreciate any input or guidance on this. In the meantime, thanks again, and good job on the clean framework!

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Yes, probably the problem causes because of cookie paths. Can you search for "abp.utils.setCookieValue" in your project ? I remember we had a similar issue but cannot remember what was it exactly. But as I remember, we changed a few lines related to "abp.utils.setCookieValue" in the client side.

    Thanks.

  • User Avatar
    0
    jcompagnon created

    Hello, and thanks for the response!

    I combed through my project for the "abp.utils.setCookieValue" function, and saw that it gets called a few times to set the Tenant and Authorization cookies; I don't see it being called for changing the Culture, however. It looks like, instead, the cookie is only being set in the AbpLocalization controller when a call is made to "AbpLocalization/ChangeCulture" in the "_Header.cshtml" file, so what I've done is re-created a controller based on AbpLocalizationController whose "ChangeCulture" method specifies a Path as the request's ApplicationPath, in the same way the CurrentCultureSetter class does. This allows me to change cultures as desired, but I know it's pretty hacky, since it's basically just redefining a framework class for the sake of changing this Path setting. Do you have any suggestions on how else I might achieve this? Thanks again for the help!

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Thanks for your feedback. This seems like a bug and we have fixed it here <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/commit/d9560634b646226c417db7a9f6c852e3f1ad4bbb">https://github.com/aspnetboilerplate/as ... e3f1ad4bbb</a>.

    You can upgrade to latest ABP or you can use your current solution. It is better to upgrade ABP but it might need some extra work.

    Thanks.

  • User Avatar
    0
    jcompagnon created

    Ah, that's great!

    Thank you both for looking into this, and again for the awesome framework. I'll definitely be upgrading to 2.x in the near future; thanks for the heads up!