From c36e5194974bdde5e52c2d9e22b40810e03be2e4 Mon Sep 17 00:00:00 2001 From: jemikanegara Date: Tue, 5 Nov 2024 23:20:07 +0800 Subject: [PATCH] translate input type button and submit --- utils/translation/modifyHtmlStrings.js | 12 ++++++++++++ utils/translation/translateNodes.js | 16 +++++++++++++++- utils/translation/updateNode.js | 10 +++++++++- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/utils/translation/modifyHtmlStrings.js b/utils/translation/modifyHtmlStrings.js index 03addb4..5674c43 100644 --- a/utils/translation/modifyHtmlStrings.js +++ b/utils/translation/modifyHtmlStrings.js @@ -78,6 +78,18 @@ function modifyHtmlStrings(window, rootElement, language, apiKey, shouldOptimize ) } + // translate input type="submit" & input type="button" + const inputTypeButtonTags = Array.from(window.document.querySelectorAll('input[type="button"]')); + const inputTypeSubmitTags = Array.from(window.document.querySelectorAll('input[type="submit"]')); + const cleanInputTypeButtonTags = inputTypeButtonTags.filter((node) => (node.value || "").trim() && !isExcludedClassName(window,node.className) && !isExcludedId(window, node.id)); + const cleanInputTypeSubmitTags = inputTypeSubmitTags.filter((node) => (node.value || "").trim() && !isExcludedClassName(window,node.className) && !isExcludedId(window, node.id)); + + otherNodes.push( + ...cleanInputTypeButtonTags, + ...cleanInputTypeSubmitTags, + ) + + const textNodes = []; extractTextNodes(window, rootElement, textNodes); diff --git a/utils/translation/translateNodes.js b/utils/translation/translateNodes.js index 4739f65..4f51c17 100644 --- a/utils/translation/translateNodes.js +++ b/utils/translation/translateNodes.js @@ -167,7 +167,7 @@ function translateNodes(window, textNodes = [], language = "", apiKey = "", seoN otherNodes.forEach((node) => { const allTranslationValuesInAllPages = Object.values(window.translationCache).map(x => Object.values(x[language] || {})) - if (node.tagName == "TEXTAREA" || node.tagName == "INPUT") { + if (node.tagName == "TEXTAREA" || (node.tagName == "INPUT" && node.type != "button" && node.type != "submit")) { const placeholderCache = window.translationCache?.[window.location.pathname]?.[language]?.[node.placeholder] // make sure the placeholder is not empty if ( @@ -181,6 +181,20 @@ function translateNodes(window, textNodes = [], language = "", apiKey = "", seoN } } + if(node.tagName == "INPUT" && (node.type == "button" || node.type == "submit")) { + const valueCache = window.translationCache?.[window.location.pathname]?.[language]?.[node.value] + // make sure the value is not empty + if ( + (node.value || "").trim() && !valueCache && !allTranslationValuesInAllPages.includes(node.value) + ) { + notInCache.push(node.value); + } + + if (valueCache) { + updateNode(window, node, language, "form", 5.20); + } + } + if(node.tagName == "OPTION") { const cache = window.translationCache?.[window.location.pathname]?.[language]?.[node.textContent] if ( diff --git a/utils/translation/updateNode.js b/utils/translation/updateNode.js index e1b8a4f..ae8e42c 100644 --- a/utils/translation/updateNode.js +++ b/utils/translation/updateNode.js @@ -54,7 +54,7 @@ function updateNode(window, node, language, type = "text", debugSource) { return; } - if (type == "form" && (node.tagName == "TEXTAREA" || node.tagName == "INPUT")) { + if (type == "form" && (node.tagName == "TEXTAREA" || (node.tagName == "INPUT" && node.type != "button" && node.type != "submit"))) { const newPlaceholder = window.translationCache?.[window.location.pathname]?.[language]?.[node.placeholder] || ""; if (newPlaceholder && !newPlaceholder.includes(DEFAULT_UNTRANSLATED_VALUE)) { node.placeholder = decodeHTMLEntities(window, newPlaceholder); @@ -62,6 +62,14 @@ function updateNode(window, node, language, type = "text", debugSource) { return; } + if (type == "form" && (node.tagName == "INPUT" && (node.type == "button" || node.type == "submit"))) { + const newValue = window.translationCache?.[window.location.pathname]?.[language]?.[node.value] || ""; + if (newValue && !newValue.includes(DEFAULT_UNTRANSLATED_VALUE)) { + node.value = decodeHTMLEntities(window, newValue); + } + return; + } + if (type == "form" && node.tagName == "OPTION") { const newText = window.translationCache?.[window.location.pathname]?.[language]?.[node.textContent] || ""; if (newText && !newText.includes(DEFAULT_UNTRANSLATED_VALUE)) {