add MetaMaskTranslation component#10405
Conversation
Builds ready [14cd336]
Page Load Metrics (576 ± 29 ms)
|
|
i tend to not camelcase metamask in code, so id prefer |
| import Box from '../../ui/box'; | ||
| import MetaMaskTranslation from '../metamask-translation'; | ||
|
|
||
| export const safeComponentList = { |
There was a problem hiding this comment.
what is this list? what does "safe" mean here and how do we use the list
There was a problem hiding this comment.
the word safe in this variable name is sort of an aspirational word. In its current iteration, it acts as the map from the element key in the template language to a real component. It is an attenuation point from 'every HTML element and component in the application' to a small number of components that we want to support in the template system.
In the future, i'd like to extend this implementation to also providing 'allow/deny' lists for props and prop validation. This way we could "safely" allow third parties to add templates with 'a' tags or 'button' where we can isolate the types of actions that can happen on those click handlers.
|
can we get a storybook usage example 😁 |
Absolutely, @kumavis! |
Builds ready [3a859a3]
Page Load Metrics (609 ± 34 ms)
|
|
development/verify-locale-strings.js
Outdated
| }); | ||
| const javascriptFilesToStrictSearch = await glob(globsToStrictSearch); | ||
|
|
||
| const strictSearchRegex = /\bt\('(\w+)'\)|\bt\('(\W+)'\)|\btranslationKey:\s'(\w+)'|\btranslationKey:\s'(\W+)'/gu; |
There was a problem hiding this comment.
Phew, OK, having trouble dissecting this one.
/\bt\('(\w+)'\)/^ That makes sense. It looks for t('anykey'). Got it.
But this one:
\bt\('(\W+)'\)
No idea what's going on there. Why would we want to match non-word characters in single quotes?
Also, neither of these accounts for this:
t(
'veryLongKeyThatWasPutOnASeparateLine'
)
There was a problem hiding this comment.
I removed the \W checks and added a 0 or more whitespace selector before and after the string to catch cases where the call is broke up onto multiple lines. For the object notation selector i added a 0 or more whitespace selector after the colon to allow for multiline checks there as well.
ui/app/components/app/metamask-translation/metamask-translation.js
Outdated
Show resolved
Hide resolved
ui/app/components/app/metamask-translation/metamask-translation.js
Outdated
Show resolved
Hide resolved
| * 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 |
There was a problem hiding this comment.
We might already have a case that doesn't meet these criteria: a link with a tooltip. The tooltip wrapper would be the first component, then the link would be the child, then the link text would be the child of the link.
Maybe the template components won't work like that, I don't know. I guess we'll see, and we can always adjust this later if we need to.
We'll need to be careful not to let this become a perverse incentive, and design weird components like LinkWithTooltip to get around this.
ui/app/components/app/metamask-translation/metamask-translation.js
Outdated
Show resolved
Hide resolved
…n.js Co-authored-by: Mark Stacey <markjstacey@gmail.com>
…n.js Co-authored-by: Mark Stacey <markjstacey@gmail.com>
…etaMask/metamask-extension into metamask-translation-component
Builds ready [c640481]
Page Load Metrics (647 ± 39 ms)
|
Gudahtt
left a comment
There was a problem hiding this comment.
LGTM, great work! The strategy for gradually phasing in strict validation for the string-literal translation keys is a fantastic idea.

Rationale
The template methods of the future
addEthereumChainconfirmation UI are passed in thetfunction for i18n strings. The problem lies in special cases wheretrequires JSX replacements, such as embedding a link in a string of text. To do this in normal situations you'd simply call:This could be done in the current setup, however, the result would require React to be in scope with the template files which I was trying to avoid. It'd also end up embedding the resulting string with jsx into the template object which seems like it could be prone to memory leaks.
This solution adds a new MetaMaskTranslation component that can render the MetaMask template objects. Additional checks are performed to ensure a maximum depth.
To provide a path forward for requiring
tto only be called with string literals, I have proposed some changes to the verify-locale script that would check for eithert('$1')ortranslationKey: '$1'when determining if keys had been used. It also provides a mechanism for gradually switching verify-locales over to use the more strict search.