Last active
November 6, 2025 21:17
-
-
Save umkl/9edcba2ced2b3d0d0e5a7643237d6fc3 to your computer and use it in GitHub Desktop.
loading lingui catalogs server side
This file contains hidden or 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
| import { I18n, Messages, setupI18n } from '@lingui/core'; | |
| import { locales } from './lingui-config'; | |
| import { log } from '@/lib/log'; | |
| export type SupportedLocales = (typeof locales)[number]; | |
| type AllI18nInstances = { [K in SupportedLocales]: I18n }; | |
| async function loadCatalog(locale: SupportedLocales): Promise<{ | |
| [k: string]: Messages; | |
| }> { | |
| const { messages } = await import(`@/locales/${locale}/messages.ts`); | |
| return { | |
| [locale]: messages, | |
| }; | |
| } | |
| const catalogs = await Promise.all(locales.map(loadCatalog)); | |
| const allMessages = catalogs.reduce((acc, oneCatalog) => { | |
| log('Loaded catalog:', JSON.stringify(oneCatalog).substring(0, 100)); | |
| return { ...acc, ...oneCatalog }; | |
| }, {}); | |
| const allI18nInstances: AllI18nInstances = locales.reduce((acc, locale) => { | |
| const messages = allMessages[locale] ?? {}; | |
| const i18n = setupI18n({ | |
| locale, | |
| messages: { [locale]: messages }, | |
| }); | |
| return { ...acc, [locale]: i18n }; | |
| }, {}); | |
| export const getI18nInstance = (locale: SupportedLocales): I18n => { | |
| if (!allI18nInstances[locale]) { | |
| throw new Error(`No Messages available for locale: ${locale}`); | |
| } | |
| return allI18nInstances[locale]; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment