Skip to content

Upgrade @fluent/react, @fluent/bundle#2631

Merged
phirework merged 3 commits intocommon-voice:masterfrom
stasm:upgrade-fluent-react
May 27, 2020
Merged

Upgrade @fluent/react, @fluent/bundle#2631
phirework merged 3 commits intocommon-voice:masterfrom
stasm:upgrade-fluent-react

Conversation

@stasm
Copy link
Copy Markdown
Contributor

@stasm stasm commented Mar 20, 2020

In projectfluent/fluent.js#458 I've been working on a new version of @fluent/react (né fluent-react), rewritten in TypeScript. The rewrite required some API changes, and I used voice-web as a test project, given it's a large React app also written in TypeScript.

I'm opening this draft PR to share my changes so far. Once the new version of @fluent/react is released, I'll be happy to finish working on it.

LocalePropsFromState,
} from './locale-helpers';
import { Flags } from '../stores/flags';
import { LocalizationProvider } from '@fluent/react';
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 dropped the compat build here and everywhere else in voice-web/web. I don't think it's actually needed, and it looks like your build system is capable of transpiling node_modules just fine?

The main reason why I'd prefer not to use compat is that its not currently typed automatically by @fluent/react's build pipeline. I.e. .d.ts files are only produced for the non-compat build.

discourseLink: <DiscourseLink />,
githubLink: <GitHubLink />,
matrixLink: <MatrixLink />
}}>
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.

The new API is more explicit about the elements passed into translations.

id="help-reach-hours"
$hours={10000}
$language={getString(locale)}>
vars={{hours: 10000, language: getString(locale)}}>
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.

The new API is more explicit about the variables passed into translations as arguments.

for (const [locale, messages] of localeMessages) {
const bundle = new FluentBundle(locale, { useIsolating: false });
bundle.addMessages(
bundle.addResource(new FluentResource(
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.

This is the FluentBundle API since version 0.14.

@phirework
Copy link
Copy Markdown
Contributor

This is awesome, thanks Stas!

@stasm stasm force-pushed the upgrade-fluent-react branch from 065b9e0 to 31f7187 Compare March 30, 2020 16:27
@stasm stasm marked this pull request as ready for review April 7, 2020 16:05
@stasm
Copy link
Copy Markdown
Contributor Author

stasm commented Apr 7, 2020

I think this is now ready for a review. I tested this locally after merging master into the branch (I didn't include the merge in the PR), and things looked good. I don't know the whole app, however, so I'll be happy to do more manual testing if you'd like me to.

@stasm stasm changed the title Upgrade to @fluent/react, @fluent/bundle Upgrade @fluent/react, @fluent/bundle Apr 16, 2020
@stasm
Copy link
Copy Markdown
Contributor Author

stasm commented Apr 16, 2020

@phirework Can you help me find a reviewer for this, please?

@phirework
Copy link
Copy Markdown
Contributor

@stasm Apologies for the delay, I'll take a more detailed look today or tomorrow. We've been busy with our migration to Kubernetes and I didn't want to merge in a big change before also changing server environments.

@phirework phirework self-requested a review April 16, 2020 15:44
@stasm
Copy link
Copy Markdown
Contributor Author

stasm commented Apr 16, 2020

Of course, no worries! I just wanted to make sure this is on your radar.

I've also just checked locally, and it looks like there haven't been any new <Localized> added since I created this PR, so it should be good to merge. More than bitrot, I was worried that there would be new code added on master which would use the old Localized API, and which would go unnoticed during the merge.

@phirework
Copy link
Copy Markdown
Contributor

Sorry for the late update! So I've finally had a chance to look through everything in more detail. Thanks again for doing this work.

So the big piece blocking this is that not all of the places we make use of the <Localized> object directly references the element, instead it's being passed around in props. You can see missing variable warnings on /speak, /languages, /datasets, etc. So for example, in contribute.tsx, these changes need to be made for it to be passed down to speak.tsx and listen.tsx:

@@ -91,7 +91,7 @@ interface Props extends WithLocalizationProps, PropsFromState {
   errorContent?: any;
   reportModalProps: Omit<ReportModalProps, 'onSubmitted'>;
   instruction: (props: {
-    $actionType: string;
+    vars: { actionType: string; }
     children: any;
   }) => React.ReactNode;
   isFirstSubmit?: boolean;

@@ -423,7 +423,7 @@ class ContributionPage extends React.Component<Props, State> {

               <div className="cards-and-instruction">
                 {instruction({
-                  $actionType: getString('action-click'),
+                  vars: { actionType: getString('action-click') },
                   children: <div className="instruction hidden-sm-down" />,
                 }) || <div className="instruction hidden-sm-down" />}

@@ -500,7 +500,7 @@ class ContributionPage extends React.Component<Props, State> {
             </div>

             {instruction({
-              $actionType: getString('action-tap'),
+              vars: { actionType: getString('action-tap') },
               children: <div className="instruction hidden-md-up" />,
             }) || <div className="instruction hidden-md-up" />}

Off the top of my head, the most fail-safe way to look for all of these would be to start with web/locales/en/messages.ftl and check all the translation strings that make use of elements or variables. That sounds very tedious and boring and I don't know if you would consider that in-scope for the work you're doing. Is this pull request a blocker to you all releasing a new version on your side?

@stasm
Copy link
Copy Markdown
Contributor Author

stasm commented Apr 27, 2020

It should be fairly easy for me to collect the names of variables and elements used in all messages, and then grep for them in the code. I didn't realize they were passed like this in objects, thanks for pointing it out.

I'm not blocked on this PR, thanks for asking. I already released @fluent/react 0.12 some time ago. I'd like to help to get this PR landed so that it doesn't bitrot completely, making it hard to upgrade to new versions of @fluent/react in the future.

@stasm stasm force-pushed the upgrade-fluent-react branch from 222c1c2 to a2a4f52 Compare May 7, 2020 10:00
@stasm stasm force-pushed the upgrade-fluent-react branch from a2a4f52 to 3217a40 Compare May 7, 2020 12:30
@stasm
Copy link
Copy Markdown
Contributor Author

stasm commented May 7, 2020

I updated the PR to fix the cases where the props to Localized are passed around as objects. I think I got them all. I first queried the localization files in web/locales/en to look for all variables and markup used in the translations. I then grepped the source in web/src and reviewed each occurence.

One tricky piece of code was this one:

https://github.com/mozilla/voice-web/blob/fd9d7c7c5bf45b438551c71c6ecb6ddeb8cf4a69/web/src/components/pages/datasets/dataset-info.tsx#L332-L338

It kind of demonstrates why the old API wasn't ergnomic. In the new API, the above becomes simply:

<Localized
    id="dataset-description-hours"
    vars={globalStats}
    elems={{b: <b />}}
>

I also found two messages which may need some work:

@phirework Would you mind taking another look at this?

@phirework phirework merged commit 4b5f9c0 into common-voice:master May 27, 2020
@phirework
Copy link
Copy Markdown
Contributor

This is awesome, thank you! I will take care of cleaning those two instances up separately, good catch. Super appreciate all your work here.

@stasm
Copy link
Copy Markdown
Contributor Author

stasm commented May 28, 2020

Thanks, @phirework!

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.

2 participants