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')
|
2019-03-22 00:03:30 +01:00
|
|
|
const t = require('../utils/i18n-helper').getMessage
|
2018-03-29 17:00:44 +02:00
|
|
|
|
|
|
|
class I18nProvider extends Component {
|
2018-08-01 04:21:25 +02:00
|
|
|
tOrDefault = (key, defaultValue, ...args) => {
|
2019-09-06 15:47:07 +02:00
|
|
|
const { localeMessages: { current, en } = {}, currentLocale } = this.props
|
|
|
|
return t(currentLocale, current, key, ...args) || t(currentLocale, en, key, ...args) || defaultValue
|
2018-08-01 04:21:25 +02:00
|
|
|
}
|
|
|
|
|
2018-05-05 17:11:53 +02:00
|
|
|
getChildContext () {
|
2019-09-06 15:47:07 +02:00
|
|
|
const { localeMessages, currentLocale } = this.props
|
2018-07-09 22:07:06 +02:00
|
|
|
const { current, en } = localeMessages
|
2018-03-29 17:00:44 +02:00
|
|
|
return {
|
2019-05-06 19:34:16 +02:00
|
|
|
/**
|
|
|
|
* Returns a localized message for the given key
|
|
|
|
* @param {string} key The message key
|
|
|
|
* @param {string[]} args A list of message substitution replacements
|
|
|
|
* @return {string|undefined|null} The localized message if available
|
|
|
|
*/
|
2018-07-09 22:07:06 +02:00
|
|
|
t (key, ...args) {
|
2019-05-06 19:34:16 +02:00
|
|
|
if (key === undefined || key === null) {
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
2019-09-06 15:47:07 +02:00
|
|
|
return t(currentLocale, current, key, ...args) || t(currentLocale, en, key, ...args) || `[${key}]`
|
2018-07-09 22:07:06 +02:00
|
|
|
},
|
2018-08-01 04:21:25 +02:00
|
|
|
tOrDefault: this.tOrDefault,
|
2019-05-01 04:53:12 +02:00
|
|
|
tOrKey: (key, ...args) => {
|
2018-08-01 04:21:25 +02:00
|
|
|
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,
|
2019-09-06 15:47:07 +02:00
|
|
|
currentLocale: PropTypes.string,
|
2018-03-29 17:00:44 +02:00
|
|
|
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 => {
|
2019-09-06 15:47:07 +02:00
|
|
|
const { localeMessages, metamask: { currentLocale } } = state
|
2018-03-29 17:00:44 +02:00
|
|
|
return {
|
2019-09-06 15:47:07 +02:00
|
|
|
currentLocale,
|
2018-03-29 17:00:44 +02:00
|
|
|
localeMessages,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 22:15:31 +02:00
|
|
|
module.exports = compose(
|
|
|
|
withRouter,
|
|
|
|
connect(mapStateToProps)
|
|
|
|
)(I18nProvider)
|
2018-03-29 17:00:44 +02:00
|
|
|
|