-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
7cd7365
15a7cca
7b6bc0c
1612dac
2f36f35
cf8c4ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'; | ||
|
||
import i18n from './i18n'; | ||
|
||
class I18nProvider extends Component { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
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(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); |
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: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
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'; |
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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
categorySelection, | ||
categories | ||
}); | ||
|
@@ -23,7 +26,9 @@ const store = createStore( | |
|
||
ReactDOM.render( | ||
<Provider store={store}> | ||
<App /> | ||
<I18nProvider> | ||
<App /> | ||
</I18nProvider> | ||
</Provider>, | ||
document.getElementById('root') | ||
); | ||
|
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); |
There was a problem hiding this comment.
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
andreact-i18next
topackage.json
viayarn add -E ...
(let's keep exact versions to avoid update surprises)