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

Dynamically load locale messages #261

Merged
merged 5 commits into from
Dec 7, 2023
Merged

Conversation

sawyerh
Copy link
Contributor

@sawyerh sawyerh commented Dec 7, 2023

Ticket

#66

Changes

  • Use a dynamic import to load all strings for the selected locale. This simplifies the process of adding a new language — now someone just needs to add the locale code (e.g. es-US) to the locales array, whereas before they'd also have to add an import statement.
  • Renamed getLocaleMessages to getMessagesWithFallbacks 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 rename i18n/index.ts to i18n/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:

├── i18n
│   ├── config.ts     # Supported locales and formatting options
│   ├── getMessagesWithFallbacks.ts 
│   └── server.ts     # 🆕 next-intl setup logic that can't be imported into client components

@lorenyu We could expand this also to export the safelisted subset of hooks and components

├── i18n
│   ├── config.ts     # Supported locales and formatting options
│   ├── getMessagesWithFallbacks.ts 
│   ├── index.ts      # 🆕 Hooks and components for rendering content
│   └── server.ts     # next-intl setup logic that can't be imported into client components

Testing

CleanShot 2023-12-06 at 16 54 15@2x

CleanShot 2023-12-06 at 16 53 47

@sawyerh sawyerh marked this pull request as ready for review December 7, 2023 01:05
@sawyerh sawyerh requested review from lorenyu and aligg December 7, 2023 01:05
@@ -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;
Copy link
Contributor Author

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

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 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],
Copy link
Contributor Author

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;
Copy link
Contributor

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?

Copy link
Contributor Author

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.

@sawyerh sawyerh merged commit 526ef47 into main Dec 7, 2023
6 checks passed
@sawyerh sawyerh deleted the sawyerh/66-dynamic-i18n-import branch December 7, 2023 19:15
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