You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.4 KiB
62 lines
1.4 KiB
4 years ago
|
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';
|
||
4 years ago
|
import {
|
||
|
getCurrentLocaleMessages,
|
||
|
getEnLocaleMessages,
|
||
4 years ago
|
} from '../ducks/locale/locale';
|
||
5 years ago
|
|
||
4 years ago
|
export const I18nContext = createContext((key) => `[${key}]`);
|
||
5 years ago
|
|
||
|
export const I18nProvider = (props) => {
|
||
4 years ago
|
const currentLocale = useSelector(getCurrentLocale);
|
||
|
const current = useSelector(getCurrentLocaleMessages);
|
||
|
const en = useSelector(getEnLocaleMessages);
|
||
5 years ago
|
|
||
|
const t = useMemo(() => {
|
||
4 years ago
|
return (key, ...args) =>
|
||
5 years ago
|
getMessage(currentLocale, current, key, ...args) ||
|
||
4 years ago
|
getMessage(currentLocale, en, key, ...args);
|
||
|
}, [currentLocale, current, en]);
|
||
5 years ago
|
|
||
4 years ago
|
return (
|
||
|
<I18nContext.Provider value={t}>{props.children}</I18nContext.Provider>
|
||
|
);
|
||
|
};
|
||
5 years ago
|
|
||
|
I18nProvider.propTypes = {
|
||
|
children: PropTypes.node,
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
|
I18nProvider.defaultProps = {
|
||
|
children: undefined,
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
|
export class LegacyI18nProvider extends Component {
|
||
|
static propTypes = {
|
||
|
children: PropTypes.node,
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
|
static defaultProps = {
|
||
|
children: undefined,
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
4 years ago
|
static contextType = I18nContext;
|
||
5 years ago
|
|
||
|
static childContextTypes = {
|
||
|
t: PropTypes.func,
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
4 years ago
|
getChildContext() {
|
||
5 years ago
|
return {
|
||
|
t: this.context,
|
||
4 years ago
|
};
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
render() {
|
||
4 years ago
|
return this.props.children;
|
||
5 years ago
|
}
|
||
|
}
|