You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.3 KiB
56 lines
1.3 KiB
7 years ago
|
const { Component } = require('react')
|
||
|
const connect = require('react-redux').connect
|
||
|
const PropTypes = require('prop-types')
|
||
7 years ago
|
const { withRouter } = require('react-router-dom')
|
||
|
const { compose } = require('recompose')
|
||
6 years ago
|
const t = require('../utils/i18n-helper').getMessage
|
||
7 years ago
|
|
||
|
class I18nProvider extends Component {
|
||
6 years ago
|
tOrDefault = (key, defaultValue, ...args) => {
|
||
|
const { localeMessages: { current, en } = {} } = this.props
|
||
|
return t(current, key, ...args) || t(en, key, ...args) || defaultValue
|
||
|
}
|
||
|
|
||
7 years ago
|
getChildContext () {
|
||
7 years ago
|
const { localeMessages } = this.props
|
||
6 years ago
|
const { current, en } = localeMessages
|
||
7 years ago
|
return {
|
||
6 years ago
|
t (key, ...args) {
|
||
|
return t(current, key, ...args) || t(en, key, ...args) || `[${key}]`
|
||
|
},
|
||
6 years ago
|
tOrDefault: this.tOrDefault,
|
||
|
tOrKey (key, ...args) {
|
||
|
return this.tOrDefault(key, key, ...args)
|
||
6 years ago
|
},
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
render () {
|
||
7 years ago
|
return this.props.children
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
|
I18nProvider.propTypes = {
|
||
|
localeMessages: PropTypes.object,
|
||
|
children: PropTypes.object,
|
||
|
}
|
||
|
|
||
|
I18nProvider.childContextTypes = {
|
||
|
t: PropTypes.func,
|
||
6 years ago
|
tOrDefault: PropTypes.func,
|
||
6 years ago
|
tOrKey: PropTypes.func,
|
||
7 years ago
|
}
|
||
|
|
||
|
const mapStateToProps = state => {
|
||
|
const { localeMessages } = state
|
||
|
return {
|
||
|
localeMessages,
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
module.exports = compose(
|
||
|
withRouter,
|
||
|
connect(mapStateToProps)
|
||
|
)(I18nProvider)
|
||
7 years ago
|
|