-
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { merge } from "lodash"; | ||
import { defaultLocale, Locale, locales } from "src/i18n"; | ||
|
||
interface LocaleFile { | ||
messages: Messages; | ||
} | ||
|
||
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more.
Yep, this is the main part of the show. This is the dynamic import.
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. |
||
return messages; | ||
} | ||
|
||
/** | ||
* Get all messages for the given locale. If any translations are missing | ||
* from the current locale, the missing key will fallback to the default locale | ||
*/ | ||
export async function getMessagesWithFallbacks( | ||
requestedLocale: string = defaultLocale | ||
) { | ||
const isValidLocale = locales.includes(requestedLocale as Locale); // https://github.com/microsoft/TypeScript/issues/26255 | ||
if (!isValidLocale) { | ||
console.error( | ||
"Unsupported locale was requested. Falling back to the default locale.", | ||
{ locale: requestedLocale, defaultLocale } | ||
); | ||
requestedLocale = defaultLocale; | ||
} | ||
|
||
const targetLocale = requestedLocale as Locale; | ||
let messages = await importMessages(targetLocale); | ||
|
||
if (targetLocale !== defaultLocale) { | ||
const fallbackMessages = await importMessages(defaultLocale); | ||
messages = merge({}, fallbackMessages, messages); | ||
} | ||
|
||
return Promise.resolve(messages); | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I want to rename this file to |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. This was just wrong before |
||
type IntlMessages = 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 puts
messages
oncontext.loaded.messages
, which is available to decorators and stories