mirror of
https://github.com/the-djmaze/snappymail.git
synced 2024-11-10 09:02:45 +08:00
Cleanup language loader
This commit is contained in:
parent
b68d04aacd
commit
0528bcf911
1 changed files with 17 additions and 30 deletions
|
@ -218,36 +218,23 @@ export function getUploadErrorDescByCode(code) {
|
|||
* @param {string} language
|
||||
*/
|
||||
export function reload(admin, language) {
|
||||
const start = Date.now();
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
return fetch(langLink(language, admin), {cache: 'reload'})
|
||||
.then(response => {
|
||||
if (response.ok && response.headers.get('Content-Type').includes('application/javascript')) {
|
||||
return response.text();
|
||||
}
|
||||
reject(new Error('Invalid response'))
|
||||
}, error => {
|
||||
reject(new Error(error.message))
|
||||
})
|
||||
.then(data => {
|
||||
var script = doc.createElement('script');
|
||||
script.text = data;
|
||||
doc.head.appendChild(script).remove();
|
||||
setTimeout(
|
||||
() => {
|
||||
// reload the data
|
||||
if (window.rainloopI18N) {
|
||||
I18N_DATA = window.rainloopI18N || {};
|
||||
i18nToNodes(doc);
|
||||
dispatchEvent(new CustomEvent('reload-time'));
|
||||
trigger(!trigger());
|
||||
}
|
||||
window.rainloopI18N = null;
|
||||
resolve();
|
||||
},
|
||||
500 < Date.now() - start ? 1 : 500
|
||||
);
|
||||
});
|
||||
const script = doc.createElement('script');
|
||||
script.onload = () => {
|
||||
// reload the data
|
||||
if (window.rainloopI18N) {
|
||||
I18N_DATA = window.rainloopI18N || {};
|
||||
i18nToNodes(doc);
|
||||
dispatchEvent(new CustomEvent('reload-time'));
|
||||
trigger(!trigger());
|
||||
}
|
||||
window.rainloopI18N = null;
|
||||
script.remove();
|
||||
resolve();
|
||||
};
|
||||
script.onerror = () => reject(new Error('Language '+language+' failed'));
|
||||
script.src = langLink(language, admin);
|
||||
// script.async = true;
|
||||
doc.head.append(script);
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue