2020-03-23 18:07:05 +01:00
|
|
|
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'
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
getCurrentLocaleMessages,
|
|
|
|
getEnLocaleMessages,
|
|
|
|
} from '../ducks/locale/locale'
|
2020-03-23 18:07:05 +01:00
|
|
|
|
|
|
|
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(() => {
|
2020-11-03 00:41:28 +01:00
|
|
|
return (key, ...args) =>
|
2020-03-23 18:07:05 +01:00
|
|
|
getMessage(currentLocale, current, key, ...args) ||
|
|
|
|
getMessage(currentLocale, en, key, ...args)
|
|
|
|
}, [currentLocale, current, en])
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
return <I18nContext.Provider value={t}>{props.children}</I18nContext.Provider>
|
2020-03-23 18:07:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
getChildContext() {
|
2020-03-23 18:07:05 +01:00
|
|
|
return {
|
|
|
|
t: this.context,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
2020-03-23 18:07:05 +01:00
|
|
|
return this.props.children
|
|
|
|
}
|
|
|
|
}
|