2021-02-12 18:24:50 +01:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { useI18nContext } from '../../../hooks/useI18nContext';
|
|
|
|
import MetaMaskTemplateRenderer, {
|
|
|
|
SectionShape,
|
|
|
|
} from '../metamask-template-renderer/metamask-template-renderer';
|
|
|
|
|
|
|
|
/**
|
2021-12-07 23:29:12 +01:00
|
|
|
* MetaMaskTranslation is a simple helper component for adding full translation
|
2021-02-12 18:24:50 +01:00
|
|
|
* support to the template system. We do pass the translation function to the
|
|
|
|
* template getValues function, but passing it React components as variables
|
|
|
|
* would require React to be in scope, and breaks the object pattern paradigm.
|
|
|
|
*
|
|
|
|
* This component gets around that by converting variables that are templates
|
|
|
|
* themselves into tiny React trees. This component does additional validation
|
|
|
|
* to make sure that the tree has a single root node, with maximum two leaves.
|
|
|
|
* Each subnode can have a maximum of one child that must be a string.
|
|
|
|
*
|
|
|
|
* This enforces a maximum recursion depth of 2, preventing translation strings
|
|
|
|
* from being performance hogs. We could further limit this, and also attenuate
|
|
|
|
* the safeComponentList for what kind of components we allow these special
|
|
|
|
* trees to contain.
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
|
|
|
* @param options
|
|
|
|
* @param options.translationKey
|
|
|
|
* @param options.variables
|
2021-02-12 18:24:50 +01:00
|
|
|
*/
|
|
|
|
export default function MetaMaskTranslation({ translationKey, variables }) {
|
|
|
|
const t = useI18nContext();
|
|
|
|
|
|
|
|
return t(
|
|
|
|
translationKey,
|
|
|
|
variables?.map((variable) => {
|
|
|
|
if (
|
|
|
|
typeof variable === 'object' &&
|
|
|
|
!Array.isArray(variable) &&
|
|
|
|
variable.element
|
|
|
|
) {
|
|
|
|
if (!variable.key) {
|
|
|
|
throw new Error(
|
|
|
|
`When using MetaMask Template Language in a MetaMaskTranslation variable, you must provide a key for the section regardless of syntax.
|
|
|
|
Section with element '${variable.element}' for translationKey: '${translationKey}' has no key property`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
variable.children &&
|
|
|
|
Array.isArray(variable.children) &&
|
|
|
|
variable.children.length > 2
|
|
|
|
) {
|
|
|
|
throw new Error(
|
|
|
|
'MetaMaskTranslation only renders templates with a single section and maximum two children',
|
|
|
|
);
|
|
|
|
} else if (
|
|
|
|
(variable.children?.[0]?.children !== undefined &&
|
|
|
|
typeof variable.children[0].children !== 'string') ||
|
|
|
|
(variable.children?.[1]?.children !== undefined &&
|
|
|
|
typeof variable.children[1].children !== 'string')
|
|
|
|
) {
|
|
|
|
throw new Error(
|
|
|
|
'MetaMaskTranslation does not allow for component trees of non trivial depth',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<MetaMaskTemplateRenderer
|
|
|
|
key={`${translationKey}-${variable.key}`}
|
|
|
|
sections={variable}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return variable;
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
MetaMaskTranslation.propTypes = {
|
2021-12-07 23:29:12 +01:00
|
|
|
/**
|
|
|
|
* Translation object key
|
|
|
|
*/
|
2021-02-12 18:24:50 +01:00
|
|
|
translationKey: PropTypes.string.isRequired,
|
2021-12-07 23:29:12 +01:00
|
|
|
/**
|
|
|
|
* Array of variables for the MetaMaskTranslation component
|
|
|
|
*/
|
2021-02-12 18:24:50 +01:00
|
|
|
variables: PropTypes.arrayOf(
|
|
|
|
PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.number,
|
|
|
|
PropTypes.shape(SectionShape),
|
|
|
|
]),
|
|
|
|
),
|
|
|
|
};
|