Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoC - Typed Translations #24250

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft

Conversation

Prithpal-Sooriya
Copy link
Contributor

@Prithpal-Sooriya Prithpal-Sooriya commented Apr 25, 2024

Description

Just me having some fun.

This actually works really well! It can inform developers if they are using a translation that does not exist, and if they have missed (or forgot to remove) translation arguments.

but 2 blockers.

  1. We need to generate a TS definitions schema, since importing JSON resolves loosely typed.
    • Very easy, you can run a similar command:
echo -E "declare const schema: $(cat messages.json); export default schema;" > messages.json.d.ts
  1. This does not really help us on JS/JSX files. Sadly no TS linting on JavaScript, unless our JS files include a // @ts-check

Open in GitHub Codespaces

Related issues

Fixes: N/A (just improving types & experimenting)

Manual testing steps

N/A

Screenshots/Recordings

Some TS Error Examples 😄

Screenshot 2024-04-25 at 18 34 24

Screenshot 2024-04-25 at 18 32 37

Screenshot 2024-04-25 at 18 33 42

Screenshot 2024-04-25 at 18 33 26

Before

After

Pre-merge author checklist

  • I’ve followed MetaMask Coding Standards.
  • I've completed the PR template to the best of my ability
  • I’ve included tests if applicable
  • I’ve documented my code using JSDoc format if applicable
  • I’ve applied the right labels on the PR (see labeling guidelines). Not required for external contributors.

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

Copy link
Contributor

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

Comment on lines +55 to +58
// t('holdToRevealContentPrivateKey1');
// t('holdToRevealContentPrivateKey2');
// t('holdToRevealContent1');
// t('holdToRevealContent2');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The yarn verify-locales still works if we just add these as comments - don't need to actually call the translations.

Comment on lines +99 to 102
const messageKey = `nameProvider_${
sourceId as 'ens' | 'etherscan' | 'lens' | 'token'
}` as const;
return t(messageKey);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a quirk due to the translations being a bit more strict - it doesn't accept any arbitrary string.
Meh we can loosen it or have a separate type definition.

In fact could slowly migrate from a loose function definition to a strict function definition if we wanted to.

Comment on lines +268 to +270
mergedAccountsProps.isAccountActive
? Boolean(t('active'))
: Boolean(null)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol, this isActive expects a boolean, but we were passing it a string | null

@@ -101,7 +101,7 @@ const getDisplayValues = ({
} else if (isSmartTransactionCancelled) {
return {
title: t('smartTransactionCancelled'),
description: t('smartTransactionCancelledDescription', [countdown]),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect example! This translation doesn't need an argument!

Thankfully from the files I could test (only typescript files), there weren't any issues where we were using translations but forgetting to pass args.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant