-
Notifications
You must be signed in to change notification settings - Fork 155
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
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Codecov ReportPatch coverage:
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
☔ View full report in Codecov by Sentry. |
avinashbot
force-pushed
the
i18n-dynamic
branch
2 times, most recently
from
July 26, 2023 16:34
713bd90
to
d062d90
Compare
YueyingLu
approved these changes
Jul 31, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds an
importMessages
export to/i18n
. All users have to do (on setups that work with dynamic imports) is: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 rootindex.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
CONTRIBUTING.md
.CONTRIBUTING.md
.Security
checkSafeUrl
function.Testing
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.