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

feat: Provide loader function for dynamically imported i18n messages #1369

Merged
merged 4 commits into from
Aug 1, 2023

Conversation

avinashbot
Copy link
Member

@avinashbot avinashbot commented Jul 25, 2023

This PR adds an importMessages export to /i18n. All users have to do (on setups that work with dynamic imports) is:

import { I18nProvider, I18nProviderProps, importMessages } from "@cloudscape-design/components/i18n";

const locale: string = getLocaleSomehow();
const messages: ReadonlyArray<I18nProviderProps.Messages> = await importMessages(locale);

ReactDOM.createRoot(rootElement).render(
  <I18nProvider locale={locale} messages={messages}>
    <App />
  </I18nProvider>
);

importMessages returns an array if we ever need to give it additional functionality in the future, but I'm not super sure about it. But you can pass this array directly into the component (which expects an array anyway). The alternate option of statically importing strings as JS/JSON still exists for people who only need one locale or don't, uh, particularly care about bundle size.

Description

Bundlers are fun. Dynamic imports are much better supported than they were, say, a year ago, but they're still janky, especially across node_modules. So while we recommend import(`.../i18n/messages/all.${locale}.js`) in our docs, bundlers sometimes still freak out over it. That leaves the only option being constructing a big painful switch-case statement with non-interpolated dynamic imports (e.g. case "en": import(`.../i18n/messages/all.en.js`)). Instead, we can just absorb that complexity and provide the function built-in.

Of course, we don't want webpack creating a dozen locale chunks unnecessarily. Thankfully, bundlers won't create language chunks until the importMessages is actually imported. Tested with this with our internal webpack setup, but it works on the same principle as our root index.ts file by simply being a "re-export" file that's easy to tree shake.

Related links, issue #, if available: AWSUI-21586

How has this been tested?

Created an integration test for the dynamic loading story. No unit tests because Jest doesn't do dynamic imports 😅

Review checklist

The following items are to be evaluated by the author(s) and the reviewer(s).

Correctness

  • Changes include appropriate documentation updates.
  • Changes are backward-compatible if not indicated, see CONTRIBUTING.md.
  • Changes do not include unsupported browser features, see CONTRIBUTING.md.
  • Changes were manually tested for accessibility, see accessibility guidelines.

Security

Testing

  • Changes are covered with new/existing unit tests?
  • Changes are covered with new/existing integration tests?

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@codecov
Copy link

codecov bot commented Jul 25, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (7a9c0b7) 93.48% compared to head (1dac34a) 93.48%.
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1369   +/-   ##
=======================================
  Coverage   93.48%   93.48%           
=======================================
  Files         624      625    +1     
  Lines       16796    16796           
  Branches     5558     5558           
=======================================
  Hits        15701    15701           
  Misses       1022     1022           
  Partials       73       73           
Files Changed Coverage Δ
src/i18n/provider.tsx 100.00% <ø> (ø)
src/i18n/get-matchable-locales.ts 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@avinashbot avinashbot marked this pull request as ready for review July 28, 2023 12:24
@avinashbot avinashbot requested a review from a team as a code owner July 28, 2023 12:24
@avinashbot avinashbot requested review from YueyingLu and removed request for a team July 28, 2023 12:24
@avinashbot avinashbot merged commit b4ed143 into main Aug 1, 2023
24 checks passed
@avinashbot avinashbot deleted the i18n-dynamic branch August 1, 2023 09:35
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