Skip to content

Commit

Permalink
Revert "Convert translator callbacks to async/await (MagicMirrorOrg#3048
Browse files Browse the repository at this point in the history
)"

This reverts commit 2b792cd.
  • Loading branch information
veeck committed Mar 7, 2023
1 parent dd3af83 commit 361528b
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 92 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ _This release is scheduled to be released on 2023-04-01._
- Update dates in Calendar widgets every minute
- Cleanup jest coverage for patches
- Update `stylelint` dependencies, switch to `stylelint-config-standard` and handle `stylelint` issues
- Convert lots of callbacks to async/await
- Fixed Open-Meteo wind speed units

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion js/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ const Loader = (function () {
Log.log("Scripts loaded for: " + module.name);
mObj.loadStyles(function () {
Log.log("Styles loaded for: " + module.name);
mObj.loadTranslations().then(() => {
mObj.loadTranslations(function () {
Log.log("Translations loaded for: " + module.name);
moduleObjects.push(mObj);
callback();
Expand Down
3 changes: 2 additions & 1 deletion js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,8 @@ const MM = (function () {

Log.setLogLevel(config.logLevel);

Translator.loadCoreTranslations(config.language).then(() => Loader.loadModules());
Translator.loadCoreTranslations(config.language);
Loader.loadModules();
},

/**
Expand Down
20 changes: 13 additions & 7 deletions js/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,30 +302,36 @@ const Module = Class.extend({

/**
* Load all translations.
*
* @param {Function} callback Function called when done.
*/
async loadTranslations() {
loadTranslations(callback) {
const translations = this.getTranslations() || {};
const language = config.language.toLowerCase();

const languages = Object.keys(translations);
const fallbackLanguage = languages[0];

if (languages.length === 0) {
callback();
return;
}

const translationFile = translations[language];
const translationsFallbackFile = translations[fallbackLanguage];

if (!translationFile) {
return Translator.load(this, translationsFallbackFile, true);
Translator.load(this, translationsFallbackFile, true, callback);
return;
}

await Translator.load(this, translationFile, false);

if (translationFile !== translationsFallbackFile) {
return Translator.load(this, translationsFallbackFile, true);
}
Translator.load(this, translationFile, false, () => {
if (translationFile !== translationsFallbackFile) {
Translator.load(this, translationsFallbackFile, true, callback);
} else {
callback();
}
});
},

/**
Expand Down
66 changes: 36 additions & 30 deletions js/translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,26 @@ const Translator = (function () {
* Load a JSON file via XHR.
*
* @param {string} file Path of the file we want to load.
* @returns {Promise<object>} the translations in the specified file
* @param {Function} callback Function called when done.
*/
async function loadJSON(file) {
function loadJSON(file, callback) {
const xhr = new XMLHttpRequest();
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);
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");
}
};
xhr.send(null);
});
callback(fileinfo);
}
};
xhr.send(null);
}

return {
Expand All @@ -50,7 +48,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:
Expand Down Expand Up @@ -103,44 +101,52 @@ 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.
*/
async load(module, file, isFallback) {
load(module, file, isFallback, callback) {
Log.log(`${module.name} - Load translation${isFallback ? " fallback" : ""}: ${file}`);

if (this.translationsFallback[module.name]) {
callback();
return;
}

const json = await loadJSON(module.file(file));
const property = isFallback ? "translationsFallback" : "translations";
this[property][module.name] = json;
loadJSON(module.file(file), (json) => {
const property = isFallback ? "translationsFallback" : "translations";
this[property][module.name] = json;
callback();
});
},

/**
* Load the core translations.
*
* @param {string} lang The language identifier of the core language.
*/
loadCoreTranslations: async function (lang) {
loadCoreTranslations: function (lang) {
if (lang in translations) {
Log.log("Loading core translation file: " + translations[lang]);
this.coreTranslations = await loadJSON(translations[lang]);
loadJSON(translations[lang], (translations) => {
this.coreTranslations = translations;
});
} else {
Log.log("Configured language not found in core translations.");
}

await this.loadCoreTranslationsFallback();
this.loadCoreTranslationsFallback();
},

/**
* Load the core translations' fallback.
* Load the core translations fallback.
* The first language defined in translations.js will be used.
*/
loadCoreTranslationsFallback: async function () {
loadCoreTranslationsFallback: function () {
let first = Object.keys(translations)[0];
if (first) {
Log.log("Loading core translation fallback file: " + translations[first]);
this.coreTranslationsFallback = await loadJSON(translations[first]);
loadJSON(translations[first], (translations) => {
this.coreTranslationsFallback = translations;
});
}
}
};
Expand Down
63 changes: 37 additions & 26 deletions tests/e2e/translations_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ describe("Translations", () => {
server = app.listen(3000);
});

afterAll(async () => {
await server.close();
afterAll(() => {
server.close();
});

it("should have a translation file in the specified path", () => {
Expand All @@ -48,15 +48,17 @@ describe("Translations", () => {
dom.window.onload = async () => {
const { Translator, Module, config } = dom.window;
config.language = "en";
Translator.load = sinon.stub().callsFake((_m, _f, _fb) => null);
Translator.load = sinon.stub().callsFake((_m, _f, _fb, callback) => callback());

Module.register("name", { getTranslations: () => translations });
const MMM = Module.create("name");

await MMM.loadTranslations();
const loaded = sinon.stub();
MMM.loadTranslations(loaded);

expect(loaded.callCount).toBe(1);
expect(Translator.load.args.length).toBe(1);
expect(Translator.load.calledWith(MMM, "translations/en.json", false)).toBe(true);
expect(Translator.load.calledWith(MMM, "translations/en.json", false, sinon.match.func)).toBe(true);

done();
};
Expand All @@ -65,16 +67,18 @@ describe("Translations", () => {
it("should load translation + fallback file", (done) => {
dom.window.onload = async () => {
const { Translator, Module } = dom.window;
Translator.load = sinon.stub().callsFake((_m, _f, _fb) => null);
Translator.load = sinon.stub().callsFake((_m, _f, _fb, callback) => callback());

Module.register("name", { getTranslations: () => translations });
const MMM = Module.create("name");

await MMM.loadTranslations();
const loaded = sinon.stub();
MMM.loadTranslations(loaded);

expect(loaded.callCount).toBe(1);
expect(Translator.load.args.length).toBe(2);
expect(Translator.load.calledWith(MMM, "translations/de.json", false)).toBe(true);
expect(Translator.load.calledWith(MMM, "translations/en.json", true)).toBe(true);
expect(Translator.load.calledWith(MMM, "translations/de.json", false, sinon.match.func)).toBe(true);
expect(Translator.load.calledWith(MMM, "translations/en.json", true, sinon.match.func)).toBe(true);

done();
};
Expand All @@ -84,15 +88,17 @@ describe("Translations", () => {
dom.window.onload = async () => {
const { Translator, Module, config } = dom.window;
config.language = "--";
Translator.load = sinon.stub().callsFake((_m, _f, _fb) => null);
Translator.load = sinon.stub().callsFake((_m, _f, _fb, callback) => callback());

Module.register("name", { getTranslations: () => translations });
const MMM = Module.create("name");

await MMM.loadTranslations();
const loaded = sinon.stub();
MMM.loadTranslations(loaded);

expect(loaded.callCount).toBe(1);
expect(Translator.load.args.length).toBe(1);
expect(Translator.load.calledWith(MMM, "translations/en.json", true)).toBe(true);
expect(Translator.load.calledWith(MMM, "translations/en.json", true, sinon.match.func)).toBe(true);

done();
};
Expand All @@ -106,8 +112,10 @@ describe("Translations", () => {
Module.register("name", {});
const MMM = Module.create("name");

await MMM.loadTranslations();
const loaded = sinon.stub();
MMM.loadTranslations(loaded);

expect(loaded.callCount).toBe(1);
expect(Translator.load.callCount).toBe(0);

done();
Expand All @@ -130,13 +138,14 @@ describe("Translations", () => {
<script src="file://${path.join(__dirname, "..", "..", "js", "translator.js")}">`,
{ runScripts: "dangerously", resources: "usable" }
);
dom.window.onload = async () => {
dom.window.onload = () => {
const { Translator } = dom.window;

await Translator.load(mmm, translations[language], false);
expect(typeof Translator.translations[mmm.name]).toBe("object");
expect(Object.keys(Translator.translations[mmm.name]).length).toBeGreaterThanOrEqual(1);
done();
Translator.load(mmm, translations[language], false, () => {
expect(typeof Translator.translations[mmm.name]).toBe("object");
expect(Object.keys(Translator.translations[mmm.name]).length).toBeGreaterThanOrEqual(1);
done();
});
};
});
}
Expand All @@ -152,12 +161,13 @@ describe("Translations", () => {
<script src="file://${path.join(__dirname, "..", "..", "js", "translator.js")}">`,
{ runScripts: "dangerously", resources: "usable" }
);
dom.window.onload = async () => {
dom.window.onload = () => {
const { Translator } = dom.window;

await Translator.load(mmm, translations.de, false);
base = Object.keys(Translator.translations[mmm.name]).sort();
done();
Translator.load(mmm, translations.de, false, () => {
base = Object.keys(Translator.translations[mmm.name]).sort();
done();
});
};
});

Expand All @@ -181,12 +191,13 @@ describe("Translations", () => {
<script src="file://${path.join(__dirname, "..", "..", "js", "translator.js")}">`,
{ runScripts: "dangerously", resources: "usable" }
);
dom.window.onload = async () => {
dom.window.onload = () => {
const { Translator } = dom.window;

await Translator.load(mmm, translations[language], false);
keys = Object.keys(Translator.translations[mmm.name]).sort();
done();
Translator.load(mmm, translations[language], false, () => {
keys = Object.keys(Translator.translations[mmm.name]).sort();
done();
});
};
});

Expand Down
Loading

0 comments on commit 361528b

Please sign in to comment.