Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 38 additions & 7 deletions src/friendly_errors/fes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
TL.addTranslations(['en', 'en-US', 'en-GB'], enTranslations);

const defaultLanguage = navigator.language;
const localTranslation = window.localStorage.getItem(defaultLanguage);
const localStorageAvailable = storageAvailable('localStorage');
const localTranslation = localStorageAvailable
? window.localStorage.getItem(`${defaultLanguage}-${VERSION}`)
: null;
let translationPromise = new Promise(async resolve => {
if (localTranslation) {
TL.addTranslations(defaultLanguage, JSON.parse(localTranslation));
Expand All @@ -19,7 +22,12 @@

const translation = await translationResponse.json();
TL.addTranslations(defaultLanguage, translation);
window.localStorage.setItem(defaultLanguage, JSON.stringify(translation));
if (localStorageAvailable) {
window.localStorage.setItem(
`${defaultLanguage}-${VERSION}`,
JSON.stringify(translation)
);
}
} catch {
try {
// CDN hosted file will be loaded if local is not present
Expand All @@ -32,10 +40,12 @@

const translation = await translationResponse.json();
TL.addTranslations(defaultLanguage, translation);
window.localStorage.setItem(
defaultLanguage,
JSON.stringify(translation)
);
if (localStorageAvailable) {
window.localStorage.setItem(
`${defaultLanguage}-${VERSION}`,
JSON.stringify(translation)
);
}
} catch {
// If CDN also cannot be reached, do nothing
}
Expand All @@ -59,7 +69,7 @@
const translation = TL.tl(
strings,
...values.map(value => {
if (value instanceof StyledMessage) {

Check warning on line 72 in src/friendly_errors/fes.js

View workflow job for this annotation

GitHub Actions / lint

eslint(no-use-before-define)

src/friendly_errors/fes.js:72:30: 'StyledMessage' was used before it was defined.
const ret = `%c${value.message.toString(FES.languageCode)}%c`;
styleStrings.push(value.styleString, '');
return ret;
Expand Down Expand Up @@ -142,7 +152,7 @@
TL.tl`seventh`,
TL.tl`eighth`,
TL.tl`ninth`,
TL.tl`tenth`,
TL.tl`tenth`
],
types: {
string: TL.tl`string`,
Expand Down Expand Up @@ -233,6 +243,27 @@
return msgWithReference;
}

// Feature detect localStorage API is available, from:
// https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API
function storageAvailable(type) {
let storage;
try {
storage = window[type];
const x = '__storage_test__';
storage.setItem(x, x);
storage.removeItem(x);
return true;
} catch (e) {
return (
e instanceof DOMException &&
e.name === 'QuotaExceededError' &&
// acknowledge QuotaExceededError only if there's something already stored
storage &&
storage.length !== 0
);
}
}

// Re-export TL
export { TL };

Expand Down
Loading