diff --git a/src/containers/blocks.jsx b/src/containers/blocks.jsx index fa51f47733859039615b280c0dddb21b3b03bd47..1b411d6336e9291efabdbca2959ef0f85a7f1d96 100644 --- a/src/containers/blocks.jsx +++ b/src/containers/blocks.jsx @@ -472,8 +472,8 @@ const mapStateToProps = state => ({ state.scratchGui.mode.isFullScreen ), extensionLibraryVisible: state.scratchGui.modals.extensionLibrary, - locale: state.scratchGui.locales.locale, - messages: state.scratchGui.locales.messages[state.scratchGui.locales.locale], + locale: state.locales.locale, + messages: state.locales.messages, toolboxXML: state.scratchGui.toolbox.toolboxXML, customProceduresVisible: state.scratchGui.customProcedures.active }); diff --git a/src/containers/language-selector.jsx b/src/containers/language-selector.jsx index 0b61984becbf6a0751c7bd93c6128091ebfbf195..9439d5ab91575ba5cada6667e7fee675a41083b0 100644 --- a/src/containers/language-selector.jsx +++ b/src/containers/language-selector.jsx @@ -16,7 +16,7 @@ class LanguageSelector extends React.Component { } handleChange (e) { const newLocale = e.target.value; - if (this.props.locales.hasOwnProperty(newLocale)) { + if (this.props.supportedLocales.includes(newLocale)) { this.props.onChangeLanguage(newLocale); } } @@ -40,13 +40,13 @@ class LanguageSelector extends React.Component { LanguageSelector.propTypes = { children: PropTypes.node, currentLocale: PropTypes.string.isRequired, - locales: PropTypes.objectOf(PropTypes.objectOf(PropTypes.string)), - onChangeLanguage: PropTypes.func.isRequired + onChangeLanguage: PropTypes.func.isRequired, + supportedLocales: PropTypes.objectOf(PropTypes.objectOf(PropTypes.string)) }; const mapStateToProps = state => ({ - currentLocale: state.scratchGui.locales.locale, - locales: state.scratchGui.locales.messages + currentLocale: state.locales.locale, + supportedLocales: Object.keys(state.locales.messagesByLocale) }); const mapDispatchToProps = dispatch => ({ diff --git a/src/index.js b/src/index.js index f47b279bce033d037381b77641801a60d567e269..dfd32a399de9c132135e2cb0f0596bd8eecc74a2 100644 --- a/src/index.js +++ b/src/index.js @@ -1,10 +1,12 @@ import GUI from './containers/gui.jsx'; -import GuiReducer, {guiInitialState, guiMiddleware, initFullScreen, initPlayer, initLocale} from './reducers/gui'; +import GuiReducer, {guiInitialState, guiMiddleware, initFullScreen, initPlayer} from './reducers/gui'; +import LocalesReducer, {localesInitialState, initLocale} from './reducers/locales'; import {ScratchPaintReducer} from 'scratch-paint'; import {setFullScreen, setPlayer} from './reducers/mode'; import {setAppElement} from 'react-modal'; const guiReducers = { + locales: LocalesReducer, scratchGui: GuiReducer, scratchPaint: ScratchPaintReducer }; @@ -18,6 +20,7 @@ export { initPlayer, initFullScreen, initLocale, + localesInitialState, setFullScreen, setPlayer }; diff --git a/src/lib/app-state-hoc.jsx b/src/lib/app-state-hoc.jsx index 7c23c48393394aa6e08dadcb07e707a4199b52ea..20e2852a131c5ad70a878d4d0d4b3038df6c8548 100644 --- a/src/lib/app-state-hoc.jsx +++ b/src/lib/app-state-hoc.jsx @@ -4,7 +4,8 @@ import {Provider} from 'react-redux'; import {createStore, combineReducers, compose} from 'redux'; import ConnectedIntlProvider from './connected-intl-provider.jsx'; -import guiReducer, {guiInitialState, guiMiddleware, initFullScreen, initLocale, initPlayer} from '../reducers/gui'; +import guiReducer, {guiInitialState, guiMiddleware, initFullScreen, initPlayer} from '../reducers/gui'; +import localesReducer, {initLocale, localesInitialState} from '../reducers/locales'; import {setPlayer, setFullScreen} from '../reducers/mode.js'; @@ -30,20 +31,25 @@ const AppStateHOC = function (WrappedComponent) { if (props.isPlayerOnly) { initializedGui = initPlayer(initializedGui); } - const reducer = combineReducers({ - scratchGui: guiReducer, - scratchPaint: ScratchPaintReducer - }); + let initializedLocales = localesInitialState; if (window.location.search.indexOf('locale=') !== -1 || window.location.search.indexOf('lang=') !== -1) { const locale = window.location.search.match(/(?:locale|lang)=([\w]+)/)[1]; - initializedGui = initLocale(initializedGui, locale); + initializedLocales = initLocale(initializedLocales, locale); } + const reducer = combineReducers({ + locales: localesReducer, + scratchGui: guiReducer, + scratchPaint: ScratchPaintReducer + }); this.store = createStore( reducer, - {scratchGui: initializedGui}, + { + locales: initializedLocales, + scratchGui: initializedGui + }, enhancer); } componentDidUpdate (prevProps) { diff --git a/src/lib/connected-intl-provider.jsx b/src/lib/connected-intl-provider.jsx index e1eacb3adc077a83ca2bb52b631d6453e1c316d0..e4150ef1b39fe73dd1e5db92c1ab427cb16298d5 100644 --- a/src/lib/connected-intl-provider.jsx +++ b/src/lib/connected-intl-provider.jsx @@ -2,9 +2,9 @@ import {IntlProvider as ReactIntlProvider} from 'react-intl'; import {connect} from 'react-redux'; const mapStateToProps = state => ({ - key: state.scratchGui.locales.locale, - locale: state.scratchGui.locales.locale, - messages: state.scratchGui.locales.messages[state.scratchGui.locales.locale] + key: state.locales.locale, + locale: state.locales.locale, + messages: state.locales.messages }); export default connect(mapStateToProps)(ReactIntlProvider); diff --git a/src/reducers/gui.js b/src/reducers/gui.js index 810fce7c213ac30aa8ce2fae0ce957b182271631..7d98ae2f3f74a757bc101910bbabe6fd491fc192 100644 --- a/src/reducers/gui.js +++ b/src/reducers/gui.js @@ -7,7 +7,6 @@ import blockDragReducer, {blockDragInitialState} from './block-drag'; import editorTabReducer, {editorTabInitialState} from './editor-tab'; import hoveredTargetReducer, {hoveredTargetInitialState} from './hovered-target'; import menuReducer, {menuInitialState} from './menus'; -import localesReducer, {localesInitialState} from './locales'; import modalReducer, {modalsInitialState} from './modals'; import modeReducer, {modeInitialState} from './mode'; import monitorReducer, {monitorsInitialState} from './monitors'; @@ -27,7 +26,6 @@ const guiInitialState = { colorPicker: colorPickerInitialState, customProcedures: customProceduresInitialState, editorTab: editorTabInitialState, - locales: localesInitialState, mode: modeInitialState, hoveredTarget: hoveredTargetInitialState, stageSize: stageSizeInitialState, @@ -60,20 +58,7 @@ const initFullScreen = function (currentState) { }} ); }; -const initLocale = function (currentState, locale) { - if (currentState.locales.messages.hasOwnProperty(locale)) { - return Object.assign( - {}, - currentState, - {locales: { - locale: locale, - messages: currentState.locales.messages - }} - ); - } - // don't change locale if it's not in the current messages - return currentState; -}; + const guiReducer = combineReducers({ assetDrag: assetDragReducer, blockDrag: blockDragReducer, @@ -81,7 +66,6 @@ const guiReducer = combineReducers({ colorPicker: colorPickerReducer, customProcedures: customProceduresReducer, editorTab: editorTabReducer, - locales: localesReducer, mode: modeReducer, hoveredTarget: hoveredTargetReducer, stageSize: stageSizeReducer, @@ -99,6 +83,5 @@ export { guiInitialState, guiMiddleware, initFullScreen, - initLocale, initPlayer }; diff --git a/src/reducers/locales.js b/src/reducers/locales.js index d1336a91e3e76beb755ac9e094e22d4a173707a7..fda8f9dc36d14dac09f72fad62f704617b87e0cf 100644 --- a/src/reducers/locales.js +++ b/src/reducers/locales.js @@ -10,7 +10,8 @@ const SELECT_LOCALE = 'scratch-gui/locales/SELECT_LOCALE'; const initialState = { locale: 'en', - messages: editorMessages + messagesByLocale: editorMessages, + messages: editorMessages.en }; const reducer = function (state, action) { @@ -19,12 +20,14 @@ const reducer = function (state, action) { case SELECT_LOCALE: return Object.assign({}, state, { locale: action.locale, - messages: state.messages + messagesByLocale: state.messagesByLocale, + messages: state.messagesByLocale[action.locale] }); case UPDATE_LOCALES: return Object.assign({}, state, { locale: state.locale, - messages: action.messages + messagesByLocale: action.messagesByLocale, + messages: action.messagesByLocale[state.locale] }); default: return state; @@ -41,13 +44,28 @@ const selectLocale = function (locale) { const setLocales = function (localesMessages) { return { type: UPDATE_LOCALES, - messages: localesMessages + messagesByLocale: localesMessages }; }; - +const initLocale = function (currentState, locale) { + if (currentState.messagesByLocale.hasOwnProperty(locale)) { + return Object.assign( + {}, + currentState, + { + locale: locale, + messagesByLocale: currentState.messagesByLocale, + messages: currentState.messagesByLocale[locale] + } + ); + } + // don't change locale if it's not in the current messages + return currentState; +}; export { reducer as default, initialState as localesInitialState, + initLocale, selectLocale, setLocales };