Skip to content

Swaps: Show a network name dynamically in a tooltip#10882

Merged
danjm merged 10 commits intoMetaMask:developfrom
dan437:swaps-network-tooltip
Apr 16, 2021
Merged

Swaps: Show a network name dynamically in a tooltip#10882
danjm merged 10 commits intoMetaMask:developfrom
dan437:swaps-network-tooltip

Conversation

@dan437
Copy link
Copy Markdown
Contributor

@dan437 dan437 commented Apr 13, 2021

Explanation

Show a selected network for Swaps dynamically in a tooltip. Only these 3 are supported at the moment:

  • Ethereum
  • BSC
  • Testnet

Manual testing steps

  • Open the MetaMask extension
  • Enter your password
  • Click on the "Swap" button
  • Select "From" and "To" tokens
  • Click on the "Review Swap" button and wait
  • On the page with a quote you can hover your mouse on the tooltip next to "Estimated network fee" and after it opens it shows the network dynamically: "The network fee covers the cost of processing your swap and storing it on the Ethereum network." It can either be "Testnet", "Ethereum" or "BSC" at the moment.

Screenshots

Showing "Testnet network" when using LOCALHOST_CHAIN_ID:
image

Showing "Ethereum network" by default if there is no localized network name:
image

@dan437 dan437 requested a review from a team as a code owner April 13, 2021 21:55
@dan437 dan437 requested a review from Gudahtt April 13, 2021 21:55
@github-actions
Copy link
Copy Markdown
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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Note that this will break for any translations that don't include a $1. I'd suggest going through each translation to either replace Ethereum with $1, or if that seems difficult then deleting this string so that we can ask for a new translation.

Copy link
Copy Markdown
Contributor Author

@dan437 dan437 Apr 13, 2021

Choose a reason for hiding this comment

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

I've talked with @danjm and he mentioned I should only care about en translations.

I've actually just tested what you mentioned and it should work just fine for other languages without $1. I just removed the $1 from the English translation and it shows whatever text is there right now (see below on the screenshot). That means that other languages will show Ethereum until their strings contain $1, which should be done by our localization team. If you can let me know what's the process of engaging our localization team to get it updated for other languages, that would be great!

image

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That will flood sentry with errors. Our localization system expects each message to have the same number of substitutions across all locales

Copy link
Copy Markdown
Contributor Author

@dan437 dan437 Apr 14, 2021

Choose a reason for hiding this comment

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

Good to know. I've tried to replace all Ethereum occurrences with $1, but I don't see Ethereum in the ko/messages.json file:
image

So, either it's not there or it's there, but I don't know where. If you give me a contact to someone from our localization team, I can check with them.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@dan437 Sorry for misleading you. When adding new translations, we only need to worry about en translations.When updating existing translations, if there is an obvious change to make to non-english translations, we can do that manually. If not, it is usually best to add a translation as new.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

According to google translate, 이더 리움 is korean for ethereum

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

which appears to be the fifth set of characters from the left

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This does hint that localizing the actual network names is something we should do as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's interesting, thanks, Dan and Mark! I've replaced 이더리움 with $1 and will make sure to localize network names as well.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

These strings should be localized. "Test" probably isn't a very good name in all locales for example.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I believe that network names should be the same across all languages. I like to think of it as names of companies.. we also don't translate them. 'Test' is a special one just for testing purposes and I'm open to changing it if there is something better. I thought that devs are used to hear Test Net(work), so that's why I chose "Test".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've changed it to "Testnet", because I believe that some translations don't use the word "network" after our dynamic value, so it would just say something like: "The network fee covers the cost of processing your swap and storing it on the Test".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

From the official Jest site: "The snapshot artifact should be committed alongside code changes, and reviewed as part of your code review process."

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I will be adding many more unit tests for the Swaps feature in upcoming PRs.

danjm
danjm previously approved these changes Apr 14, 2021
Copy link
Copy Markdown
Contributor

@danjm danjm left a comment

Choose a reason for hiding this comment

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

This looks good to me!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The translation system already defaults to using the en translation - this fallback is redundant.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Awesome, will remove it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It might be worth adding 이더리움 as a translation for networkNameEthereum so we don't have a regression here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Copy Markdown
Member

@Gudahtt Gudahtt Apr 14, 2021

Choose a reason for hiding this comment

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

Nit: In general we should avoid passing variables to t for the first parameter, because our localized string linting script then has a more difficult time discovering whether the string is used or not. There is a 'strict mode' that we've been slowly introducing one file at a time that assumes t is passed a string literal, so any use of a variable will make that task more difficult.

The alternative I'd suggest is deleting SWAPS_CHAIN_ID_TO_NETWORK_NAME_KEY_MAP, and replacing it with a function that accepts t and the chain ID, and returns the correct network name.

Edit: Updated to be a bit more clear

Copy link
Copy Markdown
Contributor

@danjm danjm Apr 14, 2021

Choose a reason for hiding this comment

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

In general we should avoid passing variables to t, because our localized string linting script then has a more difficult time discovering whether the string is used or not

This is a good point, I always forget this.

The alternative I'd suggest is deleting SWAPS_CHAIN_ID_TO_NETWORK_NAME_KEY_MAP, and replacing it with a function that accepts t and the chain ID, and returns the correct network name.

Wouldn't that still result in a variable passed to t?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Wouldn't that still result in a variable passed to t?

Ah, no, I see. The function would map chainId to calls to t using string literals

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

So, there is https://github.com/i18next/react-i18next , which is one of the best internationalization frameworks in the world for React / React Native. Interpolation (passing dynamic values into translations) is one of the most used functionalities there: https://www.i18next.com/translation-function/interpolation

I believe we should support passing dynamic values into translations, since it's a very common thing to do. With i18next it's done like this:

{
    "key": "{{what}} is {{how}}"
}
i18next.t('key', { what: 'i18next', how: 'great' });

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We're not talking about interpolation here. Our translation helper does support interpolation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We've just clarified it over Slack - the issue is not about passing variables in the second param to t, but in the first one. Will create a function as suggested.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe we should throw here instead of defaulting to 'networkNameEthereum'? 🤔 This should be unreachable right now, and in the future could only be hit if we add support for a fourth network in swaps but forget to add a network name. That scenario would be worth throwing an error for.

Unless there's some case I'm missing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It sounds fine to me. What about something like this?

if (!networkNameKey) {
  throw new Error('This network is not supported for token swaps');
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That works!

danjm
danjm previously approved these changes Apr 16, 2021
@danjm danjm merged commit 1b4bc46 into MetaMask:develop Apr 16, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Apr 16, 2021
@dan437 dan437 deleted the swaps-network-tooltip branch July 24, 2023 11:28
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants