1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/ui/app/helpers/higher-order-components/i18n-provider.js
Mark Stacey 624523168f
Remove fallback for missing localized messages (#8212)
The translation helper function we use everywhere (`t`) would fallback
to the message `[${key}]` for any key not found in the set of localized
messages. Instead it how returns `undefined` in that case.

`[${key}]` isn't something you'd typically want to show to users, so
this fallback wasn't overly useful in practice. At best it served to
hide errors.

The falsey return value in the case where the message is missing makes
it easier to implement a fallback for that case. Such a fallback is
used in the `confirm-transaction-base` component, to restore the
fallback behavior accidentally changed in #8211
2020-03-23 11:54:57 -03:00

47 lines
1.2 KiB
JavaScript

import { Component } from 'react'
import { connect } from 'react-redux'
import PropTypes from 'prop-types'
import { getMessage } from '../utils/i18n-helper'
class I18nProvider extends Component {
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)
},
}
}
render () {
return this.props.children
}
}
I18nProvider.propTypes = {
localeMessages: PropTypes.object,
currentLocale: PropTypes.string,
children: PropTypes.object,
}
I18nProvider.childContextTypes = {
t: PropTypes.func,
}
const mapStateToProps = (state) => {
const { localeMessages, metamask: { currentLocale } } = state
return {
currentLocale,
localeMessages,
}
}
export default connect(mapStateToProps)(I18nProvider)