mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-28 04:02:12 +00:00
cleaned up function and added test in case no file should be loaded
This commit is contained in:
parent
94bb8e6c03
commit
db24f20289
36
js/module.js
36
js/module.js
@ -311,29 +311,27 @@ var Module = Class.extend({
|
|||||||
*
|
*
|
||||||
* @param {Function} callback Function called when done.
|
* @param {Function} callback Function called when done.
|
||||||
*/
|
*/
|
||||||
loadTranslations: function (callback) {
|
loadTranslations(callback) {
|
||||||
var self = this;
|
const translations = this.getTranslations() || {};
|
||||||
var translations = this.getTranslations();
|
const language = config.language.toLowerCase();
|
||||||
var lang = config.language.toLowerCase();
|
|
||||||
|
|
||||||
// The variable `first` will contain the first
|
const languages = Object.keys(translations);
|
||||||
// defined translation after the following line.
|
const fallbackLanguage = languages[0];
|
||||||
for (var first in translations) {
|
|
||||||
break;
|
if (languages.length > 0) {
|
||||||
|
const translationFile = translations[language];
|
||||||
|
const translationsFallbackFile = translations[fallbackLanguage];
|
||||||
|
|
||||||
|
if (translationFile) {
|
||||||
|
Translator.load(this, translationFile, false, () => {
|
||||||
|
if (translationFile !== translationsFallbackFile) {
|
||||||
|
Translator.load(this, translationsFallbackFile, true, callback);
|
||||||
|
} else {
|
||||||
|
callback();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (translations) {
|
|
||||||
var translationFile = translations[lang] || undefined;
|
|
||||||
var translationsFallbackFile = translations[first];
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
Translator.load(self, translationsFallbackFile, true, callback);
|
Translator.load(this, translationsFallbackFile, true, callback);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
callback();
|
callback();
|
||||||
|
@ -106,6 +106,24 @@ describe("Translations", function () {
|
|||||||
done();
|
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 = {
|
const mmm = {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user