1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-28 23:06:37 +01:00
metamask-extension/ui/app/helpers/higher-order-components/i18n-provider.js

67 lines
1.8 KiB
JavaScript
Raw Normal View History

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'
class I18nProvider extends Component {
2018-08-01 04:21:25 +02:00
tOrDefault = (key, defaultValue, ...args) => {
if (!key) {
return defaultValue
}
const { localeMessages: { current, en } = {}, currentLocale } = this.props
return getMessage(currentLocale, current, key, ...args) || getMessage(currentLocale, en, key, ...args) || defaultValue
2018-08-01 04:21:25 +02:00
}
getChildContext () {
const { localeMessages, currentLocale } = this.props
const { current, en } = localeMessages
return {
/**
* Returns a localized message for the given key
* @param {string} key - The message key
* @param {string[]} args - A list of message substitution replacements
* @returns {string|undefined|null} - The localized message if available
*/
t (key, ...args) {
return getMessage(currentLocale, current, key, ...args) || getMessage(currentLocale, en, key, ...args) || `[${key}]`
},
2018-08-01 04:21:25 +02:00
tOrDefault: this.tOrDefault,
tOrKey: (key, ...args) => {
2018-08-01 04:21:25 +02:00
return this.tOrDefault(key, key, ...args)
},
}
}
render () {
return this.props.children
}
}
I18nProvider.propTypes = {
localeMessages: PropTypes.object,
currentLocale: PropTypes.string,
children: PropTypes.object,
}
I18nProvider.childContextTypes = {
t: PropTypes.func,
tOrDefault: PropTypes.func,
2018-08-01 04:21:25 +02:00
tOrKey: PropTypes.func,
}
const mapStateToProps = state => {
const { localeMessages, metamask: { currentLocale } } = state
return {
currentLocale,
localeMessages,
}
}
export default compose(
withRouter,
connect(mapStateToProps)
)(I18nProvider)