Skip to content

Commit

Permalink
Updated translate.js to latest version
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro270707 committed Jun 14, 2024
1 parent 8430ccc commit 1db5ff7
Showing 1 changed file with 52 additions and 33 deletions.
85 changes: 52 additions & 33 deletions js/translate.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -101,6 +79,47 @@ class Translate {
getKeyWrapped(key, ...args) {
return `<span data-translate-string='${JSON.stringify(new TranslatableText(key, ...args))}'>${(this.translateString(key, ...args))}</span>`;
}

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();
Expand All @@ -124,4 +143,4 @@ function changeLanguage(language) {
} else {
console.error("The new URL's origin does not match the current origin");
}
}
}

0 comments on commit 1db5ff7

Please sign in to comment.