one more optimization/fix

This commit is contained in:
Jeremy Kauffman 2019-11-12 17:25:44 -05:00 committed by Sean Yesmunt
parent 7e2fb22836
commit 5da78c9553

View file

@ -86,34 +86,37 @@ export function doSetDarkTime(value, options) {
} }
export function doSetLanguage(language) { export function doSetLanguage(language) {
return dispatch => { return (dispatch, getState) => {
// this should match the behavior/logic in index-web.html const { settings } = getState();
fetch('https://lbry.com/i18n/get/lbry-desktop/app-strings/' + language + '.json') if (settings.language !== language || !settings.loadedLanguages.include(language)) {
.then(r => r.json()) // this should match the behavior/logic in index-web.html
.then(j => { fetch('https://lbry.com/i18n/get/lbry-desktop/app-strings/' + language + '.json')
window.i18n_messages[language] = j; .then(r => r.json())
dispatch({ .then(j => {
type: LOCAL_ACTIONS.DOWNLOAD_LANGUAGE_SUCCESS, window.i18n_messages[language] = j;
data: { dispatch({
language, type: LOCAL_ACTIONS.DOWNLOAD_LANGUAGE_SUCCESS,
}, data: {
language,
},
});
})
.then(() => {
// set on localStorage so it can be read outside of redux
window.localStorage.setItem(SETTINGS.LANGUAGE, language);
dispatch(doSetClientSetting(SETTINGS.LANGUAGE, language));
})
.catch(e => {
window.localStorage.setItem(SETTINGS.LANGUAGE, 'en');
dispatch(doSetClientSetting(SETTINGS.LANGUAGE, 'en'));
const languageName = SUPPORTED_LANGUAGES[language] ? SUPPORTED_LANGUAGES[language] : language;
dispatch(
doToast({
message: __('Failed to load %language% translations.', { language: languageName }),
error: true,
})
);
}); });
}) }
.then(() => {
// set on localStorage so it can be read outside of redux
window.localStorage.setItem(SETTINGS.LANGUAGE, language);
dispatch(doSetClientSetting(SETTINGS.LANGUAGE, language));
})
.catch(e => {
window.localStorage.setItem(SETTINGS.LANGUAGE, 'en');
dispatch(doSetClientSetting(SETTINGS.LANGUAGE, 'en'));
const languageName = SUPPORTED_LANGUAGES[language] ? SUPPORTED_LANGUAGES[language] : language;
dispatch(
doToast({
message: __('Failed to load %language% translations.', { language: languageName }),
error: true,
})
);
});
}; };
} }