|
16 | 16 | * that says what it costs. |
17 | 17 | * |
18 | 18 | * ⚠️ Anything on a page-load path belongs on `./registry.js`, not here. |
| 19 | + * |
| 20 | + * ## Each catalogue here is COMPLETE: the pack plus its lazy namespaces |
| 21 | + * |
| 22 | + * Some of a language's strings are not in its pack: the `marketplace.*` group |
| 23 | + * is a lazy namespace (`../namespaces/marketplace/`), fetched when a |
| 24 | + * marketplace page mounts so the console's eager `en` pack does not carry it. |
| 25 | + * This door's two purposes both want every string, so each language it hands |
| 26 | + * out is its pack deep-merged with its catalogue from every such namespace: |
| 27 | + * the parity / drift suites keep walking the moved keys, and an app that makes |
| 28 | + * every catalogue resident gets the marketplace text resident too. A key in |
| 29 | + * both places is refused at module evaluation — the gates refuse it as well. |
| 30 | + * |
| 31 | + * ⚠️ So `en` here is not the same object as the package entry's `en`, which is |
| 32 | + * the RESIDENT pack alone (what an instance holds before any namespace has |
| 33 | + * loaded), nor as `@object-ui/i18n/locales/en`, the pack module itself. |
19 | 34 | */ |
20 | | -export { default as en, type TranslationKeys } from './en.js'; |
21 | | -export { default as zh } from './zh.js'; |
22 | | -export { default as ja } from './ja.js'; |
23 | | -export { default as ko } from './ko.js'; |
24 | | -export { default as de } from './de.js'; |
25 | | -export { default as fr } from './fr.js'; |
26 | | -export { default as es } from './es.js'; |
27 | | -export { default as pt } from './pt.js'; |
28 | | -export { default as ru } from './ru.js'; |
29 | | -export { default as ar } from './ar.js'; |
| 35 | +import enPack from './en.js'; |
| 36 | +import zhPack from './zh.js'; |
| 37 | +import jaPack from './ja.js'; |
| 38 | +import koPack from './ko.js'; |
| 39 | +import dePack from './de.js'; |
| 40 | +import frPack from './fr.js'; |
| 41 | +import esPack from './es.js'; |
| 42 | +import ptPack from './pt.js'; |
| 43 | +import ruPack from './ru.js'; |
| 44 | +import arPack from './ar.js'; |
| 45 | +import enMarketplace from '../namespaces/marketplace/en.js'; |
| 46 | +import zhMarketplace from '../namespaces/marketplace/zh.js'; |
| 47 | +import jaMarketplace from '../namespaces/marketplace/ja.js'; |
| 48 | +import koMarketplace from '../namespaces/marketplace/ko.js'; |
| 49 | +import deMarketplace from '../namespaces/marketplace/de.js'; |
| 50 | +import frMarketplace from '../namespaces/marketplace/fr.js'; |
| 51 | +import esMarketplace from '../namespaces/marketplace/es.js'; |
| 52 | +import ptMarketplace from '../namespaces/marketplace/pt.js'; |
| 53 | +import ruMarketplace from '../namespaces/marketplace/ru.js'; |
| 54 | +import arMarketplace from '../namespaces/marketplace/ar.js'; |
| 55 | + |
| 56 | +export type { TranslationKeys } from './en.js'; |
| 57 | + |
| 58 | +const isPlainObject = (value: unknown): value is Record<string, unknown> => |
| 59 | + typeof value === 'object' && value !== null && !Array.isArray(value); |
30 | 60 |
|
31 | 61 | /** |
32 | | - * Map of all built-in locales keyed by language code |
| 62 | + * `pack` deep-merged with a namespace's partial pack. Groups both sides hold |
| 63 | + * (`marketplace`, `marketplace.install`) merge; a LEAF both sides hold is a |
| 64 | + * key defined twice, which no reader could resolve the same way twice, and |
| 65 | + * throws. |
33 | 66 | */ |
34 | | -import en from './en.js'; |
35 | | -import zh from './zh.js'; |
36 | | -import ja from './ja.js'; |
37 | | -import ko from './ko.js'; |
38 | | -import de from './de.js'; |
39 | | -import fr from './fr.js'; |
40 | | -import es from './es.js'; |
41 | | -import pt from './pt.js'; |
42 | | -import ru from './ru.js'; |
43 | | -import ar from './ar.js'; |
| 67 | +function complete<P extends object, N extends object>(pack: P, partial: N, label: string): P & N { |
| 68 | + const merge = (into: Record<string, unknown>, from: Record<string, unknown>, at: string) => { |
| 69 | + const out: Record<string, unknown> = { ...into }; |
| 70 | + for (const key of Object.keys(from)) { |
| 71 | + if (key === '__proto__') continue; |
| 72 | + const path = at ? `${at}.${key}` : key; |
| 73 | + if (isPlainObject(out[key]) && isPlainObject(from[key])) { |
| 74 | + out[key] = merge(out[key] as Record<string, unknown>, from[key] as Record<string, unknown>, path); |
| 75 | + } else if (Object.prototype.hasOwnProperty.call(out, key)) { |
| 76 | + throw new Error(`${label}: \`${path}\` is defined both in the pack and in a namespace catalogue.`); |
| 77 | + } else { |
| 78 | + out[key] = from[key]; |
| 79 | + } |
| 80 | + } |
| 81 | + return out; |
| 82 | + }; |
| 83 | + return merge(pack as Record<string, unknown>, partial as Record<string, unknown>, '') as P & N; |
| 84 | +} |
| 85 | + |
| 86 | +// Annotated rather than inferred: the inferred type spells the packs' literal |
| 87 | +// types out inline in the emitted declaration, which outgrew what tsc will |
| 88 | +// serialize (TS7056). Each `typeof` is emitted as a reference. |
| 89 | +export const en: typeof enPack & typeof enMarketplace = complete(enPack, enMarketplace, 'en'); |
| 90 | +export const zh: typeof zhPack & typeof zhMarketplace = complete(zhPack, zhMarketplace, 'zh'); |
| 91 | +export const ja: typeof jaPack & typeof jaMarketplace = complete(jaPack, jaMarketplace, 'ja'); |
| 92 | +export const ko: typeof koPack & typeof koMarketplace = complete(koPack, koMarketplace, 'ko'); |
| 93 | +export const de: typeof dePack & typeof deMarketplace = complete(dePack, deMarketplace, 'de'); |
| 94 | +export const fr: typeof frPack & typeof frMarketplace = complete(frPack, frMarketplace, 'fr'); |
| 95 | +export const es: typeof esPack & typeof esMarketplace = complete(esPack, esMarketplace, 'es'); |
| 96 | +export const pt: typeof ptPack & typeof ptMarketplace = complete(ptPack, ptMarketplace, 'pt'); |
| 97 | +export const ru: typeof ruPack & typeof ruMarketplace = complete(ruPack, ruMarketplace, 'ru'); |
| 98 | +export const ar: typeof arPack & typeof arMarketplace = complete(arPack, arMarketplace, 'ar'); |
44 | 99 |
|
45 | | -// Annotated rather than inferred: the inferred type spells all ten packs' |
46 | | -// literal types out inline in the emitted declaration, which outgrew what tsc |
47 | | -// will serialize (TS7056). Each `typeof <pack>` is the same type, emitted as a |
48 | | -// reference. |
| 100 | +/** |
| 101 | + * Map of all built-in locales keyed by language code — each one complete (see |
| 102 | + * the header). |
| 103 | + */ |
49 | 104 | export const builtInLocales: { |
50 | 105 | readonly en: typeof en; |
51 | 106 | readonly zh: typeof zh; |
|
0 commit comments