From d10ec54a5e151967a89c419fc481252c0acc2ee0 Mon Sep 17 00:00:00 2001 From: Jeremy Kauffman Date: Fri, 8 Nov 2019 15:51:42 -0500 Subject: [PATCH] this also doesnt work --- static/app-strings.json | 3 ++- ui/component/app/index.js | 5 ++++- ui/component/app/view.jsx | 11 +++++++++++ ui/constants/action_types.js | 3 +-- ui/redux/actions/settings.js | 7 +++++++ ui/redux/reducers/settings.js | 15 ++++++++++++++- ui/redux/selectors/settings.js | 4 ++-- 7 files changed, 41 insertions(+), 7 deletions(-) diff --git a/static/app-strings.json b/static/app-strings.json index 1651d7378..395079099 100644 --- a/static/app-strings.json +++ b/static/app-strings.json @@ -861,5 +861,6 @@ "You haven't downloaded anything from LBRY yet.": "You haven't downloaded anything from LBRY yet.", "Explore new content": "Explore new content", "Use this address to receive LBC.": "Use this address to receive LBC.", - "Embedded": "Embedded" + "Embedded": "Embedded", + "Failed to load %language% translations.": "Failed to load %language% translations." } diff --git a/ui/component/app/index.js b/ui/component/app/index.js index d29bc7b58..0ffc13033 100644 --- a/ui/component/app/index.js +++ b/ui/component/app/index.js @@ -3,8 +3,9 @@ import { hot } from 'react-hot-loader/root'; import { connect } from 'react-redux'; import { selectUser, doRewardList, doFetchAccessToken, selectGetSyncErrorMessage, selectUploadCount } from 'lbryinc'; import { doFetchTransactions, doFetchChannelListMine } from 'lbry-redux'; -import { makeSelectClientSetting, selectThemePath } from 'redux/selectors/settings'; +import { makeSelectClientSetting, selectLoadedLanguages, selectThemePath } from 'redux/selectors/settings'; import { selectIsUpgradeAvailable, selectAutoUpdateDownloaded } from 'redux/selectors/app'; +import { doSetLanguage } from 'redux/actions/settings'; import { doDownloadUpgradeRequested, doSignIn, doSyncWithPreferences } from 'redux/actions/app'; import App from './view'; @@ -12,6 +13,7 @@ const select = state => ({ user: selectUser(state), theme: selectThemePath(state), language: makeSelectClientSetting(SETTINGS.LANGUAGE)(state), + languages: selectLoadedLanguages(state), autoUpdateDownloaded: selectAutoUpdateDownloaded(state), isUpgradeAvailable: selectIsUpgradeAvailable(state), syncEnabled: makeSelectClientSetting(SETTINGS.ENABLE_SYNC)(state), @@ -24,6 +26,7 @@ const perform = dispatch => ({ fetchTransactions: (page, pageSize) => dispatch(doFetchTransactions(page, pageSize)), fetchAccessToken: () => dispatch(doFetchAccessToken()), fetchChannelListMine: () => dispatch(doFetchChannelListMine()), + setLanguage: language => dispatch(doSetLanguage(language)), signIn: () => dispatch(doSignIn()), requestDownloadUpgrade: () => dispatch(doDownloadUpgradeRequested()), checkSync: () => dispatch(doSyncWithPreferences()), diff --git a/ui/component/app/view.jsx b/ui/component/app/view.jsx index c2f6b9d52..f2fc70ff3 100644 --- a/ui/component/app/view.jsx +++ b/ui/component/app/view.jsx @@ -26,6 +26,7 @@ type Props = { alertError: (string | {}) => void, pageTitle: ?string, language: string, + languages: Array, theme: string, user: ?{ id: string, has_verified_email: boolean, is_reward_approved: boolean }, location: { pathname: string, hash: string }, @@ -38,6 +39,7 @@ type Props = { requestDownloadUpgrade: () => void, fetchChannelListMine: () => void, onSignedIn: () => void, + setLanguage: string => void, isUpgradeAvailable: boolean, autoUpdateDownloaded: boolean, checkSync: () => void, @@ -65,6 +67,9 @@ function App(props: Props) { uploadCount, history, syncError, + language, + languages, + setLanguage, } = props; const appRef = useRef(); @@ -111,6 +116,12 @@ function App(props: Props) { document.documentElement.setAttribute('data-mode', theme); }, [theme]); + useEffect(() => { + if (!languages.includes(language)) { + setLanguage(language); + } + }, [language, languages]); + useEffect(() => { if (previousUserId === undefined && userId) { analytics.setUser(userId); diff --git a/ui/constants/action_types.js b/ui/constants/action_types.js index c1e60b47c..fb7cfe522 100644 --- a/ui/constants/action_types.js +++ b/ui/constants/action_types.js @@ -159,8 +159,7 @@ export const CLAIM_REWARD_CLEAR_ERROR = 'CLAIM_REWARD_CLEAR_ERROR'; export const FETCH_REWARD_CONTENT_COMPLETED = 'FETCH_REWARD_CONTENT_COMPLETED'; // Language -export const DOWNLOAD_LANGUAGE_SUCCEEDED = 'DOWNLOAD_LANGUAGE_SUCCEEDED'; -export const DOWNLOAD_LANGUAGE_FAILED = 'DOWNLOAD_LANGUAGE_FAILED'; +export const DOWNLOAD_LANGUAGE_SUCCESS = 'DOWNLOAD_LANGUAGE_SUCCESS'; // Subscriptions export const CHANNEL_SUBSCRIBE = 'CHANNEL_SUBSCRIBE'; diff --git a/ui/redux/actions/settings.js b/ui/redux/actions/settings.js index ad43af788..ef713b818 100644 --- a/ui/redux/actions/settings.js +++ b/ui/redux/actions/settings.js @@ -1,5 +1,6 @@ import { Lbry, ACTIONS, doToast } from 'lbry-redux'; import * as SETTINGS from 'constants/settings'; +import * as LOCAL_ACTIONS from 'constants/action_types'; import analytics from 'analytics'; import SUPPORTED_LANGUAGES from 'constants/supported_languages'; @@ -91,6 +92,12 @@ export function doSetLanguage(language) { .then(r => r.json()) .then(j => { window.i18n_messages[language] = j; + dispatch({ + type: LOCAL_ACTIONS.DOWNLOAD_LANGUAGE_SUCCESS, + data: { + language, + }, + }); }) .then(() => { // set on localStorage so it can be read outside of redux diff --git a/ui/redux/reducers/settings.js b/ui/redux/reducers/settings.js index f4290aa07..737c5ff72 100644 --- a/ui/redux/reducers/settings.js +++ b/ui/redux/reducers/settings.js @@ -5,7 +5,7 @@ import moment from 'moment'; const reducers = {}; const defaultState = { isNight: false, - isFetchingLanguage: false, + loadedLanguages: Object.keys(window.i18n_messages) || [], daemonSettings: {}, clientSettings: { // UX @@ -70,6 +70,19 @@ reducers[ACTIONS.UPDATE_IS_NIGHT] = state => { }); }; +reducers[ACTIONS.DOWNLOAD_LANGUAGE_SUCCESS] = (state, action) => { + const { loadedLanguages } = state; + const { language } = action.data; + + if (language && !loadedLanguages.includes(language)) { + return Object.assign({}, state, { + loadedLanguages: [...loadedLanguages, language], + }); + } else { + return state; + } +}; + export default function reducer(state = defaultState, action) { const handler = reducers[action.type]; if (handler) return handler(state, action); diff --git a/ui/redux/selectors/settings.js b/ui/redux/selectors/settings.js index e7a0b5f71..eba15b952 100644 --- a/ui/redux/selectors/settings.js +++ b/ui/redux/selectors/settings.js @@ -13,9 +13,9 @@ export const selectClientSettings = createSelector( state => state.clientSettings || {} ); -export const selectIsFetchingLanguage = createSelector( +export const selectLoadedLanguages = createSelector( selectState, - state => state.isFetchingLanguage || {} + state => state.loadedLanguages || {} ); export const makeSelectClientSetting = setting =>