-
Notifications
You must be signed in to change notification settings - Fork 4
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
Dynamically load locale messages #261
Conversation
@@ -1,3 +1,3 @@ | |||
// Use type safe message keys with `next-intl` | |||
type Messages = typeof import("src/i18n/messages/en-US").default; | |||
type Messages = typeof import("src/i18n/messages/en-US").messages; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was just wrong before
app/src/i18n/index.ts
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to rename this file to config.ts
but it makes for a confusing diff. I'll rename before merging unless anyone objects.
const preview: Preview = { | ||
loaders: [i18nMessagesLoader], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This puts messages
on context.loaded.messages
, which is available to decorators and stories
} | ||
|
||
async function importMessages(locale: Locale) { | ||
const { messages } = (await import(`./messages/${locale}`)) as LocaleFile; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 Is this the part you mentioned in the PR description about using a dynamic import to get all the content strings for the specified locale?
Newb question and probably a better way to ask this but does this have any ramifications for like.. the point in build / run time where content strings are brought in?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the part you mentioned in the PR description about using a dynamic import to get all the content strings for the specified locale?
Yep, this is the main part of the show. This is the dynamic import.
does this have any ramifications for like.. the point in build / run time where content strings are brought in?
My understanding is that the dynamic import happens at runtime. This avoids statically loading a bunch of content strings that aren't needed, which could impact memory usage.
Ticket
#66
Changes
es-US
) to thelocales
array, whereas before they'd also have to add an import statement.getLocaleMessages
togetMessagesWithFallbacks
and moved it to its own file. A few reasons for this:(1) Project engineers shouldn't ever need to interact with the
getMessagesWithFallbacks
logic — it should Just Work. (2) We can now renamei18n/index.ts
toi18n/config.ts
to make it clearer what the purpose of that file is.Context for reviewers
I'm pulling this out from the larger App Router migration branch. There's an additional piece that will be in that branch, resulting in a final directory structure looking something like this:
@lorenyu We could expand this also to export the safelisted subset of hooks and components
Testing