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

i18n: Fix translatable title strings rendering on purchases sections #49733

Merged
merged 3 commits into from
Feb 10, 2021

Conversation

yuliyan
Copy link
Member

@yuliyan yuliyan commented Feb 4, 2021

Changes proposed in this Pull Request

  • Define /me/purchases titles with getters that would fire translate function every time they're being accessed, so that titles would update properly for async loaded translations.
  • Remove setTitle action dispatcher and use <DocumentHead /> instead for setting the title
  • Wrap components that use the shared titles in localize hoc, to ensure component will be re-rendered and call the translate functions after adding translation asynchronously.

Testing instructions

  • Change UI language no any non-English Mag-16
  • Go to /me/purchases?flags=use-translation-chunks
  • Confirm navigation items are translated in the selected language

Merge instructions

Related to p1612455006017200-slack-C74C3THK7

@yuliyan yuliyan requested a review from a8ci18n February 4, 2021 23:01
@yuliyan yuliyan requested a review from a team as a code owner February 4, 2021 23:01
@matticbot
Copy link
Contributor

@matticbot matticbot added [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. OSS Citizen labels Feb 4, 2021
@matticbot
Copy link
Contributor

matticbot commented Feb 4, 2021

Here is how your PR affects size of JS and CSS bundles shipped to the user's browser:

Sections (~157 bytes added 📈 [gzipped])

name            parsed_size           gzip_size
purchases           +1429 B  (+0.1%)     +157 B  (+0.0%)
site-purchases       +867 B  (+0.1%)     +131 B  (+0.0%)

Sections contain code specific for a given set of routes. Is downloaded and parsed only when a particular route is navigated to.

Legend

What is parsed and gzip size?

Parsed Size: Uncompressed size of the JS and CSS files. This much code needs to be parsed and stored in memory.
Gzip Size: Compressed size of the JS and CSS files. This much data needs to be downloaded over network.

Generated by performance advisor bot at iscalypsofastyet.com.

@yuliyan yuliyan force-pushed the fix/translatable-purchases-titles branch 2 times, most recently from 4c663d3 to 311394c Compare February 5, 2021 12:30
get: () => i18n.translate( 'Add Credit Card' ),
},
managePurchase: {
get: () => i18n.translate( 'Purchase Settings' ),
Copy link
Contributor

Choose a reason for hiding this comment

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

This appears to be untranslated when going to details from 'Active Upgrades' section.

Copy link
Member

@sirbrillig sirbrillig left a comment

Choose a reason for hiding this comment

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

Yeah, there seems to be some inconsistency here.

If I load /me/purchases with the flag, then go to add a card, it seems to be correct.

Screen Shot 2021-02-05 at 12 04 02 PM

But then as @dlind1 mentioned, if I click on a purchase from the list, the subtitle is not translated:

Screen Shot 2021-02-05 at 12 04 32 PM

Here's how it looks (translated) if I reload the purchase page:

Screen Shot 2021-02-05 at 12 04 47 PM

I'm not 100% sure how flags persist, but if I load the purchase page with that flag explicitly in the URL, neither the main title nor the subtitle is translated:

Screen Shot 2021-02-05 at 12 05 15 PM

@yuliyan
Copy link
Member Author

yuliyan commented Feb 5, 2021

Thank you for catching this. Turns out purchases controller is not subscribed to i18n changes and therefore the components are not re-rendering after loading translations asynchronously past their initial render.
I'll see how can this be resolved.

@yuliyan yuliyan added [Status] In Progress [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. and removed [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. OSS Citizen [Status] In Progress labels Feb 5, 2021
@yuliyan yuliyan changed the title i18n: Define translatable purchases properties with getters i18n: Fix translatable title strings rendering Feb 8, 2021
@yuliyan
Copy link
Member Author

yuliyan commented Feb 8, 2021

I've replaced the setTitle with <DocumentHead /> and wrapped all components that use the shared titles with localize to ensure components will re-render after loading the translations.

Could you do another round of testing and see if that resolves the problem?

Copy link
Member

@sirbrillig sirbrillig left a comment

Choose a reason for hiding this comment

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

This works as described. Here's the screenshots for the cases I had tried before that were untranslated:

Screen Shot 2021-02-08 at 7 32 48 PM

Screen Shot 2021-02-08 at 7 33 12 PM

I did have one question about what side effects localize() has, but I'll approve this anyway.

@@ -40,4 +41,4 @@ ConfirmCancelDomainLoadingPlaceholder.propTypes = {
selectedSite: PropTypes.oneOfType( [ PropTypes.bool, PropTypes.object ] ),
};

export default ConfirmCancelDomainLoadingPlaceholder;
export default localize( ConfirmCancelDomainLoadingPlaceholder );
Copy link
Member

Choose a reason for hiding this comment

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

I'm curious: what does localize() do here? I thought that it just added a translate prop, but clearly there must be some other reason it's used. Does useTranslate have the same effect?

Copy link
Member Author

@yuliyan yuliyan Feb 9, 2021

Choose a reason for hiding this comment

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

I'm curious: what does localize() do here? I thought that it just added a translate prop, but clearly there must be some other reason it's used.

Yes, it enhances the component with the i18n props, but it also subscribes the component to the i18n's change event and forces the component to re-render. This event is being fired when switching the locale or when adding translations to the existing Tannin locale data, as it is in our case with async loaded translation chunks.

So the problem here was that the component rendered before the translations were available in the locale data, and didn't re-render after translations were added.

Does useTranslate have the same effect?

Yes, it subscribes the component to the same event.

Copy link
Member

Choose a reason for hiding this comment

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

🙇‍♂️ Thank you for the explanation! Does this mean that it's a good idea to always use either the HOC or the hook on top-level components?

Copy link
Member Author

Choose a reason for hiding this comment

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

🙇‍♂️ Thank you for the explanation! Does this mean that it's a good idea to always use either the HOC or the hook on top-level components?

I don't think it matters that much, but using the hook without assigning it to a variable may look a bit strange, imo. Essentially, it should be the same in terms of performance and behaviour.

@yuliyan yuliyan changed the title i18n: Fix translatable title strings rendering i18n: Fix translatable title strings rendering on purchases sections Feb 9, 2021
@yuliyan yuliyan force-pushed the fix/translatable-purchases-titles branch from 400a9d3 to b83e45d Compare February 10, 2021 09:15
@yuliyan yuliyan merged commit 0c67fa0 into trunk Feb 10, 2021
@yuliyan yuliyan deleted the fix/translatable-purchases-titles branch February 10, 2021 09:34
@matticbot matticbot removed the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Feb 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants