Skip to content
Snippets Groups Projects
Commit 68ee78a5 authored by Kocher, Manuel's avatar Kocher, Manuel
Browse files

add missing file error handling to translation-overrides

parent 8eb4a610
No related branches found
No related tags found
No related merge requests found
...@@ -9,8 +9,14 @@ async function fetchOverridesByLanguage(overrides, lng) { ...@@ -9,8 +9,14 @@ async function fetchOverridesByLanguage(overrides, lng) {
fetch(overrides + lng +'/translation.json', { fetch(overrides + lng +'/translation.json', {
headers: {'Content-Type': 'application/json'}, headers: {'Content-Type': 'application/json'},
}); });
let json = await result.json();
return json; // throw error if response is not json
try {
let json = await result.json();
return json;
} catch(e) {
throw new Error("Could not parse response as json: " + e);
}
} }
// handles translation cache promises // handles translation cache promises
...@@ -165,21 +171,26 @@ export async function setOverridesByGlobalCache(i18n, element) { ...@@ -165,21 +171,26 @@ export async function setOverridesByGlobalCache(i18n, element) {
// to the original one. This way we an change the overrides at runtime // to the original one. This way we an change the overrides at runtime
// and can even remove them. // and can even remove them.
// The scoped mixin saves the real tag name under data-tag-name // The scoped mixin saves the real tag name under data-tag-name
let tagName = ((element.dataset && element.dataset.tagName) || element.tagName).toLowerCase(); let tagName = ((element.dataset && element.dataset.tagName) || element.tagName).toLowerCase();
let namespace = i18n.options.fallbackNS; let namespace = i18n.options.fallbackNS;
let overrideNamespace = getOverrideNamespace(namespace); let overrideNamespace = getOverrideNamespace(namespace);
let hasOverrides = false; let hasOverrides = false;
for (let lng of i18n.languages) { for (let lng of i18n.languages) {
cacheOverrides(element.langDir, lng); // check if cacheOverrides throws error
translationCache[lng] = await translationCache[lng]; try {
i18n.removeResourceBundle(lng, overrideNamespace); cacheOverrides(element.langDir, lng);
if (translationCache[lng] === undefined || translationCache[lng][tagName] === undefined) continue; translationCache[lng] = await translationCache[lng];
let resources = translationCache[lng][tagName]; i18n.removeResourceBundle(lng, overrideNamespace);
hasOverrides = true; if (translationCache[lng] === undefined || translationCache[lng][tagName] === undefined) continue;
i18n.addResourceBundle(lng, overrideNamespace, resources); let resources = translationCache[lng][tagName];
hasOverrides = true;
i18n.addResourceBundle(lng, overrideNamespace, resources);
} catch(e) {
// leave loop and use default translations if error is thrown
hasOverrides = false;
break;
}
} }
i18n.setDefaultNamespace(hasOverrides ? overrideNamespace : namespace); i18n.setDefaultNamespace(hasOverrides ? overrideNamespace : namespace);
element.requestUpdate(); element.requestUpdate();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment