From 1db5ff71ea04cd95dce7bec41c1b1030ae3c61c8 Mon Sep 17 00:00:00 2001 From: Pedro Date: Thu, 13 Jun 2024 22:41:59 -0300 Subject: [PATCH] Updated translate.js to latest version --- js/translate.js | 85 ++++++++++++++++++++++++++++++------------------- 1 file changed, 52 insertions(+), 33 deletions(-) diff --git a/js/translate.js b/js/translate.js index 907c4ba..e297a8d 100644 --- a/js/translate.js +++ b/js/translate.js @@ -1,28 +1,5 @@ -const urlParams = new URLSearchParams(window.location.search); -switch (urlParams.get('lang')) { - case 'en-us': - case 'en-uk': - case 'en': - localStorage.language = 'en'; - break; - case 'br': - case 'pt-br': - case 'pt': - localStorage.language = 'pt'; - break; - default: - if (urlParams.get('lang')) { - localStorage.language = urlParams.get('lang'); - } - break; -} - -if (localStorage.language === undefined) { - localStorage.language = Translate.DEFAULT_LANGUAGE; -} - class Translate { - static DEFAULT_LANGUAGE = 'en'; + static DEFAULT_LANGUAGE = 'pt'; constructor(language) { this.language = language; @@ -82,15 +59,16 @@ class Translate { this.file = await fetch('/language/' + (this.language || localStorage.language) + '.json'); this.file = await this.file.clone().json(); } - let allElements = document.getElementsByTagName('*'); - for (let currentElement of allElements) { - for (let attribute of currentElement.attributes) { - if (attribute.name === "data-translate-string") { - currentElement.innerHTML = getTextFromJSON(attribute.value).get(); - } else if (attribute.name.startsWith("data-translate-")) { - currentElement[attribute.name.substring("data-translate-".length)] = getTextFromJSON(attribute.value).get(); - } + let reloadElements = () => { + let allElements = document.getElementsByTagName('*'); + for (let currentElement of allElements) { + this.#updateElement(currentElement); } + }; + if (document.readyState === 'loading') { + document.addEventListener("DOMContentLoaded", reloadElements); + } else { + reloadElements(); } for (let listener of this.changeListeners) { listener(); @@ -101,6 +79,47 @@ class Translate { getKeyWrapped(key, ...args) { return `${(this.translateString(key, ...args))}`; } + + setAttribute(element, attribute, text) { + if (text instanceof LiteralText || text instanceof TranslatableText) { + text = JSON.stringify(text); + } + element.setAttribute("data-translate-" + attribute, text); + this.#updateElement(element); + } + + #updateElement(element) { + for (let attribute of element.attributes) { + if (attribute.name === "data-translate-string") { + element.innerHTML = getTextFromJSON(attribute.value).get(); + } else if (attribute.name.startsWith("data-translate-")) { + element.setAttribute(attribute.name.substring("data-translate-".length), getTextFromJSON(attribute.value).get()); + } + } + } +} + +const urlParams = new URLSearchParams(window.location.search); +switch (urlParams.get('lang')) { + case 'en-us': + case 'en-uk': + case 'en': + localStorage.language = 'en'; + break; + case 'br': + case 'pt-br': + case 'pt': + localStorage.language = 'pt'; + break; + default: + if (urlParams.get('lang')) { + localStorage.language = urlParams.get('lang'); + } + break; +} + +if (localStorage.language === undefined) { + localStorage.language = Translate.DEFAULT_LANGUAGE; } var translate = new Translate(); @@ -124,4 +143,4 @@ function changeLanguage(language) { } else { console.error("The new URL's origin does not match the current origin"); } -} +} \ No newline at end of file