diff options
Diffstat (limited to 'front/odiparpack/app/containers/LanguageProvider')
5 files changed, 114 insertions, 0 deletions
diff --git a/front/odiparpack/app/containers/LanguageProvider/actions.js b/front/odiparpack/app/containers/LanguageProvider/actions.js new file mode 100644 index 0000000..129dda4 --- /dev/null +++ b/front/odiparpack/app/containers/LanguageProvider/actions.js @@ -0,0 +1,14 @@ +/* + * + * LanguageProvider actions + * + */ + +import { CHANGE_LOCALE } from './constants'; + +export default function changeLocale(languageLocale) { + return { + type: CHANGE_LOCALE, + locale: languageLocale, + }; +} diff --git a/front/odiparpack/app/containers/LanguageProvider/constants.js b/front/odiparpack/app/containers/LanguageProvider/constants.js new file mode 100644 index 0000000..365ac6d --- /dev/null +++ b/front/odiparpack/app/containers/LanguageProvider/constants.js @@ -0,0 +1,8 @@ +/* + * + * LanguageProvider constants + * + */ + +const CHANGE_LOCALE = 'CHANGE_LOCALE'; +export default CHANGE_LOCALE; diff --git a/front/odiparpack/app/containers/LanguageProvider/index.js b/front/odiparpack/app/containers/LanguageProvider/index.js new file mode 100644 index 0000000..666240d --- /dev/null +++ b/front/odiparpack/app/containers/LanguageProvider/index.js @@ -0,0 +1,51 @@ +/* + * + * LanguageProvider + * + * this component connects the redux state language locale to the + * IntlProvider component and i18n messages (loaded from `app/translations`) + */ + +import React from 'react'; +import PropTypes from 'prop-types'; +import { connect } from 'react-redux'; +import { createSelector } from 'reselect'; +import { IntlProvider } from 'react-intl'; + +import { makeSelectLocale } from './selectors'; + +export class LanguageProvider extends React.PureComponent { + // eslint-disable-line react/prefer-stateless-function + render() { + return ( + <IntlProvider + locale={this.props.locale} + key={this.props.locale} + messages={this.props.messages[this.props.locale]} + > + {React.Children.only(this.props.children)} + </IntlProvider> + ); + } +} + +LanguageProvider.propTypes = { + locale: PropTypes.string.isRequired, + messages: PropTypes.object.isRequired, + children: PropTypes.element.isRequired, +}; + +const mapStateToProps = createSelector(makeSelectLocale(), locale => ({ + locale, +})); + +function mapDispatchToProps(dispatch) { + return { + dispatch, + }; +} + +export default connect( + mapStateToProps, + mapDispatchToProps, +)(LanguageProvider); diff --git a/front/odiparpack/app/containers/LanguageProvider/reducer.js b/front/odiparpack/app/containers/LanguageProvider/reducer.js new file mode 100644 index 0000000..089c426 --- /dev/null +++ b/front/odiparpack/app/containers/LanguageProvider/reducer.js @@ -0,0 +1,25 @@ +/* + * + * LanguageProvider reducer + * + */ + +import { fromJS } from 'immutable'; + +import CHANGE_LOCALE from './constants'; +import { DEFAULT_LOCALE } from '../../i18n'; // eslint-disable-line + +export const initialState = fromJS({ + locale: DEFAULT_LOCALE, +}); + +function languageProviderReducer(state = initialState, action) { + switch (action.type) { + case CHANGE_LOCALE: + return state.set('locale', action.locale); + default: + return state; + } +} + +export default languageProviderReducer; diff --git a/front/odiparpack/app/containers/LanguageProvider/selectors.js b/front/odiparpack/app/containers/LanguageProvider/selectors.js new file mode 100644 index 0000000..3ef15a2 --- /dev/null +++ b/front/odiparpack/app/containers/LanguageProvider/selectors.js @@ -0,0 +1,16 @@ +import { createSelector } from 'reselect'; +import { initialState } from './reducer'; + +/** + * Direct selector to the languageToggle state domain + */ +const selectLanguage = state => state.get('language', initialState); + +/** + * Select the language locale + */ + +const makeSelectLocale = () => + createSelector(selectLanguage, languageState => languageState.get('locale')); + +export { selectLanguage, makeSelectLocale }; |
