I have 2 languages on my website, with Wix Multilingual. I’ve embedded HTML element (a call-to-action button) and want it to be show on current website language.
For this, I’ve copied HTML element and made 2 versions of it for each language.
I tried to use the following code to show/hide the right version of HTML element when the user switched the language.
But it seems that onReady event is only triggered when page is re-loaded from scratch, while after changing language, page reloads only partially (texts and so on).
That’s why to reflect this script I need to go to another page and then return to initial one.
Could anybody advise on a workaround here how to implement this simple while very much needed functionality smoothly?
Moreover, I tried the following script that I use to manage a repeater when language changes and also added language handling for HTML element:
export function repeater7_itemReady_1 ( $item ) {
//repeater handling
let language = wixWindow . multilingual . currentLanguage ;
$w ( “#dataset2” ). setFilter ( wixData . filter (). eq ( “language” , language ));
What is triggering the language change after the page loads? That is where you will be able to also trigger element changes.
The code you have currently written will only be evaluate on page load (as you stated)
There is an example in the docs of using a click event to trigger a language change - if you are doing something like that you could also manipulate elements on this change as well.
Thanks for your attention and advice!
Now, I’m using standard language menu element like this:
I also tried suggestions about recoding dropdown from scratch to be able to use onChange event, that stated via link you sent.
Here is a code I did:
function switchLang ( ) { switch ( wixWindow . multilingual . currentLanguage )
{ case “en” : {
//showing current lang in dropdown element
$w ( ‘#dropdown1’ ). selectedIndex = 0 ;
//this supposed to hide/show right HTML element
$w ( ‘#html1’ ). show ();
$w ( ‘#html2’ ). hide ();
break ;
} case “uk” : {
$w ( ‘#dropdown1’ ). selectedIndex = 1 ;
$w ( ‘#html1’ ). hide ();
$w ( ‘#html2’ ). show ();
break ;
}
}
}
$w . onReady ( function () {
//from Example
const languages = wixWindow . multilingual . siteLanguages ;
//Here I changed to uppercase label rather than full lang name
let languageOptions = languages . map (( obj ) => { return { label : obj . languageCode . toUpperCase (), value : obj . languageCode };
});
//sets dropdown value on initial page loading and hides one of HTML elements
switchLang ();
//I'm not using filteredLanguageOptions here because it doesn't allow dropdown to display current lang
$w ( '#dropdown1' ). options = languageOptions ;
$w ( "#dropdown1" ). onChange (( event ) => {
wixWindow . multilingual . currentLanguage = event . target . value ;
**//on dropdown Change, I call** **switchLang() to do its work**
switchLang ();
});
});
As a result, it looks like “hide” part works well but not a “show” one. No one of HTML elements is shown after 2 back and forth lang switches.