Reply To: Translation for the explanatory messages of eye-tracking module

PennController for IBEX Forums Support Translation for the explanatory messages of eye-tracking module Reply To: Translation for the explanatory messages of eye-tracking module

#7272
Jeremy
Keymaster

Hello,

All this is fetched from a distant page: you won’t find its content locally in your project on the PCIbex Farm

As a general solution, you can create a new .js file in your project’s Scripts folder, with this at the top:

function textNodesUnder(el){
  var n, a=[], walk=document.createTreeWalker(el,NodeFilter.SHOW_TEXT,null,false);
  while(n=walk.nextNode()) a.push(n);
  return a;
}
const targetNode = document.documentElement;
const config = { attributes: false, childList: true, subtree: true };
const replaceTexts = new Map();
const callback = function(mutationsList, observer) {
    if (!document.body) return;
    for(const mutation of mutationsList) {
        if (mutation.type === 'childList') {
            const textNodes = textNodesUnder(document.body);
            var key, keys = replaceTexts.keys();
            while ((key=keys.next()) && !key.done)
                for (let node of textNodes){
                    if (node.textContent)
                        node.textContent = node.textContent.replace(key.value, replaceTexts.get(key.value));
                }
        }
    }
};
const observer = new MutationObserver(callback);
observer.observe(targetNode, config);

Then you can list texts to replace below that piece of code this way:

replaceTexts.set(
    "It looks like we were not able to precisely calibrate the tracker:",
    "Il semble que nous n'avons pas pu calibrer le tracker précisément"
).set(
    /You calibration score is (\d+) and you need at least (\d+)/,
    "Votre score est de $1 et vous avez besoin d'au moins $2"
)

As you can see, you can pass a simple string (first set command) or a regular expression (second set command)

Jeremy