Convert translator callbacks to async/await (#3048)

Co-authored-by: veeck <michael@veeck.de>
This commit is contained in:
Veeck
2023-02-21 22:58:18 +01:00
committed by GitHub
parent a23769156e
commit 2b792cdbb8
7 changed files with 92 additions and 119 deletions

View File

@@ -11,26 +11,28 @@ const Translator = (function () {
* Load a JSON file via XHR.
*
* @param {string} file Path of the file we want to load.
* @param {Function} callback Function called when done.
* @returns {Promise<object>} the translations in the specified file
*/
function loadJSON(file, callback) {
async function loadJSON(file) {
const xhr = new XMLHttpRequest();
xhr.overrideMimeType("application/json");
xhr.open("GET", file, true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
// needs error handler try/catch at least
let fileinfo = null;
try {
fileinfo = JSON.parse(xhr.responseText);
} catch (exception) {
// nothing here, but don't die
Log.error(" loading json file =" + file + " failed");
return new Promise(function (resolve, reject) {
xhr.overrideMimeType("application/json");
xhr.open("GET", file, true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
// needs error handler try/catch at least
let fileinfo = null;
try {
fileinfo = JSON.parse(xhr.responseText);
} catch (exception) {
// nothing here, but don't die
Log.error(" loading json file =" + file + " failed");
}
resolve(fileinfo);
}
callback(fileinfo);
}
};
xhr.send(null);
};
xhr.send(null);
});
}
return {
@@ -48,7 +50,7 @@ const Translator = (function () {
* @returns {string} the translated key
*/
translate: function (module, key, variables) {
variables = variables || {}; //Empty object by default
variables = variables || {}; // Empty object by default
/**
* Combines template and variables like:
@@ -101,21 +103,17 @@ const Translator = (function () {
* @param {Module} module The module to load the translation file for.
* @param {string} file Path of the file we want to load.
* @param {boolean} isFallback Flag to indicate fallback translations.
* @param {Function} callback Function called when done.
*/
load(module, file, isFallback, callback) {
async load(module, file, isFallback) {
Log.log(`${module.name} - Load translation${isFallback ? " fallback" : ""}: ${file}`);
if (this.translationsFallback[module.name]) {
callback();
return;
}
loadJSON(module.file(file), (json) => {
const property = isFallback ? "translationsFallback" : "translations";
this[property][module.name] = json;
callback();
});
const json = await loadJSON(module.file(file));
const property = isFallback ? "translationsFallback" : "translations";
this[property][module.name] = json;
},
/**
@@ -123,30 +121,26 @@ const Translator = (function () {
*
* @param {string} lang The language identifier of the core language.
*/
loadCoreTranslations: function (lang) {
loadCoreTranslations: async function (lang) {
if (lang in translations) {
Log.log("Loading core translation file: " + translations[lang]);
loadJSON(translations[lang], (translations) => {
this.coreTranslations = translations;
});
this.coreTranslations = await loadJSON(translations[lang]);
} else {
Log.log("Configured language not found in core translations.");
}
this.loadCoreTranslationsFallback();
await this.loadCoreTranslationsFallback();
},
/**
* Load the core translations fallback.
* Load the core translations' fallback.
* The first language defined in translations.js will be used.
*/
loadCoreTranslationsFallback: function () {
loadCoreTranslationsFallback: async function () {
let first = Object.keys(translations)[0];
if (first) {
Log.log("Loading core translation fallback file: " + translations[first]);
loadJSON(translations[first], (translations) => {
this.coreTranslationsFallback = translations;
});
this.coreTranslationsFallback = await loadJSON(translations[first]);
}
}
};