Convert callbacks to arrow functions

This commit is contained in:
Filip Troníček 2020-12-07 16:07:32 +01:00
parent e9a4ffa3c8
commit c6f442288e

View File

@ -1,12 +1,14 @@
// Copy and paste this code into OBS.Ninja's developer's console to generate new Translation files
function downloadTranslation(filename, trans = {}) { // downloads the current translation to a file
function downloadTranslation(filename, trans = {}) { // downloads the current translation to a file
const textDoc = JSON.stringify(trans, null, 2);
const hiddenElement = document.createElement('a');
hiddenElement.href = `data:text/html,${encodeURIComponent(textDoc)}`;
hiddenElement.href = `data:text/html,${
encodeURIComponent(textDoc)
}`;
hiddenElement.target = '_blank';
hiddenElement.download = `${filename}.json`;
hiddenElement.click();
@ -15,9 +17,11 @@ function downloadTranslation(filename, trans = {}) { // downloads the current t
}
function updateTranslation(filename) { // updates the website with a specific translation
function updateTranslation(filename) { // updates the website with a specific translation
const request = new XMLHttpRequest();
request.open('GET', `./translations/${filename}.json?${(Math.random() * 100).toString()}`, false); // `false` makes the request synchronous
request.open('GET', `./translations/${filename}.json?${
(Math.random() * 100).toString()
}`, false); // `false` makes the request synchronous
request.send(null);
if (request.status !== 200) {
@ -34,7 +38,7 @@ function updateTranslation(filename) { // updates the website with a specific t
const oldTransItems = data.innerHTML;
const allItems1 = document.querySelectorAll('[data-translate]');
allItems1.forEach(function (ele) {
allItems1.forEach((ele) => {
if (ele.dataset.translate in oldTransItems) {
ele.innerHTML = oldTransItems[ele.dataset.translate];
}
@ -42,7 +46,7 @@ function updateTranslation(filename) { // updates the website with a specific t
const oldTransTitles = data.titles;
const allTitles1 = document.querySelectorAll('[title]');
allTitles1.forEach(function (ele) {
allTitles1.forEach((ele) => {
const key = ele.title.replace(/[\W]+/g, "-").toLowerCase();
if (key in oldTransTitles) {
ele.title = oldTransTitles[key];
@ -51,7 +55,7 @@ function updateTranslation(filename) { // updates the website with a specific t
const oldTransPlaceholders = data.placeholders;
const allPlaceholders1 = document.querySelectorAll('[placeholder]');
allPlaceholders1.forEach(function (ele) {
allPlaceholders1.forEach((ele) => {
const key = ele.placeholder.replace(/[\W]+/g, "-").toLowerCase();
if (key in oldTransPlaceholders) {
ele.placeholder = oldTransPlaceholders[key];
@ -61,24 +65,38 @@ function updateTranslation(filename) { // updates the website with a specific t
return [true, data];
}
const updateList = ["cs", "de", "en", "es", "fr", "it", "ja", "nl", "pig", "pt", "ru", "tr", "blank"]; // list of languages to update. Update this if you add a new language.
const updateList = [
"cs",
"de",
"en",
"es",
"fr",
"it",
"ja",
"nl",
"pig",
"pt",
"ru",
"tr",
"blank"
]; // list of languages to update. Update this if you add a new language.
const allItems = document.querySelectorAll('[data-translate]');
const defaultTrans = {};
allItems.forEach(function (ele) {
allItems.forEach((ele) => {
const key = ele.dataset.translate.replace(/[\W]+/g, "-").toLowerCase();
defaultTrans[key] = ele.innerHTML;
});
const defaultTransTitles = {};
const allTitles = document.querySelectorAll('[title]');
allTitles.forEach(function (ele) {
allTitles.forEach((ele) => {
defaultTransTitles[ele.title] = ele.title;
});
const defaultTransPlaceholders = {};
const allPlaceholders = document.querySelectorAll('[placeholder]');
allPlaceholders.forEach(function (ele) {
allPlaceholders.forEach((ele) => {
defaultTransPlaceholders[ele.placeholder] = ele.placeholder;
});
@ -89,50 +107,50 @@ combinedTrans.placeholders = defaultTransPlaceholders;
var counter = 0;
for (const i in updateList) {
const lang = updateList[i];
setTimeout(function (ln) {
setTimeout((ln) => {
var suceess = updateTranslation(ln); // we don't need to worry about DATA.
if (suceess[0] == true) {
const newTrans = suceess[1].innerHTML;
const allItems = document.querySelectorAll('[data-translate]');
allItems.forEach(function (ele) {
allItems.forEach((ele) => {
const key = ele.dataset.translate;
newTrans[key] = ele.innerHTML;
});
const newTransTitles = suceess[1].titles;
const allTitles = document.querySelectorAll('[title]');
allTitles.forEach(function (ele) {
allTitles.forEach((ele) => {
const key = ele.title.replace(/[\W]+/g, "-").toLowerCase();
newTransTitles[key] = ele.title;
});
const newPlaceholders = suceess[1].placeholders;
const allPlaceholders = document.querySelectorAll('[placeholder]');
allPlaceholders.forEach(function (ele) {
allPlaceholders.forEach((ele) => {
const key = ele.placeholder.replace(/[\W]+/g, "-").toLowerCase();
newPlaceholders[key] = ele.placeholder;
});
////// DOWNLOAD UPDATED TRANSLATION
const outputTrans = {}
// //// DOWNLOAD UPDATED TRANSLATION
const outputTrans = {};
outputTrans.titles = newTransTitles;
outputTrans.innerHTML = newTrans;
outputTrans.placeholders = newPlaceholders;
downloadTranslation(ln, outputTrans);
}
////////// RESET THING BACK
allItems.forEach(function (ele) {
// //////// RESET THING BACK
allItems.forEach((ele) => {
if (ele.dataset.translate in defaultTrans) {
ele.innerHTML = defaultTrans[ele.dataset.translate];
}
});
allTitles.forEach(function (ele) {
allTitles.forEach((ele) => {
const key = ele.title.replace(/[\W]+/g, "-").toLowerCase();
if (key in defaultTransTitles) {
ele.title = defaultTransTitles[key];
}
});
allPlaceholders.forEach(function (ele) {
allPlaceholders.forEach((ele) => {
const key = ele.placeholder.replace(/[\W]+/g, "-").toLowerCase();
if (key in defaultTransPlaceholders) {
ele.placeholder = defaultTransPlaceholders[key];
@ -140,4 +158,4 @@ for (const i in updateList) {
});
}, counter, lang);
counter += 300;
}
}