in fusion-plugin-i18n/src/browser.js [72:123]
async load(translationKeys) {
const loadedKeys = Object.keys(this.translations);
const unloaded = translationKeys.filter(key => {
return loadedKeys.indexOf(key) < 0 && !this.requestedKeys.has(key);
});
if (unloaded.length > 0) {
// Don't try to load translations again if a request is already in
// flight. This means that we need to add unloaded chunks to
// loadedChunks optimistically and remove them if some error happens
unloaded.forEach(key => {
this.requestedKeys.add(key);
});
const fetchOpts = {
method: 'POST',
headers: {
Accept: '*/*',
'Content-Type': 'application/json',
...(this.locale ? {'X-Fusion-Locale-Code': this.locale} : {}),
},
body: JSON.stringify(unloaded),
};
// TODO(#3) don't append prefix if injected fetch also injects prefix
return fetch(
`/_translations${
this.locale ? `?localeCode=${this.locale}` : ''
}`,
fetchOpts
)
.then(r => {
try {
return r.json();
} catch (err) {
events && events.emit('i18n-load-error', {text: r.text()});
throw err;
}
})
.then((data: {[string]: string}) => {
for (const key in data) {
this.translations[key] = data[key];
this.requestedKeys.delete(key);
}
})
.catch((err: Error) => {
// An error occurred, so remove the chunks we were trying to load
// from loadedChunks. This allows us to try to load those chunk
// translations again
unloaded.forEach(key => {
this.requestedKeys.delete(key);
});
});
}
}