|
1 | 1 | import type { I18nConfig } from 'next-i18next' |
2 | 2 |
|
| 3 | +// In dev, read from disk so `reloadOnPrerender` can pick up edits without a |
| 4 | +// dev-server restart — bundlers cache dynamic `import()` JSON modules across |
| 5 | +// HMR cycles, which would otherwise stall hot-reload after the first edit. |
| 6 | +// In production, use bundler-traceable `import()` so locale files end up in |
| 7 | +// the serverless function bundle (Vercel/Lambda/etc.). |
| 8 | +const resourceLoader: I18nConfig['resourceLoader'] = |
| 9 | + process.env.NODE_ENV === 'development' |
| 10 | + ? async (language, namespace) => { |
| 11 | + const fs = await import('fs/promises') |
| 12 | + const path = await import('path') |
| 13 | + const content = await fs.readFile( |
| 14 | + path.resolve(process.cwd(), `app/i18n/locales/${language}/${namespace}.json`), |
| 15 | + 'utf-8' |
| 16 | + ) |
| 17 | + return JSON.parse(content) |
| 18 | + } |
| 19 | + : (language, namespace) => import(`./app/i18n/locales/${language}/${namespace}.json`) |
| 20 | + |
3 | 21 | const i18nConfig: I18nConfig = { |
4 | 22 | supportedLngs: ['en', 'de', 'it'], |
5 | 23 | fallbackLng: 'en', |
6 | 24 | defaultNS: 'translation', |
7 | 25 | ns: ['translation', 'footer', 'client-page', 'second-page', 'second-client-page'], |
8 | | - resourceLoader: (language: string, namespace: string) => import(`./app/i18n/locales/${language}/${namespace}.json`), |
| 26 | + resourceLoader, |
| 27 | + reloadOnPrerender: process.env.NODE_ENV === 'development', |
9 | 28 | } |
10 | 29 |
|
11 | 30 | export default i18nConfig |
0 commit comments