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

Fix language switching and update date-fns to v4 #2788

Merged
merged 6 commits into from
Sep 27, 2024

Conversation

lindboe
Copy link
Contributor

@lindboe lindboe commented Sep 26, 2024

Please verify the following:

  • yarn test jest tests pass with new tests, if relevant
  • yarn lint eslint checks pass with new code, if relevant
  • yarn format:check prettier checks pass with new code, if relevant
  • README.md (or relevant documentation) has been updated with your changes

Describe your PR

While updating date-fns to v4, I ran into a few issues with language switching.

Languages were not being loaded correctly

The previous way we had of loading languages with i18n-js doesn't work with i18next. hasOwnProperty does not work with i18n.languages, as it's an array. i18n.languages is also empty on init, as it's only an array of the currently configured languages to use (preferred, then fallback), not all supported languages.

Instead I created a function to pick the first locale from device locales that we support, and configure i18n with that.

Fast refresh would crash the app

This we fix by referring to i18n from the library, we don't need to store it in a variable in i18n.ts.

Updates for date-fns v4

Here I just needed to update some import paths, but while I was there, I also added support for more of our supported locales, and conditionally load only the locale actually in use.

Future improvements

I recommend we decide whether and how we want to follow up on these items later:

  1. A better Episode test. It's not clear what this test wants to assert. If it wants to assert that the models are accounting for params correctly, we may have to find a way to manually test that, probably spying on t(). If it wants to assert that we're translating accessibility labels correctly, we should make it an E2E test. Etc.
  2. Live reloading of locale while app is open on Android. Theoretically possible, but wasn't straightforward when I tried, particularly because we're using the translate function everywhere which doesn't know to react when language is changed (unlike if we were using the useTranslation hook.)
  3. More foolproof toggling of RTL settings (i.e., always verifying if RTL setting matches what we expect, and if not, restart the app).
  4. The drawer isn't rendering correctly when RTL is set, but it was also not rendering correctly with the i18n-js config. We need to circle back to this.
Platform This PR With i18n-js
iOS Device don't have
iOS Sim don't have
Android device

- `hasOwnProperty` does not work with i18n.languages, as it's an array.
  It is also empty on init.
- Create function to find first supported locale among devices locales.
  (I experimented with passing multiple languages to i18next with a
  language detector, but its fallback language is not ideal and will just
  fall back to the global `fallbackLng` instead of trying the second
  option in the list.)
- Do a little more to ensure we only set RTL to true if the locale we
  end up picking is RTL, although it's still not perfectly guaranteed
  here, as i18next could fall back to `en` for some reason. The problem
  is that we either need to set RTL immediately, or restart the app if RTL
  setting needs to change.
- Fix imports
- Conditionally require locales
- Add more locale support
Copy link
Collaborator

@fpena fpena left a comment

Choose a reason for hiding this comment

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

Looks good overall. Left 2 comments.

@@ -27,14 +27,14 @@ const episode = EpisodeModel.create(data)
test("publish date format", () => {
expect(episode.datePublished.textLabel).toBe("Jan 20, 2022")
expect(episode.datePublished.accessibilityLabel).toBe(
'demoPodcastListScreen:accessibility.publishLabel {"date":"Jan 20, 2022"}',
"demoPodcastListScreen:accessibility.publishLabel",
Copy link
Collaborator

@fpena fpena Sep 26, 2024

Choose a reason for hiding this comment

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

Can you explain please why are we removing the date? Reading from the PR description, it seems like we still want to improve this test later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't really understand why this changed, but I also didn't want to block this on this test, because the behavior is correct when I actually test it in the app.

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 single quotes changing to double quotes was required by the linter, and I think might be related to tests having a separate TS config? but not sure, I thought that would be handled by prettier.

Actually I'm even more confused by this because apparently it failed on the CLI project's prettier check (https://app.circleci.com/pipelines/github/infinitered/ignite/3631/workflows/6e7e16e3-e4c1-4a6b-949b-d1fe26da1665/jobs/4375), but the inner project isn't complaining about the double-quotes.

That config just isn't unified, so we get weird things like this sometimes.

Copy link
Collaborator

Choose a reason for hiding this comment

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

My question was not related to quotes but related to deleting the date within it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, for that part see my first comment. I'm not sure why it changed, but I'd also like to not block this PR on that since the logic is working correctly on the real app, this is just a test that I'm not sure is testing anything useful the way it's currently set up.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Filed issue for this: #2789

Comment on lines +25 to +28
const systemTagMatchesSupportedTags = (deviceTag: string) => {
const primaryTag = deviceTag.split("-")[0]
return supportedTags.includes(primaryTag)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

What it be worth writing a test for this one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was thinking splitting on - and checking includes were simple enough it didn't need a test, if we did something more complex maybe then?

Most of the complexity here comes in with third party systems, so an E2E test to check that we can switch languages makes the most sense to me.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, I agree this is not complex, but I'm also thinking of future Felipe doing an i18 migration again. It's all good; not a blocker.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For sure. I still think in that case what is most useful is an E2E test instead of unit tests.

@lindboe lindboe marked this pull request as ready for review September 26, 2024 22:18
)
})

test("duration format", () => {
expect(episode.duration.textLabel).toBe("42:58")
expect(episode.duration.accessibilityLabel).toBe(
'demoPodcastListScreen:accessibility.durationLabel {"hours":0,"minutes":42,"seconds":58}',
"demoPodcastListScreen:accessibility.durationLabel",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same as here.

Copy link
Contributor

@mazenchami mazenchami 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, great work!

Copy link
Collaborator

@fpena fpena left a comment

Choose a reason for hiding this comment

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

Looks good, thanks Lizzi!

@lindboe lindboe merged commit 31428b7 into v10 Sep 27, 2024
1 check passed
@lindboe lindboe deleted the lindboe/fix-language-switching branch September 27, 2024 22:35
@infinitered-circleci
Copy link

🎉 This PR is included in version 10.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

4 participants