Skip to content

Commit

Permalink
use i18n in redux and components
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonYCHuang committed Nov 12, 2016
1 parent dd05206 commit 56c3adc
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 35 deletions.
7 changes: 7 additions & 0 deletions client/app/bundles/comments/actions/commentsActionCreators.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,10 @@ export function submitComment(comment) {
);
};
}

export function setLocale(locale) {
return {
type: actionTypes.SET_LOCALE,
locale,
};
}
24 changes: 24 additions & 0 deletions client/app/bundles/comments/common/i18n.jsx
Original file line number Diff line number Diff line change
@@ -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) => (
<select onChange={(e) => onChange(e.target.value)} >
<option value="en">English</option>
<option value="de">Deutsch</option>
<option value="ja">日本語</option>
<option value="zh-CN">简体中文</option>
<option value="zh-TW">正體中文</option>
</select>
);

const SetI18nLocale = (locale) => {
I18n.locale = locale;
};

export { InitI18n, SelectLanguage, SetI18nLocale };
18 changes: 11 additions & 7 deletions client/app/bundles/comments/components/CommentBox/CommentBox.jsx
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -30,17 +31,20 @@ export default class CommentBox extends BaseComponent {
leave: css.elementLeave,
leaveActive: css.elementLeaveActive,
};
const locale = data.get('locale');
SetI18nLocale(locale);

return (
<div className="commentBox container">
<h2>
Comments {data.get('isFetching') && 'Loading...'}
{ I18n.t('comments') } {data.get('isFetching') && 'Loading...'}
</h2>
<p>
<b>Text</b> supports Github Flavored Markdown.
Comments older than 24 hours are deleted.<br />
<b>Name</b> is preserved. <b>Text</b> is reset, between submits.
</p>
{ SelectLanguage(actions.setLocale) }
<ul>
<li>{ I18n.t('description.support_markdown') }</li>
<li>{ I18n.t('description.delete_rule') }</li>
<li>{ I18n.t('description.submit_rule') }</li>
</ul>
<CommentForm
isSaving={data.get('isSaving')}
error={data.get('submitCommentError')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import NavItem from 'react-bootstrap/lib/NavItem';
import Alert from 'react-bootstrap/lib/Alert';
import ReactCSSTransitionGroup from 'react/lib/ReactCSSTransitionGroup';
import _ from 'lodash';
import I18n from 'i18n-js';

import BaseComponent from 'libs/components/BaseComponent';

const emptyComment = { author: '', text: '' };
const textPlaceholder = 'Say something using markdown...';

function bsStyleFor(propName, error) {
if (error) {
Expand Down Expand Up @@ -120,8 +120,8 @@ export default class CommentForm extends BaseComponent {
<form className="commentForm form-horizontal" onSubmit={this.handleSubmit}>
<Input
type="text"
label="Name"
placeholder="Your Name"
label={I18n.t('input.name.label')}
placeholder={I18n.t('input.name.placeholder')}
labelClassName="col-sm-2"
wrapperClassName="col-sm-10"
ref={node => { this.horizontalAuthorNode = node; }}
Expand All @@ -133,8 +133,8 @@ export default class CommentForm extends BaseComponent {
/>
<Input
type="textarea"
label="Text"
placeholder={textPlaceholder}
label={I18n.t('input.text.label')}
placeholder={I18n.t('input.text.placeholder')}
labelClassName="col-sm-2"
wrapperClassName="col-sm-10"
ref={node => { this.horizontalTextNode = node; }}
Expand All @@ -149,7 +149,7 @@ export default class CommentForm extends BaseComponent {
<input
type="submit"
className="btn btn-primary"
value={this.props.isSaving ? 'Saving...' : 'Post'}
value={this.props.isSaving ? `${I18n.t('input.saving')}...` : I18n.t('input.post')}
disabled={this.props.isSaving}
/>
</div>
Expand All @@ -166,8 +166,8 @@ export default class CommentForm extends BaseComponent {
<form className="commentForm form" onSubmit={this.handleSubmit}>
<Input
type="text"
label="Name"
placeholder="Your Name"
label={I18n.t('input.name.label')}
placeholder={I18n.t('input.name.placeholder')}
ref={node => { this.stackedAuthorNode = node; }}
value={this.state.comment.author}
onChange={this.handleChange}
Expand All @@ -177,8 +177,8 @@ export default class CommentForm extends BaseComponent {
/>
<Input
type="textarea"
label="Text"
placeholder={textPlaceholder}
label={I18n.t('input.text.label')}
placeholder={I18n.t('input.text.placeholder')}
ref={node => { this.stackedTextNode = node; }}
value={this.state.comment.text}
onChange={this.handleChange}
Expand All @@ -189,7 +189,7 @@ export default class CommentForm extends BaseComponent {
<input
type="submit"
className="btn btn-primary"
value={this.props.isSaving ? 'Saving...' : 'Post'}
value={this.props.isSaving ? `${I18n.t('input.saving')}...` : I18n.t('input.post')}
disabled={this.props.isSaving}
/>
</form>
Expand All @@ -208,7 +208,7 @@ export default class CommentForm extends BaseComponent {
<input
type="text"
className="form-control"
placeholder="Your Name"
placeholder={I18n.t('input.name.placeholder')}
ref={node => { this.inlineAuthorNode = node; }}
value={this.state.comment.author}
onChange={this.handleChange}
Expand All @@ -219,7 +219,7 @@ export default class CommentForm extends BaseComponent {
<input
type="text"
className="form-control"
placeholder={textPlaceholder}
placeholder={I18n.t('input.text.placeholder')}
ref={node => { this.inlineTextNode = node; }}
value={this.state.comment.text}
onChange={this.handleChange}
Expand All @@ -230,7 +230,10 @@ export default class CommentForm extends BaseComponent {
<input
type="submit"
className="btn btn-primary"
value={this.props.isSaving ? 'Saving...' : 'Post'}
value={this.props.isSaving
? `${I18n.t('input.saving')}...`
: I18n.t('input.post')
}
disabled={this.props.isSaving}
/>
</Col>
Expand Down Expand Up @@ -292,9 +295,9 @@ export default class CommentForm extends BaseComponent {
</ReactCSSTransitionGroup>

<Nav bsStyle="pills" activeKey={this.state.formMode} onSelect={this.handleSelect}>
<NavItem eventKey={0}>Horizontal Form</NavItem>
<NavItem eventKey={1}>Stacked Form</NavItem>
<NavItem eventKey={2}>Inline Form</NavItem>
<NavItem eventKey={0}>{I18n.t('form.horizontal')}</NavItem>
<NavItem eventKey={1}>{I18n.t('form.stacked')}</NavItem>
<NavItem eventKey={2}>{I18n.t('form.inline')}</NavItem>
</Nav>
{inputForm}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => (
<SimpleCommentScreen {...props} railsContext={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() {
Expand Down Expand Up @@ -65,6 +75,10 @@ export default class SimpleCommentScreen extends BaseComponent {
);
}

handleSetLocale(locale) {
this.setState({ locale: locale });
}

render() {
const cssTransitionGroupClassNames = {
enter: css.elementEnter,
Expand All @@ -73,13 +87,18 @@ export default class SimpleCommentScreen extends BaseComponent {
leaveActive: css.elementLeaveActive,
};

const { locale } = this.state;
SetI18nLocale(locale);

return (
<div className="commentBox container">
<h2>Comments</h2>
<p>
Text take Github Flavored Markdown. Comments older than 24 hours are deleted.<br />
<b>Name</b> is preserved. <b>Text</b> is reset, between submits.
</p>
<h2>{ I18n.t('comments') }</h2>
{ SelectLanguage(this.handleSetLocale) }
<ul>
<li>{ I18n.t('description.support_markdown') }</li>
<li>{ I18n.t('description.delete_rule') }</li>
<li>{ I18n.t('description.submit_rule') }</li>
</ul>
<CommentForm
isSaving={this.state.isSaving}
actions={{ submitComment: this.handleCommentSubmit }}
Expand Down
2 changes: 2 additions & 0 deletions client/app/bundles/comments/constants/commentsConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ export const SUBMIT_COMMENT_FAILURE = 'SUBMIT_COMMENT_FAILURE';

export const SET_IS_FETCHING = 'SET_IS_FETCHING';
export const SET_IS_SAVING = 'SET_IS_SAVING';

export const SET_LOCALE = 'SET_LOCALE';
9 changes: 8 additions & 1 deletion client/app/bundles/comments/reducers/commentsReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ export const $$initialState = Immutable.fromJS({
submitCommentError: null,
isFetching: false,
isSaving: false,
locale: null,
});

export default function commentsReducer($$state = $$initialState, action = null) {
const { type, comment, comments, error } = action;
const { type, comment, comments, error, locale } = action;

switch (type) {

Expand Down Expand Up @@ -65,6 +66,12 @@ export default function commentsReducer($$state = $$initialState, action = null)
});
}

case actionTypes.SET_LOCALE: {
return $$state.merge({
locale,
});
}

default: {
return $$state;
}
Expand Down
3 changes: 3 additions & 0 deletions client/app/bundles/comments/startup/App.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import React from 'react';
import { Provider } from 'react-redux';
import ReactOnRails from 'react-on-rails';
import { InitI18n } from '../common/i18n';

import NonRouterCommentsContainer from '../containers/NonRouterCommentsContainer';

export default (_props, _railsContext) => {
const store = ReactOnRails.getStore('commentsStore');

InitI18n(_railsContext);

return (
<Provider store={store}>
<NonRouterCommentsContainer />
Expand Down
3 changes: 3 additions & 0 deletions client/app/bundles/comments/startup/ClientRouterApp.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -16,6 +17,8 @@ export default (_props, _railsContext) => {
store
);

InitI18n(_railsContext);

return (
<Provider store={store}>
<Router history={history} children={routes} />
Expand Down

0 comments on commit 56c3adc

Please sign in to comment.