From b967a51265639c1f2c240c45150959086925b92e Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 09:31:49 +0000 Subject: [PATCH 1/4] fix(platform-objects): load platform translation bundles under what a host already loaded A host bundle for a platform key now wins in either boot order. The bundles load in start() when an i18n service exists (before the kernel:ready authored-translation sync), else on a kernel:ready backstop registered in init(). Claude-Session: https://claude.ai/code/session_01ADzJtzYTLUfgrRZHxkagkX Co-authored-by: Claude --- packages/platform-objects/src/plugin.ts | 171 +++++++++++++++++++----- 1 file changed, 137 insertions(+), 34 deletions(-) diff --git a/packages/platform-objects/src/plugin.ts b/packages/platform-objects/src/plugin.ts index b1b673c8394..9dc317736f4 100644 --- a/packages/platform-objects/src/plugin.ts +++ b/packages/platform-objects/src/plugin.ts @@ -94,8 +94,11 @@ import { SEED_SETTLEMENT_SERVICE } from '@objectstack/spec/contracts'; * (`metadataForms.*` for object/field/agent/flow/view configuration * forms). Replaces the historical detour of piggy-backing on * `plugin-auth` — translations belong with the package that defines - * them. Loaded on `kernel:ready` so the i18n service plugin (which may - * register later than ordinary services) is guaranteed to be present. + * them. Loaded UNDER what is already loaded, so a host's bundle for a + * platform key wins in either boot order: in `start()` when an i18n + * service exists by then, else on `kernel:ready` (an `AppPlugin` on a + * lean kernel registers its in-memory fallback during its own `start()`). + * See `contributeTranslations`. * * Gracefully degrades on lean kernels: without a manifest service the * ledger is simply absent — every flag reader answers "not verified", the @@ -197,6 +200,12 @@ export class PlatformObjectsPlugin { // activation ledger stays unregistered (every enable/disable door // refuses loudly with 503 rather than keeping a bit that reverts). } + + // The translation backstop for a kernel with no i18n service yet at this + // plugin's `start()`. Registered here, not in `start()`, so it runs ahead of + // the authored-translation sync hooks of the plugins ordered after this one + // (see `contributeTranslations`). A no-op when `start()` already loaded. + ctx?.hook?.('kernel:ready', () => this.contributeTranslations(ctx)); } async start(ctx: any): Promise { @@ -297,44 +306,138 @@ export class PlatformObjectsPlugin { ctx?.hook?.('app:seeded', () => attest('app:seeded')); ctx?.hook?.('kernel:ready', () => attest('kernel:ready')); - ctx?.hook?.('kernel:ready', async () => { - let i18n: II18nService | undefined; - try { - i18n = ctx.getService?.('i18n'); - } catch { - return; - } - if (!i18n || typeof i18n.loadTranslations !== 'function') return; + // Platform translation bundles: here, as soon as an i18n service exists. + // `init()` registered the `kernel:ready` backstop for a kernel whose i18n + // service only appears later in Phase 2. + this.contributeTranslations(ctx); + } - const bundles: Array<[string, Record]> = [ - ['Setup', SetupAppTranslations as unknown as Record], - ['MetadataForms', MetadataFormsTranslations as unknown as Record], - ]; + /** + * The i18n service instance the platform bundles were loaded into, so the + * `kernel:ready` backstop loads them once per instance, never twice. + */ + private translationsLoadedInto: II18nService | undefined; - let loaded = 0; - for (const [bundleName, bundle] of bundles) { - for (const [locale, data] of Object.entries(bundle)) { - if (!data || typeof data !== 'object') continue; - try { - i18n.loadTranslations(locale, data as Record); - loaded++; - } catch (err: any) { - ctx?.logger?.warn?.( - `[platform-objects] failed to load ${bundleName} translations for '${locale}': ${ - err?.message ?? err - }`, - ); - } + /** + * Load the platform translation bundles UNDER whatever is already loaded + * for each locale, so a host's bundle for a platform key wins in either + * boot order — the precedence `content/docs/ui/translations.mdx` publishes + * ("yours would override the platform's"). A bundle loaded later wins by + * the service's own deep merge (last writer); one loaded earlier wins + * because {@link layUnder} hands it back on top of the platform's. + * + * ## Why `start()` first, with `kernel:ready` only as a backstop + * + * The read that makes "under" possible is `II18nService.getTranslations`, + * and both real providers answer it with the runtime-AUTHORED overlay + * (Studio `translation` items) merged over the bundles. That overlay is + * synced at `kernel:ready`, by a hook `AppPlugin` registers in `init()` — + * ahead of every hook registered in `start()`. Read after it, the overlay's + * values would be written into the bundle layer, and a deleted Studio + * override of a platform key would keep serving the deleted text until the + * next restart. Read in `start()`, no sync has run yet. The backstop hook is + * registered in `init()` for the same reason: it then runs ahead of the + * sync hooks of every plugin ordered after this one. + */ + private contributeTranslations(ctx: any): void { + let i18n: II18nService | undefined; + try { + i18n = ctx.getService?.('i18n'); + } catch { + return; + } + if (!i18n || typeof i18n.loadTranslations !== 'function') return; + if (this.translationsLoadedInto === i18n) return; + this.translationsLoadedInto = i18n; + + // `getTranslations` is on the contract. A provider without it still gets + // the platform bundles, loaded the old way: over whatever is there. + const canLayUnder = typeof i18n.getTranslations === 'function'; + if (!canLayUnder) { + ctx?.logger?.warn?.( + '[platform-objects] the i18n service has no getTranslations(locale), so the platform ' + + 'translation bundles were loaded OVER any bundle already registered: a host bundle ' + + 'loaded before this point does not override the platform text it repeats. Implement ' + + 'getTranslations (II18nService) on the provider to restore that precedence.', + ); + } + + const bundles: Array<[string, Record]> = [ + ['Setup', SetupAppTranslations as unknown as Record], + ['MetadataForms', MetadataFormsTranslations as unknown as Record], + ]; + + let loaded = 0; + for (const [bundleName, bundle] of bundles) { + for (const [locale, data] of Object.entries(bundle)) { + if (!data || typeof data !== 'object') continue; + try { + const platform = data as Record; + i18n.loadTranslations( + locale, + canLayUnder ? layUnder(platform, readLoaded(i18n, locale)) : platform, + ); + loaded++; + } catch (err: any) { + ctx?.logger?.warn?.( + `[platform-objects] failed to load ${bundleName} translations for '${locale}': ${ + err?.message ?? err + }`, + ); } } + } - if (loaded > 0) { - ctx?.logger?.info?.( - `[platform-objects] contributed platform translations (${loaded} locale entries)`, - ); - } - }); + if (loaded > 0) { + ctx?.logger?.info?.( + `[platform-objects] contributed platform translations (${loaded} locale entries)`, + ); + } + } +} + +/** What the i18n service holds for `locale` now; empty when it cannot say. */ +function readLoaded(i18n: II18nService, locale: string): Record { + try { + const loaded = i18n.getTranslations(locale); + return isPlainRecord(loaded) ? loaded : {}; + } catch { + // Unreadable reads as nothing loaded: the platform bundle still lands. + return {}; + } +} + +function isPlainRecord(value: unknown): value is Record { + return value !== null && typeof value === 'object' && !Array.isArray(value); +} + +/** + * The platform bundle with every leaf the service already holds laid back on + * top — what to hand `loadTranslations` so the platform text goes UNDER it. + * + * Walks the platform bundle's own keys only: a key outside it is untouched by + * the service's deep merge anyway, so nothing is copied that the load would + * not reach. Where both sides hold an object it recurses, so a host label + * deep inside `objects..fields` wins and its platform siblings still + * land; anywhere else the loaded value is handed back as it is — a string + * over a platform subtree, or a subtree over a platform string — so the load + * never replaces or deletes a key the host shipped. Arrays are values, as in + * the providers' own merge. Neither input is mutated. + */ +function layUnder( + platform: Record, + loaded: Record, +): Record { + const out: Record = {}; + for (const [key, platformValue] of Object.entries(platform)) { + const loadedValue = loaded[key]; + if (isPlainRecord(platformValue) && isPlainRecord(loadedValue)) { + out[key] = layUnder(platformValue, loadedValue); + } else { + out[key] = loadedValue === undefined ? platformValue : loadedValue; + } } + return out; } /** From fc615085afc443ef344edf67e13c55e5a02ae21e Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 09:37:05 +0000 Subject: [PATCH 2/4] test(platform-objects): pin host-bundle precedence over the platform bundles on a real i18n service Claude-Session: https://claude.ai/code/session_01ADzJtzYTLUfgrRZHxkagkX Co-authored-by: Claude --- .../src/plugin.translations.test.ts | 185 ++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 packages/platform-objects/src/plugin.translations.test.ts diff --git a/packages/platform-objects/src/plugin.translations.test.ts b/packages/platform-objects/src/plugin.translations.test.ts new file mode 100644 index 00000000000..53098b20d0c --- /dev/null +++ b/packages/platform-objects/src/plugin.translations.test.ts @@ -0,0 +1,185 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +import { describe, it, expect } from 'vitest'; +import { createMemoryI18n } from '@objectstack/core'; +import { PlatformObjectsPlugin } from './plugin.js'; +import { SetupAppTranslations } from './apps/translations/index.js'; + +/** + * A host's translation bundle for a platform key wins over this package's + * bundle in either boot order — the precedence `content/docs/ui/translations.mdx` + * publishes ("yours would override the platform's"). Pinned on a REAL + * `II18nService` (the kernel's in-memory provider, the one every kernel without + * `I18nServicePlugin` serves), because the defect lived in how two loads + * compose inside that service, which a recording double cannot show. + */ + +const LABEL = 'objects.sys_organization.label'; +const PLURAL = 'objects.sys_organization.pluralLabel'; +const SLUG_LABEL = 'objects.sys_organization.fields.slug.label'; +const SLUG_HELP = 'objects.sys_organization.fields.slug.help'; +const HOST_ONLY = 'objects.sys_organization.fields.host_only.label'; + +/** A host's zh-CN bundle: one top-level label, one nested field label, one key of its own. */ +const HOST_ZH = { + objects: { + sys_organization: { + label: '工作区', + fields: { slug: { label: '工作区标识' }, host_only: { label: '仅宿主' } }, + }, + }, +}; + +const platform = (locale: string): any => (SetupAppTranslations as any)[locale].objects.sys_organization; + +/** Fake PluginContext whose hooks run in registration order, as both kernels run them. */ +function makeCtx() { + const services = new Map(); + const byEvent: Record Promise | void>> = {}; + const warns: string[] = []; + const ctx: any = { + logger: { info: () => {}, warn: (m: string) => { warns.push(String(m)); } }, + _warns: warns, + registerService: (name: string, svc: any) => { services.set(name, svc); }, + getService: (name: string) => { + const s = services.get(name); + if (!s) throw new Error(`service '${name}' not registered`); + return s; + }, + hook: (event: string, fn: () => Promise | void) => { (byEvent[event] ??= []).push(fn); }, + _flush: async (event: string) => { for (const h of byEvent[event] ?? []) await h(); }, + }; + return ctx; +} + +async function boot(ctx: any, plugin = new PlatformObjectsPlugin()) { + await plugin.init(ctx); + await plugin.start(ctx); + await ctx._flush('kernel:ready'); +} + +describe('PlatformObjectsPlugin: a host translation bundle wins over the platform bundle', () => { + it('precondition: the host label differs from the platform label, so a pass cannot be vacuous', () => { + expect(platform('zh-CN').label).not.toBe('工作区'); + expect(platform('zh-CN').fields.slug.label).not.toBe('工作区标识'); + }); + + it('a host bundle loaded BEFORE the platform bundles serves the host label', async () => { + const ctx = makeCtx(); + const i18n = createMemoryI18n(); + ctx.registerService('i18n', i18n); + i18n.loadTranslations('zh-CN', HOST_ZH); // a host plugin's init-time bundle + + await boot(ctx); + + expect(i18n.t(LABEL, 'zh-CN')).toBe('工作区'); + expect(i18n.t(SLUG_LABEL, 'zh-CN')).toBe('工作区标识'); + }); + + it('a host bundle loaded AFTER kernel:ready also serves the host label', async () => { + const ctx = makeCtx(); + const i18n = createMemoryI18n(); + ctx.registerService('i18n', i18n); + const plugin = new PlatformObjectsPlugin(); + await plugin.init(ctx); + await plugin.start(ctx); + ctx.hook('kernel:ready', () => i18n.loadTranslations('zh-CN', HOST_ZH)); // registered after the plugin's + + await ctx._flush('kernel:ready'); + + expect(i18n.t(LABEL, 'zh-CN')).toBe('工作区'); + expect(i18n.t(SLUG_LABEL, 'zh-CN')).toBe('工作区标识'); + }); + + it('CONTROL: a key the host does not ship serves the platform text, and a host-only key survives', async () => { + const ctx = makeCtx(); + const i18n = createMemoryI18n(); + ctx.registerService('i18n', i18n); + i18n.loadTranslations('zh-CN', HOST_ZH); + + await boot(ctx); + + expect(i18n.t(PLURAL, 'zh-CN')).toBe(platform('zh-CN').pluralLabel); + expect(i18n.t(SLUG_HELP, 'zh-CN')).toBe(platform('zh-CN').fields.slug.help); + expect(i18n.t(HOST_ONLY, 'zh-CN')).toBe('仅宿主'); + }); + + it('CONTROL: a locale the host does not ship serves the platform text unchanged', async () => { + const ctx = makeCtx(); + const i18n = createMemoryI18n(); + ctx.registerService('i18n', i18n); + i18n.loadTranslations('zh-CN', HOST_ZH); + + await boot(ctx); + + expect(i18n.t(LABEL, 'ja-JP')).toBe(platform('ja-JP').label); + expect(i18n.t(SLUG_LABEL, 'ja-JP')).toBe(platform('ja-JP').fields.slug.label); + expect(i18n.t(LABEL, 'en')).toBe(platform('en').label); + }); + + it('an authored overlay synced at kernel:ready is never written into the bundle layer', async () => { + // `AppPlugin` wires the authored-translation sync in init(), so its + // kernel:ready hook is registered ahead of anything registered in start(). + const ctx = makeCtx(); + const i18n = createMemoryI18n(); + ctx.registerService('i18n', i18n); + ctx.hook('kernel:ready', () => { + i18n.replaceAuthoredTranslations({ + 'zh-CN': { objects: { sys_organization: { pluralLabel: '已编写' } } }, + }); + }); + + await boot(ctx); + expect(i18n.t(PLURAL, 'zh-CN')).toBe('已编写'); + + i18n.replaceAuthoredTranslations({}); // the Studio override is deleted + expect(i18n.t(PLURAL, 'zh-CN')).toBe(platform('zh-CN').pluralLabel); + }); + + it('an i18n service that appears only after start() gets the bundles on kernel:ready, host still winning', async () => { + const ctx = makeCtx(); + const plugin = new PlatformObjectsPlugin(); + await plugin.init(ctx); + await plugin.start(ctx); // no i18n service yet: nothing to load into + + // A later plugin's start() (an AppPlugin on a lean kernel) registers one and loads its bundle. + const i18n = createMemoryI18n(); + ctx.registerService('i18n', i18n); + i18n.loadTranslations('zh-CN', HOST_ZH); + + await ctx._flush('kernel:ready'); + + expect(i18n.t(LABEL, 'zh-CN')).toBe('工作区'); + expect(i18n.t(PLURAL, 'zh-CN')).toBe(platform('zh-CN').pluralLabel); + }); + + it('loads each bundle once per service: the kernel:ready backstop adds nothing after start()', async () => { + const ctx = makeCtx(); + const real = createMemoryI18n(); + const loads: string[] = []; + ctx.registerService('i18n', { + ...real, + loadTranslations: (locale: string, data: Record) => { + loads.push(locale); + real.loadTranslations(locale, data); + }, + }); + + await boot(ctx); + + // Two bundles (Setup, MetadataForms) times the four locales each ships. + expect(loads.sort()).toEqual(['en', 'en', 'es-ES', 'es-ES', 'ja-JP', 'ja-JP', 'zh-CN', 'zh-CN']); + }); + + it('fail-safe: a provider with no getTranslations still gets the platform bundles, and says so', async () => { + const ctx = makeCtx(); + const withoutRead: any = { ...createMemoryI18n() }; + delete withoutRead.getTranslations; + ctx.registerService('i18n', withoutRead); + + await boot(ctx); + + expect(withoutRead.t(LABEL, 'zh-CN')).toBe(platform('zh-CN').label); + expect(ctx._warns.filter((w: string) => w.includes('getTranslations'))).toHaveLength(1); + }); +}); From 3284b5db9e7f81f3430855fcd4fb03da67082d35 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 09:37:59 +0000 Subject: [PATCH 3/4] test(platform-objects): pin the late-i18n path against writing in an authored overlay Claude-Session: https://claude.ai/code/session_01ADzJtzYTLUfgrRZHxkagkX Co-authored-by: Claude --- .../src/plugin.translations.test.ts | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/packages/platform-objects/src/plugin.translations.test.ts b/packages/platform-objects/src/plugin.translations.test.ts index 53098b20d0c..df08f165bed 100644 --- a/packages/platform-objects/src/plugin.translations.test.ts +++ b/packages/platform-objects/src/plugin.translations.test.ts @@ -153,6 +153,30 @@ describe('PlatformObjectsPlugin: a host translation bundle wins over the platfor expect(i18n.t(PLURAL, 'zh-CN')).toBe(platform('zh-CN').pluralLabel); }); + it('on that late path too, the authored overlay of a plugin ordered after this one is never written in', async () => { + const ctx = makeCtx(); + const plugin = new PlatformObjectsPlugin(); + await plugin.init(ctx); + // The next plugin's init() wires the authored-translation sync (as AppPlugin does). + let i18n: ReturnType | undefined; + ctx.hook('kernel:ready', () => { + i18n?.replaceAuthoredTranslations({ + 'zh-CN': { objects: { sys_organization: { pluralLabel: '已编写' } } }, + }); + }); + await plugin.start(ctx); // no i18n service yet + i18n = createMemoryI18n(); + ctx.registerService('i18n', i18n); + i18n.loadTranslations('zh-CN', HOST_ZH); + + await ctx._flush('kernel:ready'); + expect(i18n.t(PLURAL, 'zh-CN')).toBe('已编写'); + expect(i18n.t(LABEL, 'zh-CN')).toBe('工作区'); + + i18n.replaceAuthoredTranslations({}); + expect(i18n.t(PLURAL, 'zh-CN')).toBe(platform('zh-CN').pluralLabel); + }); + it('loads each bundle once per service: the kernel:ready backstop adds nothing after start()', async () => { const ctx = makeCtx(); const real = createMemoryI18n(); From 1291f2b3a0ea1b2f3135060487faaf5c6d5c789c Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 09:41:15 +0000 Subject: [PATCH 4/4] chore(changeset): platform-objects host translation bundles win over the platform's Claude-Session: https://claude.ai/code/session_01ADzJtzYTLUfgrRZHxkagkX Co-authored-by: Claude --- .changeset/22972-host-bundle-wins.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 .changeset/22972-host-bundle-wins.md diff --git a/.changeset/22972-host-bundle-wins.md b/.changeset/22972-host-bundle-wins.md new file mode 100644 index 00000000000..bdae02ec02e --- /dev/null +++ b/.changeset/22972-host-bundle-wins.md @@ -0,0 +1,18 @@ +--- +'@objectstack/platform-objects': patch +--- + +A host's translation bundle for a platform key now overrides the platform's text, whichever loads first + +Clause-②: no + +`PlatformObjectsPlugin` loaded its translation bundles (the Setup App, the `sys_*` objects, `metadataForms.*`) on `kernel:ready`, and the i18n service merges each load over the last. A bundle a host loaded earlier, during its own `init()` or `start()` (every `defineStack({ translations })` app is one), was overwritten for every key the platform also ships. Measured on both i18n providers: a host zh-CN bundle that set `objects.sys_organization.label` to 「工作区」 still served 「组织」. The docs already said such a bundle "would override the platform's". + +The platform bundles now load UNDER what the service already holds for each locale. + +- **A host's key wins in either order.** A bundle loaded before the platform's is laid back on top of it, and a bundle loaded after it wins as before. +- **Everything the host does not ship still serves the platform's text.** That covers sibling keys of a host key (a host field label leaves the platform's `help` for that field in place), and every locale the host does not ship. +- **Studio-authored translations are unaffected.** The bundles now load in `start()`, as soon as an i18n service exists, which is before the authored overlay is synced at `kernel:ready`. Deleting an authored override of a platform key brings back the platform text, as before. A kernel whose i18n service only appears later in Phase 2 (an `AppPlugin` registering the in-memory fallback on a lean kernel) gets the bundles on `kernel:ready` instead. +- **A provider without `getTranslations`** still gets the platform bundles, loaded over what is there as before, and the boot warns once that host bundles loaded earlier do not override it. + +No platform text changes: zh-CN `sys_organization` is still 「组织」. A host that wants different text ships it in its own bundle. The other platform packages' bundles share no key with this package's, and no example app's bundle repeats a platform key, so nothing else changes what it serves.