I am creating a multi language website at www.fitco-consulting.com and have created buttons on the Header section which sets a language value in the local wix storage on the Site Code Page when I click it. (local.setItem(“siteLangPref”, lang)
I need a section of code running in a perpetual loop on the home page within the site to read that value from the wix storage so that the written text is in the correct language. How can I do this please someone?
Are you using Wix Multilingual for your multi language site?
https://support.wix.com/en/wix-multilingual/wix-multilingual-basics
SEO Info and Subdomains in Wix Multilingual.
https://support.wix.com/en/wix-multilingual/seo-in-wix-multilingual
Here is how I have created a message handler between site and page regions. As you read through I think it will make sense, if you’re an intermediate at wix coding:
Create a collection , needs no content. Set to Member Generated Content I call mine “MsgRouter”.
Datasets:
Datasets are used to invoke the .save() and .onBeforeSave(). I don’t save anything to the collection, I just use Save() and onBeforeSave() as event handlers.
Create a Dataset in the header, set to Read& Write. In my code I call it “dashboardDS”
Created a dataset in the body; set to Read&Write. Hover the Dataset icon, right click and select show on all pages . In my code I call it “pageDS”.
Memory variables
Use local memory variables to store the event information. I put two functions in my public folder; Get and Save Memory variables:
export function GetMemoryCm d(keep) {
let memObj = JSON.parse(memory.getItem(global.CMD_KEY));
//console.log(‘in get memory cmd’ + memObj);
if (keep) {} else
memory.removeItem(memObj); <=== note this removes the object from memory
return memObj;
}
export function SaveMemoryCmd (cmd, value) {
let memObj = MEMORYOBJ;
memObj.cmd = cmd;
if (value)
memObj.value = value;
memory.setItem(global.CMD_KEY, JSON.stringify(memObj));
return true;
}
Code for Save() in page or site
In the page or site code you invoke a save() on the Dataset for the site or page;
SaveMemoryCmd (global.LOGON_USER, collectionData.title);
$w(“#dashboardDS").save(); <=== or #pageDS
Code for onBeforeSave in page or site
$w.onReady(()
….
//site code receives this event from page code
$w(“#dashboardDS”). onBeforeSave (() => {
let memObj = GetMemoryCmd (); <=== memobject is your memory object from page/site
switch (memObj.cmd) {
case global.CHAINTITLE_CHG: <== whatever
……
break;
case global.LOGON_USER: <=== whatever
…
break;
}
})
});
It’s a bit of a hack, but it works like a champ for me, and took me quite a while to figure this out. I think Wix should really help out more with event handling across various layers of coding.
Cheers,
Tom
Tom
It is a stretch for me but I am following your instructions and have created the DashboardDS in the header and the PageDS in the body. I have then cut and pasted your 2 functions export function GetMemoryCm d(keep) { and export function SaveMemoryCmd (cmd, value) { and am getting an error which I attach in the picture which when I hover the mouse over it says "parsing error: unexpected token <==. Looking forward to your help. Cheers Cliff
I am happy with the using the collection to manage the translation as it lets me keep easy control of the content of the site
Hey Cliff, that is just a hand-written annotation and should not be in the syntax of the code. So just erase “<=== note this removes the object from memory”.
You’ll need to create your own object to store content in memory, in lines 87 and 94 and 98. It’s just a standard object. On line 98 you stringify the object to put into memory as a string, and on line 87 you JSON.parse to turn the memory string into an object.
This is example code from my website to help you with the coding within both the page and the site.
To go from site to page, you reverse the logic, that is put the onBeforeSave() on the pageDS
//this is page code, which saves a memory command, then
// then invokes the site-dataset's save function.
// this simulates a click of the logon user button in the header
// from code in a page
SaveMemoryCmd(global.LOGON_USER, collectionData.title);
$w("#dashboardDS").save();
//this is site code, which receives the event from page
// I have this in the OnReady() function
$w("#dashboardDS").onBeforeSave(() => {
let memObj = GetMemoryCmd();
switch (memObj.cmd) {
case global.LOGON_USER:
loggedonBox_click();
break;
}