Migrate "i18n-provider" to new context API (#8213)
The "i18n-provider" module has been replaced by a new `i18n.js` module in the `contexts` directory which provides the `t` function via the new React Context API. The legacy context API is still used throughout the codebase, so a legacy context provider has also been added as a shim until we migrate away from the old API. The migration does require changing every single place where the `t` function is used, so it is a non-trivial amount of work. This shim allows us to tackle it one piece at a time without breaking anything. This was placed in a new `contexts` directory because it didn't seem to belong in any existing categories. It certainly isn't a higher-order component.feature/default_network_editable
parent
2301d9980e
commit
2b6aff535e
@ -0,0 +1,61 @@ |
||||
import React, { Component, createContext, useMemo } from 'react' |
||||
import PropTypes from 'prop-types' |
||||
import { useSelector } from 'react-redux' |
||||
import { getMessage } from '../helpers/utils/i18n-helper' |
||||
import { getCurrentLocale } from '../ducks/metamask/metamask' |
||||
import { getCurrentLocaleMessages, getEnLocaleMessages } from '../ducks/locale/locale' |
||||
|
||||
export const I18nContext = createContext((key) => `[${key}]`) |
||||
|
||||
export const I18nProvider = (props) => { |
||||
const currentLocale = useSelector(getCurrentLocale) |
||||
const current = useSelector(getCurrentLocaleMessages) |
||||
const en = useSelector(getEnLocaleMessages) |
||||
|
||||
const t = useMemo(() => { |
||||
return (key, ...args) => ( |
||||
getMessage(currentLocale, current, key, ...args) || |
||||
getMessage(currentLocale, en, key, ...args) |
||||
) |
||||
}, [currentLocale, current, en]) |
||||
|
||||
return ( |
||||
<I18nContext.Provider value={t}> |
||||
{ props.children } |
||||
</I18nContext.Provider> |
||||
) |
||||
} |
||||
|
||||
I18nProvider.propTypes = { |
||||
children: PropTypes.node, |
||||
} |
||||
|
||||
I18nProvider.defaultProps = { |
||||
children: undefined, |
||||
} |
||||
|
||||
export class LegacyI18nProvider extends Component { |
||||
static propTypes = { |
||||
children: PropTypes.node, |
||||
} |
||||
|
||||
static defaultProps = { |
||||
children: undefined, |
||||
} |
||||
|
||||
static contextType = I18nContext |
||||
|
||||
static childContextTypes = { |
||||
t: PropTypes.func, |
||||
} |
||||
|
||||
getChildContext () { |
||||
return { |
||||
t: this.context, |
||||
} |
||||
} |
||||
|
||||
render () { |
||||
return this.props.children |
||||
} |
||||
} |
@ -1,46 +0,0 @@ |
||||
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) |
Loading…
Reference in new issue