From 56c3adca2e6383cfb4b0d116e474a1a072c96eea Mon Sep 17 00:00:00 2001 From: jasonych99 Date: Sat, 12 Nov 2016 17:43:18 +0100 Subject: [PATCH] use i18n in redux and components --- .../actions/commentsActionCreators.js | 7 ++++ client/app/bundles/comments/common/i18n.jsx | 24 ++++++++++++ .../components/CommentBox/CommentBox.jsx | 18 +++++---- .../CommentBox/CommentForm/CommentForm.jsx | 37 ++++++++++-------- .../SimpleCommentScreen.jsx | 39 ++++++++++++++----- .../comments/constants/commentsConstants.js | 2 + .../comments/reducers/commentsReducer.js | 9 ++++- client/app/bundles/comments/startup/App.jsx | 3 ++ .../comments/startup/ClientRouterApp.jsx | 3 ++ 9 files changed, 107 insertions(+), 35 deletions(-) create mode 100644 client/app/bundles/comments/common/i18n.jsx diff --git a/client/app/bundles/comments/actions/commentsActionCreators.js b/client/app/bundles/comments/actions/commentsActionCreators.js index 941e91d2b..f9e80018d 100644 --- a/client/app/bundles/comments/actions/commentsActionCreators.js +++ b/client/app/bundles/comments/actions/commentsActionCreators.js @@ -64,3 +64,10 @@ export function submitComment(comment) { ); }; } + +export function setLocale(locale) { + return { + type: actionTypes.SET_LOCALE, + locale, + }; +} diff --git a/client/app/bundles/comments/common/i18n.jsx b/client/app/bundles/comments/common/i18n.jsx new file mode 100644 index 000000000..aba1933c9 --- /dev/null +++ b/client/app/bundles/comments/common/i18n.jsx @@ -0,0 +1,24 @@ +import React from 'react'; +import I18n from 'i18n-js'; + +const InitI18n = (railsContext) => { + I18n.translations = railsContext.translations; + I18n.defaultLocale = railsContext.i18nDefaultLocale; + I18n.fallbacks = true; +}; + +const SelectLanguage = (onChange) => ( + +); + +const SetI18nLocale = (locale) => { + I18n.locale = locale; +}; + +export { InitI18n, SelectLanguage, SetI18nLocale }; diff --git a/client/app/bundles/comments/components/CommentBox/CommentBox.jsx b/client/app/bundles/comments/components/CommentBox/CommentBox.jsx index 6b74a43f8..8272fcd2b 100644 --- a/client/app/bundles/comments/components/CommentBox/CommentBox.jsx +++ b/client/app/bundles/comments/components/CommentBox/CommentBox.jsx @@ -1,9 +1,10 @@ import BaseComponent from 'libs/components/BaseComponent'; import React, { PropTypes } from 'react'; - +import I18n from 'i18n-js'; import CommentForm from './CommentForm/CommentForm'; import CommentList from './CommentList/CommentList'; import css from './CommentBox.scss'; +import { SelectLanguage, SetI18nLocale } from '../../common/i18n'; export default class CommentBox extends BaseComponent { static propTypes = { @@ -30,17 +31,20 @@ export default class CommentBox extends BaseComponent { leave: css.elementLeave, leaveActive: css.elementLeaveActive, }; + const locale = data.get('locale'); + SetI18nLocale(locale); return (

- Comments {data.get('isFetching') && 'Loading...'} + { I18n.t('comments') } {data.get('isFetching') && 'Loading...'}

-

- Text supports Github Flavored Markdown. - Comments older than 24 hours are deleted.
- Name is preserved. Text is reset, between submits. -

+ { SelectLanguage(actions.setLocale) } + { this.horizontalAuthorNode = node; }} @@ -133,8 +133,8 @@ export default class CommentForm extends BaseComponent { /> { this.horizontalTextNode = node; }} @@ -149,7 +149,7 @@ export default class CommentForm extends BaseComponent {
@@ -166,8 +166,8 @@ export default class CommentForm extends BaseComponent {
{ this.stackedAuthorNode = node; }} value={this.state.comment.author} onChange={this.handleChange} @@ -177,8 +177,8 @@ export default class CommentForm extends BaseComponent { /> { this.stackedTextNode = node; }} value={this.state.comment.text} onChange={this.handleChange} @@ -189,7 +189,7 @@ export default class CommentForm extends BaseComponent {
@@ -208,7 +208,7 @@ export default class CommentForm extends BaseComponent { { this.inlineAuthorNode = node; }} value={this.state.comment.author} onChange={this.handleChange} @@ -219,7 +219,7 @@ export default class CommentForm extends BaseComponent { { this.inlineTextNode = node; }} value={this.state.comment.text} onChange={this.handleChange} @@ -230,7 +230,10 @@ export default class CommentForm extends BaseComponent { @@ -292,9 +295,9 @@ export default class CommentForm extends BaseComponent { {inputForm} diff --git a/client/app/bundles/comments/components/SimpleCommentScreen/SimpleCommentScreen.jsx b/client/app/bundles/comments/components/SimpleCommentScreen/SimpleCommentScreen.jsx index c1898f227..75ab49944 100644 --- a/client/app/bundles/comments/components/SimpleCommentScreen/SimpleCommentScreen.jsx +++ b/client/app/bundles/comments/components/SimpleCommentScreen/SimpleCommentScreen.jsx @@ -2,28 +2,38 @@ import React from 'react'; import Immutable from 'immutable'; import request from 'axios'; import ReactOnRails from 'react-on-rails'; - +import I18n from 'i18n-js'; import BaseComponent from 'libs/components/BaseComponent'; import CommentForm from '../CommentBox/CommentForm/CommentForm'; import CommentList from '../CommentBox/CommentList/CommentList'; import css from './SimpleCommentScreen.scss'; +import { InitI18n, SelectLanguage, SetI18nLocale } from '../../common/i18n'; + +export default (props, railsContext) => ( + +); + +class SimpleCommentScreen extends BaseComponent { + constructor(props) { + super(props); + const { railsContext } = props; -export default class SimpleCommentScreen extends BaseComponent { - constructor(props, context) { - super(props, context); this.state = { $$comments: Immutable.fromJS([]), isSaving: false, fetchCommentsError: null, submitCommentError: null, + locale: null, }; - _.bindAll(this, 'fetchComments', 'handleCommentSubmit'); + InitI18n(railsContext); + _.bindAll(this, 'fetchComments', 'handleCommentSubmit', 'handleSetLocale'); } componentDidMount() { this.fetchComments(); + this.handleSetLocale(this.props.railsContext.i18nLocale); } fetchComments() { @@ -65,6 +75,10 @@ export default class SimpleCommentScreen extends BaseComponent { ); } + handleSetLocale(locale) { + this.setState({ locale: locale }); + } + render() { const cssTransitionGroupClassNames = { enter: css.elementEnter, @@ -73,13 +87,18 @@ export default class SimpleCommentScreen extends BaseComponent { leaveActive: css.elementLeaveActive, }; + const { locale } = this.state; + SetI18nLocale(locale); + return (
-

Comments

-

- Text take Github Flavored Markdown. Comments older than 24 hours are deleted.
- Name is preserved. Text is reset, between submits. -

+

{ I18n.t('comments') }

+ { SelectLanguage(this.handleSetLocale) } +
    +
  • { I18n.t('description.support_markdown') }
  • +
  • { I18n.t('description.delete_rule') }
  • +
  • { I18n.t('description.submit_rule') }
  • +
{ const store = ReactOnRails.getStore('commentsStore'); + InitI18n(_railsContext); + return ( diff --git a/client/app/bundles/comments/startup/ClientRouterApp.jsx b/client/app/bundles/comments/startup/ClientRouterApp.jsx index bb47ee2d8..1cb8a2d9b 100644 --- a/client/app/bundles/comments/startup/ClientRouterApp.jsx +++ b/client/app/bundles/comments/startup/ClientRouterApp.jsx @@ -4,6 +4,7 @@ import { Provider } from 'react-redux'; import ReactOnRails from 'react-on-rails'; import { syncHistoryWithStore } from 'react-router-redux'; import { Router, browserHistory } from 'react-router'; +import { InitI18n } from '../common/i18n'; import routes from '../routes/routes'; @@ -16,6 +17,8 @@ export default (_props, _railsContext) => { store ); + InitI18n(_railsContext); + return (