2018-03-29 17:00:44 +02:00
|
|
|
const { Component } = require('react')
|
|
|
|
const connect = require('react-redux').connect
|
|
|
|
const PropTypes = require('prop-types')
|
2018-04-02 22:15:31 +02:00
|
|
|
const { withRouter } = require('react-router-dom')
|
|
|
|
const { compose } = require('recompose')
|
2018-03-29 17:00:44 +02:00
|
|
|
const t = require('../i18n-helper').getMessage
|
|
|
|
|
|
|
|
class I18nProvider extends Component {
|
2018-08-01 04:21:25 +02:00
|
|
|
tOrDefault = (key, defaultValue, ...args) => {
|
|
|
|
const { localeMessages: { current, en } = {} } = this.props
|
|
|
|
return t(current, key, ...args) || t(en, key, ...args) || defaultValue
|
|
|
|
}
|
|
|
|
|
2018-05-05 17:11:53 +02:00
|
|
|
getChildContext () {
|
2018-03-29 17:00:44 +02:00
|
|
|
const { localeMessages } = this.props
|
2018-07-09 22:07:06 +02:00
|
|
|
const { current, en } = localeMessages
|
2018-03-29 17:00:44 +02:00
|
|
|
return {
|
2018-07-09 22:07:06 +02:00
|
|
|
t (key, ...args) {
|
|
|
|
return t(current, key, ...args) || t(en, key, ...args) || `[${key}]`
|
|
|
|
},
|
2018-08-01 04:21:25 +02:00
|
|
|
tOrDefault: this.tOrDefault,
|
|
|
|
tOrKey (key, ...args) {
|
|
|
|
return this.tOrDefault(key, key, ...args)
|
2018-07-31 07:03:20 +02:00
|
|
|
},
|
2018-03-29 17:00:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 17:11:53 +02:00
|
|
|
render () {
|
2018-03-30 00:46:45 +02:00
|
|
|
return this.props.children
|
2018-03-29 17:00:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
I18nProvider.propTypes = {
|
|
|
|
localeMessages: PropTypes.object,
|
|
|
|
children: PropTypes.object,
|
|
|
|
}
|
|
|
|
|
|
|
|
I18nProvider.childContextTypes = {
|
|
|
|
t: PropTypes.func,
|
2018-07-31 07:03:20 +02:00
|
|
|
tOrDefault: PropTypes.func,
|
2018-08-01 04:21:25 +02:00
|
|
|
tOrKey: PropTypes.func,
|
2018-03-29 17:00:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = state => {
|
|
|
|
const { localeMessages } = state
|
|
|
|
return {
|
|
|
|
localeMessages,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 22:15:31 +02:00
|
|
|
module.exports = compose(
|
|
|
|
withRouter,
|
|
|
|
connect(mapStateToProps)
|
|
|
|
)(I18nProvider)
|
2018-03-29 17:00:44 +02:00
|
|
|
|