Skip to content

Commit

Permalink
translate input type button and submit
Browse files Browse the repository at this point in the history
  • Loading branch information
jemikanegara committed Nov 5, 2024
1 parent 8e2b910 commit c36e519
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
12 changes: 12 additions & 0 deletions utils/translation/modifyHtmlStrings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
16 changes: 15 additions & 1 deletion utils/translation/translateNodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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 (
Expand Down
10 changes: 9 additions & 1 deletion utils/translation/updateNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,22 @@ 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);
}
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)) {
Expand Down

0 comments on commit c36e519

Please sign in to comment.