|
1 | 1 | import { defineConfig } from '@lunariajs/core/config' |
2 | | -import { locales, sourceLocale } from './lunaria/prepare-json-files.ts' |
| 2 | +import type { Locale, Merge } from '@lunariajs/core' |
| 3 | +import { currentLocales, countryLocaleVariants } from './config/i18n.ts' |
| 4 | + |
| 5 | +// The source locale is `en` (en.json contains all reference translation keys). |
| 6 | +// Country variants like `en-US` inherit from `en` via the merge config. |
| 7 | +const sourceLocale: Locale = { label: 'English', lang: 'en' } |
| 8 | + |
| 9 | +// Build the list of Lunaria locales from currentLocales. |
| 10 | +// currentLocales has expanded codes (en-US, en-GB, ar-EG, es-ES, es-419, etc.) |
| 11 | +// but NOT the base codes (ar, es) that the variants inherit from. |
| 12 | +// We need to add those base codes as Lunaria locales too, so they can be |
| 13 | +// referenced in the merge config and tracked independently. |
| 14 | +const localeSet = new Set<string>() |
| 15 | +const locales: Locale[] = [] |
| 16 | + |
| 17 | +for (const l of currentLocales) { |
| 18 | + if (l.code === sourceLocale.lang || !l.name) continue |
| 19 | + if (!localeSet.has(l.code)) { |
| 20 | + localeSet.add(l.code) |
| 21 | + locales.push({ label: l.name, lang: l.code }) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +// Add base language codes (ar, es, etc.) that aren't already in the list. |
| 26 | +// These are the keys of countryLocaleVariants that aren't the source locale. |
| 27 | +for (const baseLang of Object.keys(countryLocaleVariants)) { |
| 28 | + if (baseLang === sourceLocale.lang) continue |
| 29 | + if (!localeSet.has(baseLang)) { |
| 30 | + // Use the first variant's name or the base code as label |
| 31 | + const variants = countryLocaleVariants[baseLang]! |
| 32 | + const label = variants[0]?.name ?? baseLang |
| 33 | + localeSet.add(baseLang) |
| 34 | + locales.push({ label, lang: baseLang }) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +if (locales.length === 0) { |
| 39 | + throw new Error('No locales found besides source locale') |
| 40 | +} |
| 41 | + |
| 42 | +// Build merge config from countryLocaleVariants: |
| 43 | +// Each variant locale merges keys from its base locale, so keys present in |
| 44 | +// the base file count as covered for the variant. |
| 45 | +// e.g. { 'en-US': ['en'], 'en-GB': ['en'], 'ar-EG': ['ar'], 'es-ES': ['es'], 'es-419': ['es'] } |
| 46 | +const merge: Merge = {} |
| 47 | +for (const [baseLang, variants] of Object.entries(countryLocaleVariants)) { |
| 48 | + for (const variant of variants) { |
| 49 | + // Each variant merges from its base language and (if not the source) implicitly |
| 50 | + // from the source via normal Lunaria tracking. |
| 51 | + const existing = merge[variant.code] |
| 52 | + if (existing) { |
| 53 | + existing.push(baseLang) |
| 54 | + } else { |
| 55 | + merge[variant.code] = [baseLang] |
| 56 | + } |
| 57 | + } |
| 58 | +} |
3 | 59 |
|
4 | 60 | export default defineConfig({ |
5 | 61 | repository: { |
6 | 62 | name: 'npmx-dev/npmx.dev', |
7 | 63 | }, |
8 | 64 | sourceLocale, |
9 | | - locales, |
| 65 | + locales: locales as [Locale, ...Locale[]], |
10 | 66 | files: [ |
11 | 67 | { |
12 | | - include: ['lunaria/files/en-US.json'], |
13 | | - pattern: 'lunaria/files/@lang.json', |
| 68 | + include: ['i18n/locales/en.json'], |
| 69 | + pattern: 'i18n/locales/@lang.json', |
14 | 70 | type: 'dictionary', |
| 71 | + merge, |
| 72 | + optionalKeys: { |
| 73 | + $schema: true, |
| 74 | + vacations: true, |
| 75 | + }, |
15 | 76 | }, |
16 | 77 | ], |
17 | 78 | tracking: { |
|
0 commit comments