@ovh-ux/ng-translate-async-loader
Version:
Angular translate asynchronous loader
69 lines (58 loc) • 2.31 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('angular')) :
typeof define === 'function' && define.amd ? define(['angular'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global["ng-translate-async-loader"] = factory(global.angular));
})(this, (function (angular) { 'use strict';
function ___$insertStyle(css) {
if (!css || typeof window === 'undefined') {
return;
}
const style = document.createElement('style');
style.setAttribute('type', 'text/css');
style.innerHTML = css;
document.head.appendChild(style);
return css;
}
// Inspired by angular-translate-partial-loader (see sources)
var factory = ["$q", function ($q) {
/**
* The tricky part of this loader is that we need to check
* if an import (addTranslations) has been called during all
* the pending imports. If it's the case we cannot return
* translations yet and we need to loop again in order to
* wait for all the imports to be finised.
*/
// loaded translations
var translations = {};
// list of pending translation import
var pending = [];
// true when a new translation import occurs
var changed = false;
var loader = function loader() {
// when the function is first called, we have no changes
changed = false;
// when all pending import are resolved
return $q.all(pending).then(function () {
// if an import has been added during pending changes we need to loop
if (changed) {
return loader();
// otherwise it's fine we just return translations
}
return translations;
});
};
loader.addTranslations = function addTranslations(translationPromise) {
// a new import occurs, we keep trace of the change
changed = true;
// push the pending import
pending.push(translationPromise.then(function (tr) {
Object.assign(translations, tr);
}));
};
return loader;
}];
var moduleName = 'ngTranslateAsyncLoader';
angular.module(moduleName, []).factory('asyncLoader', factory);
return moduleName;
}));
//# sourceMappingURL=ng-translate-async-loader.js.map