Cleanup language loader

This commit is contained in:
djmaze 2020-10-15 12:59:56 +02:00
parent b68d04aacd
commit 0528bcf911

View file

@ -218,36 +218,23 @@ export function getUploadErrorDescByCode(code) {
* @param {string} language * @param {string} language
*/ */
export function reload(admin, language) { export function reload(admin, language) {
const start = Date.now();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
return fetch(langLink(language, admin), {cache: 'reload'}) const script = doc.createElement('script');
.then(response => { script.onload = () => {
if (response.ok && response.headers.get('Content-Type').includes('application/javascript')) { // reload the data
return response.text(); if (window.rainloopI18N) {
} I18N_DATA = window.rainloopI18N || {};
reject(new Error('Invalid response')) i18nToNodes(doc);
}, error => { dispatchEvent(new CustomEvent('reload-time'));
reject(new Error(error.message)) trigger(!trigger());
}) }
.then(data => { window.rainloopI18N = null;
var script = doc.createElement('script'); script.remove();
script.text = data; resolve();
doc.head.appendChild(script).remove(); };
setTimeout( script.onerror = () => reject(new Error('Language '+language+' failed'));
() => { script.src = langLink(language, admin);
// reload the data // script.async = true;
if (window.rainloopI18N) { doc.head.append(script);
I18N_DATA = window.rainloopI18N || {};
i18nToNodes(doc);
dispatchEvent(new CustomEvent('reload-time'));
trigger(!trigger());
}
window.rainloopI18N = null;
resolve();
},
500 < Date.now() - start ? 1 : 500
);
});
}); });
} }