Skip to content

Commit

Permalink
replace nodes earlier
Browse files Browse the repository at this point in the history
  • Loading branch information
jemikanegara committed May 20, 2024
1 parent 81a9cb5 commit da79f65
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ if (isBrowser()) {
})();
}

function updateNode(node, language) {
const text = node.textContent;
const newText = window.translationCache?.[window.location.pathname]?.[language]?.[text] || "";

if(newText && !newText.includes("weploy-untranslated")) {
// make sure text is still the same before replacing
if(node.textContent == text) {
node.textContent = newText;
}
}
}

function filterValidTextNodes(textNodes) {
return textNodes.filter((textNode) => {
const textContent = textNode.textContent
Expand Down Expand Up @@ -112,6 +124,8 @@ function processTextNodes(textNodes = [], language = "", apiKey = "") {
&& !cacheValues.includes(text) // check in value (to handle nodes that already translated)
) {
notInCache.push(text); // If not cached, add to notInCache array
} else {
updateNode(node, language)
}
});

Expand Down Expand Up @@ -146,14 +160,7 @@ function processTextNodes(textNodes = [], language = "", apiKey = "") {

// Update textNodes from the cache
cleanTextNodes.forEach((node) => {
const text = node.textContent;
const newText = window.translationCache?.[window.location.pathname]?.[language]?.[text] || ""
if(newText && !newText.includes("weploy-untranslated")) {
// make sure text is still the same before replacing
if(node.textContent == text) {
node.textContent = newText;
}
}
updateNode(node, language)
});

if (isBrowser()) window.localStorage.setItem("translationCachePerPage", JSON.stringify(window.translationCache));
Expand All @@ -172,14 +179,8 @@ function processTextNodes(textNodes = [], language = "", apiKey = "") {
if ((window.translationCache?.[window.location.pathname]?.[language]?.[text] || "").includes("weploy-untranslated")) {
window.translationCache[window.location.pathname][language][text] = undefined;
}

const newText = window.translationCache?.[window.location.pathname]?.[language]?.[text] || ""
if(newText && !newText.includes("weploy-untranslated")) {
// make sure text is still the same before replacing
if(node.textContent == text) {
node.textContent = newText;
}
}

updateNode(node, language)
});

if (isBrowser() && !getIsTranslationInitialized()) window.localStorage.setItem("translationCachePerPage", JSON.stringify(window.translationCache));
Expand All @@ -195,6 +196,8 @@ function modifyHtmlStrings(rootElement, language, apiKey) {

const validTextNodes = filterValidTextNodes(textNodes) || [];

console.log("validTextNodes", validTextNodes)

await processTextNodes(validTextNodes, language, apiKey).then(() => {
setIsTranslationInitialized(true);
}).catch(reject).finally(() => {
Expand Down

0 comments on commit da79f65

Please sign in to comment.