One button to go home & change currentLanguage?

Greetings,

I do realise that there’ve been threads on one button for two functions but the suggestions there didn’t really help.

I’m trying to create a button which onClick would change the currentLanguage AND go to the home page (kind of a hard reset button to counter the multilingual flaws (dynamic pages, databases)).

import wixWindow from 'wix-window';
import wixLocation from 'wix-location';

$w.onReady(function () {

    $w("#languageEN").onClick((event) => {
 if (wixWindow.multilingual.currentLanguage === "en") {
            wixWindow.multilingual.currentLanguage = "ja";
        } else {
            wixWindow.multilingual.currentLanguage = "en";
        }
    })
    $w('#languageEN').link = "/home";
});

With the above you either get redirected to /home with no language change, or if you are already on the home page, only then the language change occurs.

The code is in the Site Code tab.

I’ve also tried ↓ and some other variations but the result was the same as above.

export function languageEN_click(event) {
    wixWindow.multilingual.currentLanguage = "ja";
    wixLocation.to("/home");
}

Any heads up?

Hi,

I’d suggest you’d first make sure your secondary language has been enabled since it’s usually disabled by default. Click here for instructions.
Once you’ve enabled your secondary languages you can try adding the wixLocation.to() with the complete url within the if statement. I was able to make it work with the following code:

You can also check out the url to see it working on the live site.
I hope this was helpful!

Good luck!

Works like a charm on the live site.

Bless you, Miguel!