2020-01-09 04:34:58 +01:00
|
|
|
import { Component } from 'react'
|
|
|
|
import { connect } from 'react-redux'
|
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import { withRouter } from 'react-router-dom'
|
|
|
|
import { compose } from 'recompose'
|
|
|
|
import { getMessage } from '../utils/i18n-helper'
|
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-21 18:31:45 +02:00
|
|
|
if (!key) {
|
|
|
|
return defaultValue
|
|
|
|
}
|
2019-09-06 15:47:07 +02:00
|
|
|
const { localeMessages: { current, en } = {}, currentLocale } = this.props
|
2019-09-21 18:31:45 +02:00
|
|
|
return getMessage(currentLocale, current, key, ...args) || getMessage(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
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {string} key - The message key
|
|
|
|
* @param {string[]} args - A list of message substitution replacements
|
|
|
|
* @returns {string|undefined|null} - The localized message if available
|
2019-05-06 19:34:16 +02:00
|
|
|
*/
|
2018-07-09 22:07:06 +02:00
|
|
|
t (key, ...args) {
|
2019-09-21 18:31:45 +02:00
|
|
|
return getMessage(currentLocale, current, key, ...args) || getMessage(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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export default compose(
|
2018-04-02 22:15:31 +02:00
|
|
|
withRouter,
|
|
|
|
connect(mapStateToProps)
|
|
|
|
)(I18nProvider)
|
2018-03-29 17:00:44 +02:00
|
|
|
|