From db24f20289cbaeb2fe51eeb43a46689599483233 Mon Sep 17 00:00:00 2001 From: Felix Wiedenbach Date: Fri, 29 Jan 2021 22:25:49 +0100 Subject: [PATCH] cleaned up function and added test in case no file should be loaded --- js/module.js | 34 ++++++++++++++++------------------ tests/e2e/translations_spec.js | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/js/module.js b/js/module.js index 14458f8a..a6e574fc 100644 --- a/js/module.js +++ b/js/module.js @@ -311,29 +311,27 @@ var Module = Class.extend({ * * @param {Function} callback Function called when done. */ - loadTranslations: function (callback) { - var self = this; - var translations = this.getTranslations(); - var lang = config.language.toLowerCase(); + loadTranslations(callback) { + const translations = this.getTranslations() || {}; + const language = config.language.toLowerCase(); - // The variable `first` will contain the first - // defined translation after the following line. - for (var first in translations) { - break; - } + const languages = Object.keys(translations); + const fallbackLanguage = languages[0]; - if (translations) { - var translationFile = translations[lang] || undefined; - var translationsFallbackFile = translations[first]; + if (languages.length > 0) { + const translationFile = translations[language]; + const translationsFallbackFile = translations[fallbackLanguage]; - // If a translation file is set, load it and then also load the fallback translation file. - // Otherwise only load the fallback translation file. - if (translationFile !== undefined && translationFile !== translationsFallbackFile) { - Translator.load(self, translationFile, false, function () { - Translator.load(self, translationsFallbackFile, true, callback); + if (translationFile) { + Translator.load(this, translationFile, false, () => { + if (translationFile !== translationsFallbackFile) { + Translator.load(this, translationsFallbackFile, true, callback); + } else { + callback(); + } }); } else { - Translator.load(self, translationsFallbackFile, true, callback); + Translator.load(this, translationsFallbackFile, true, callback); } } else { callback(); diff --git a/tests/e2e/translations_spec.js b/tests/e2e/translations_spec.js index 639f8f12..5f0d6053 100644 --- a/tests/e2e/translations_spec.js +++ b/tests/e2e/translations_spec.js @@ -106,6 +106,24 @@ describe("Translations", function () { done(); }; }); + + it("should load no file", (done) => { + dom.window.onload = async function () { + const { Translator, Module } = dom.window; + Translator.load = sinon.stub(); + + Module.register("name", {}); + const MMM = Module.create("name"); + + const loaded = sinon.stub(); + MMM.loadTranslations(loaded); + + expect(loaded.callCount).to.equal(1); + expect(Translator.load.callCount).to.equal(0); + + done(); + }; + }); }); const mmm = {