Skip to content
Snippets Groups Projects
Commit 60a3dc3a authored by chrisgarrity's avatar chrisgarrity
Browse files

Locales cleanup

* updated locales reducer to include current messages and messagesByLocale (the previous all messages)
* moved locales out of gui
* include locales in the things exported by gui (index.js)
parent a0455422
No related branches found
No related tags found
No related merge requests found
...@@ -472,8 +472,8 @@ const mapStateToProps = state => ({ ...@@ -472,8 +472,8 @@ const mapStateToProps = state => ({
state.scratchGui.mode.isFullScreen state.scratchGui.mode.isFullScreen
), ),
extensionLibraryVisible: state.scratchGui.modals.extensionLibrary, extensionLibraryVisible: state.scratchGui.modals.extensionLibrary,
locale: state.scratchGui.locales.locale, locale: state.locales.locale,
messages: state.scratchGui.locales.messages[state.scratchGui.locales.locale], messages: state.locales.messages,
toolboxXML: state.scratchGui.toolbox.toolboxXML, toolboxXML: state.scratchGui.toolbox.toolboxXML,
customProceduresVisible: state.scratchGui.customProcedures.active customProceduresVisible: state.scratchGui.customProcedures.active
}); });
......
...@@ -16,7 +16,7 @@ class LanguageSelector extends React.Component { ...@@ -16,7 +16,7 @@ class LanguageSelector extends React.Component {
} }
handleChange (e) { handleChange (e) {
const newLocale = e.target.value; const newLocale = e.target.value;
if (this.props.locales.hasOwnProperty(newLocale)) { if (this.props.supportedLocales.includes(newLocale)) {
this.props.onChangeLanguage(newLocale); this.props.onChangeLanguage(newLocale);
} }
} }
...@@ -40,13 +40,13 @@ class LanguageSelector extends React.Component { ...@@ -40,13 +40,13 @@ class LanguageSelector extends React.Component {
LanguageSelector.propTypes = { LanguageSelector.propTypes = {
children: PropTypes.node, children: PropTypes.node,
currentLocale: PropTypes.string.isRequired, 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 => ({ const mapStateToProps = state => ({
currentLocale: state.scratchGui.locales.locale, currentLocale: state.locales.locale,
locales: state.scratchGui.locales.messages supportedLocales: Object.keys(state.locales.messagesByLocale)
}); });
const mapDispatchToProps = dispatch => ({ const mapDispatchToProps = dispatch => ({
......
import GUI from './containers/gui.jsx'; 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 {ScratchPaintReducer} from 'scratch-paint';
import {setFullScreen, setPlayer} from './reducers/mode'; import {setFullScreen, setPlayer} from './reducers/mode';
import {setAppElement} from 'react-modal'; import {setAppElement} from 'react-modal';
const guiReducers = { const guiReducers = {
locales: LocalesReducer,
scratchGui: GuiReducer, scratchGui: GuiReducer,
scratchPaint: ScratchPaintReducer scratchPaint: ScratchPaintReducer
}; };
...@@ -18,6 +20,7 @@ export { ...@@ -18,6 +20,7 @@ export {
initPlayer, initPlayer,
initFullScreen, initFullScreen,
initLocale, initLocale,
localesInitialState,
setFullScreen, setFullScreen,
setPlayer setPlayer
}; };
...@@ -4,7 +4,8 @@ import {Provider} from 'react-redux'; ...@@ -4,7 +4,8 @@ import {Provider} from 'react-redux';
import {createStore, combineReducers, compose} from 'redux'; import {createStore, combineReducers, compose} from 'redux';
import ConnectedIntlProvider from './connected-intl-provider.jsx'; 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'; import {setPlayer, setFullScreen} from '../reducers/mode.js';
...@@ -30,20 +31,25 @@ const AppStateHOC = function (WrappedComponent) { ...@@ -30,20 +31,25 @@ const AppStateHOC = function (WrappedComponent) {
if (props.isPlayerOnly) { if (props.isPlayerOnly) {
initializedGui = initPlayer(initializedGui); initializedGui = initPlayer(initializedGui);
} }
const reducer = combineReducers({
scratchGui: guiReducer,
scratchPaint: ScratchPaintReducer
});
let initializedLocales = localesInitialState;
if (window.location.search.indexOf('locale=') !== -1 || if (window.location.search.indexOf('locale=') !== -1 ||
window.location.search.indexOf('lang=') !== -1) { window.location.search.indexOf('lang=') !== -1) {
const locale = window.location.search.match(/(?:locale|lang)=([\w]+)/)[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( this.store = createStore(
reducer, reducer,
{scratchGui: initializedGui}, {
locales: initializedLocales,
scratchGui: initializedGui
},
enhancer); enhancer);
} }
componentDidUpdate (prevProps) { componentDidUpdate (prevProps) {
......
...@@ -2,9 +2,9 @@ import {IntlProvider as ReactIntlProvider} from 'react-intl'; ...@@ -2,9 +2,9 @@ import {IntlProvider as ReactIntlProvider} from 'react-intl';
import {connect} from 'react-redux'; import {connect} from 'react-redux';
const mapStateToProps = state => ({ const mapStateToProps = state => ({
key: state.scratchGui.locales.locale, key: state.locales.locale,
locale: state.scratchGui.locales.locale, locale: state.locales.locale,
messages: state.scratchGui.locales.messages[state.scratchGui.locales.locale] messages: state.locales.messages
}); });
export default connect(mapStateToProps)(ReactIntlProvider); export default connect(mapStateToProps)(ReactIntlProvider);
...@@ -7,7 +7,6 @@ import blockDragReducer, {blockDragInitialState} from './block-drag'; ...@@ -7,7 +7,6 @@ import blockDragReducer, {blockDragInitialState} from './block-drag';
import editorTabReducer, {editorTabInitialState} from './editor-tab'; import editorTabReducer, {editorTabInitialState} from './editor-tab';
import hoveredTargetReducer, {hoveredTargetInitialState} from './hovered-target'; import hoveredTargetReducer, {hoveredTargetInitialState} from './hovered-target';
import menuReducer, {menuInitialState} from './menus'; import menuReducer, {menuInitialState} from './menus';
import localesReducer, {localesInitialState} from './locales';
import modalReducer, {modalsInitialState} from './modals'; import modalReducer, {modalsInitialState} from './modals';
import modeReducer, {modeInitialState} from './mode'; import modeReducer, {modeInitialState} from './mode';
import monitorReducer, {monitorsInitialState} from './monitors'; import monitorReducer, {monitorsInitialState} from './monitors';
...@@ -27,7 +26,6 @@ const guiInitialState = { ...@@ -27,7 +26,6 @@ const guiInitialState = {
colorPicker: colorPickerInitialState, colorPicker: colorPickerInitialState,
customProcedures: customProceduresInitialState, customProcedures: customProceduresInitialState,
editorTab: editorTabInitialState, editorTab: editorTabInitialState,
locales: localesInitialState,
mode: modeInitialState, mode: modeInitialState,
hoveredTarget: hoveredTargetInitialState, hoveredTarget: hoveredTargetInitialState,
stageSize: stageSizeInitialState, stageSize: stageSizeInitialState,
...@@ -60,20 +58,7 @@ const initFullScreen = function (currentState) { ...@@ -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({ const guiReducer = combineReducers({
assetDrag: assetDragReducer, assetDrag: assetDragReducer,
blockDrag: blockDragReducer, blockDrag: blockDragReducer,
...@@ -81,7 +66,6 @@ const guiReducer = combineReducers({ ...@@ -81,7 +66,6 @@ const guiReducer = combineReducers({
colorPicker: colorPickerReducer, colorPicker: colorPickerReducer,
customProcedures: customProceduresReducer, customProcedures: customProceduresReducer,
editorTab: editorTabReducer, editorTab: editorTabReducer,
locales: localesReducer,
mode: modeReducer, mode: modeReducer,
hoveredTarget: hoveredTargetReducer, hoveredTarget: hoveredTargetReducer,
stageSize: stageSizeReducer, stageSize: stageSizeReducer,
...@@ -99,6 +83,5 @@ export { ...@@ -99,6 +83,5 @@ export {
guiInitialState, guiInitialState,
guiMiddleware, guiMiddleware,
initFullScreen, initFullScreen,
initLocale,
initPlayer initPlayer
}; };
...@@ -10,7 +10,8 @@ const SELECT_LOCALE = 'scratch-gui/locales/SELECT_LOCALE'; ...@@ -10,7 +10,8 @@ const SELECT_LOCALE = 'scratch-gui/locales/SELECT_LOCALE';
const initialState = { const initialState = {
locale: 'en', locale: 'en',
messages: editorMessages messagesByLocale: editorMessages,
messages: editorMessages.en
}; };
const reducer = function (state, action) { const reducer = function (state, action) {
...@@ -19,12 +20,14 @@ const reducer = function (state, action) { ...@@ -19,12 +20,14 @@ const reducer = function (state, action) {
case SELECT_LOCALE: case SELECT_LOCALE:
return Object.assign({}, state, { return Object.assign({}, state, {
locale: action.locale, locale: action.locale,
messages: state.messages messagesByLocale: state.messagesByLocale,
messages: state.messagesByLocale[action.locale]
}); });
case UPDATE_LOCALES: case UPDATE_LOCALES:
return Object.assign({}, state, { return Object.assign({}, state, {
locale: state.locale, locale: state.locale,
messages: action.messages messagesByLocale: action.messagesByLocale,
messages: action.messagesByLocale[state.locale]
}); });
default: default:
return state; return state;
...@@ -41,13 +44,28 @@ const selectLocale = function (locale) { ...@@ -41,13 +44,28 @@ const selectLocale = function (locale) {
const setLocales = function (localesMessages) { const setLocales = function (localesMessages) {
return { return {
type: UPDATE_LOCALES, 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 { export {
reducer as default, reducer as default,
initialState as localesInitialState, initialState as localesInitialState,
initLocale,
selectLocale, selectLocale,
setLocales setLocales
}; };
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment