Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set up basic i18n #20

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/app/i18n/I18nProvider.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { I18nextProvider } from 'react-i18next';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't forget to add i18next and react-i18next to package.json via yarn add -E ... (let's keep exact versions to avoid update surprises)


import i18n from './i18n';

class I18nProvider extends Component {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this wrapper component, I suggest using thunk action with a side effect, like this:

export const setLocale = locale => dispatch => {
  dispatch({
    type: SET_LOCALE,
    locale,
  });
  i18n.changeLanguage(locale);
};

Of course, handle the SET_LOCALE action in the reducer by saving the locale in the store state.

This way you can remove this I18nProvider wrapper, not rely on the deprecated componentWillUpdate method, and it's also a bit more inline with the Redux philosophy of keeping side effects in action creators.

componentWillUpdate(nextProps) {
if (nextProps.preferredLanguage !== this.props.preferredLanguage) {
i18n.changeLanguage(nextProps.preferredLanguage);
}
}

render() {
return (
<I18nextProvider i18n={i18n}>
{this.props.children}
</I18nextProvider>
);
}
}

I18nProvider.propTypes = {
children: PropTypes.node,
// connect:
preferredLanguage: PropTypes.string
};

const mapStateToProps = state => ({
preferredLanguage: state.i18n.preferredLanguage
});

export default connect(mapStateToProps)(I18nProvider);
27 changes: 27 additions & 0 deletions src/app/i18n/i18n.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import i18next from 'i18next';

i18next.init({
interpolation: {
// React already does escaping
escapeValue: false
},
lng: 'ro',
resources: {
ro: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Find a way to keep the translation files separate: ro.json, en.json. Not sure how, but i18next should support it. You could also import jsons via the webpack json loader.

translation: {
proposals: {
pageTitle: 'Pagina de propuneri'
}
}
},
en: {
translation: {
proposals: {
pageTitle: 'Proposals page'
}
}
}
}
});

export default i18next;
2 changes: 2 additions & 0 deletions src/app/i18n/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as I18nProvider } from './I18nProvider';
export { default as i18n } from './reducer';
10 changes: 10 additions & 0 deletions src/app/i18n/reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const i18n = (state = {
preferredLanguage: 'ro',
}, action) => {
switch (action.type) {
default:
return state;
}
};

export default i18n;
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import { Provider } from 'react-redux';
import { combineReducers, createStore } from 'redux';

import { App } from './app';
import { I18nProvider } from './app/i18n';
import registerServiceWorker from './registerServiceWorker';

// Import module reducers
import { i18n } from './app/i18n';
import { categorySelection } from './category-selection';
import { categories } from './categories';

const rootReducer = combineReducers({
i18n,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

categorySelection,
categories
});
Expand All @@ -23,7 +26,9 @@ const store = createStore(

ReactDOM.render(
<Provider store={store}>
<App />
<I18nProvider>
<App />
</I18nProvider>
</Provider>,
document.getElementById('root')
);
Expand Down
13 changes: 11 additions & 2 deletions src/proposals/components/ProposalsPage/ProposalsPage.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { translate } from 'react-i18next';

export default class ProposalsPage extends Component {
class ProposalsPage extends Component {
render() {
return <p>Proposals page</p>;
return <p>{this.props.t('proposals.pageTitle')}</p>;
}
}

ProposalsPage.propTypes = {
// translate HoC:
t: PropTypes.func
};

export default translate()(ProposalsPage);