@ovh-ux/ng-translate-async-loader
Version:
Angular translate asynchronous loader
62 lines (52 loc) • 1.79 kB
JavaScript
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;
}
import angular from 'angular';
// 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);
export { moduleName as default };