diff --git a/.changeset/21006-translate-object-list-views.md b/.changeset/21006-translate-object-list-views.md new file mode 100644 index 00000000000..049055d6261 --- /dev/null +++ b/.changeset/21006-translate-object-list-views.md @@ -0,0 +1,12 @@ +--- +'@objectstack/spec': patch +--- + +fix(spec): an object's embedded `listViews` are served in the reader's language, from the `_views` keys `os i18n extract` already writes + +Clause-②: no + +- `GET /api/v1/meta/object` and `GET /api/v1/meta/object/:name` now translate each view in an object's `listViews`: its `label`, its `description`, and the copy of its `bulkActionDefs` (label, confirm prompt, confirm button, and each param's label, help and placeholder). They read the keys `os i18n extract` writes for those views, `objects.._views..*`, where `` is the view's key under `listViews`. For example, `sys_account`'s `mine` view is now served as `我的链接` to a `zh-CN` reader. It used to be served as the authored `My Links`. +- A view with no translation for the requested locale keeps its authored text. An object with no `_views` entries is served unchanged. +- A string that was changed after the package shipped still wins over the packaged translation, the same rule a served view document and a dashboard follow. The string is compared with the same view in the packaged object. If it differs, the changed string is served in every locale. A view the packaged object does not declare keeps all of its own strings. +- `translateObject` (and `translateMetadataDocument('object', …)`) in `@objectstack/spec/system` does the translating, so any caller of those functions gets the same result. diff --git a/packages/spec/src/system/i18n-resolver.object-list-views.test.ts b/packages/spec/src/system/i18n-resolver.object-list-views.test.ts new file mode 100644 index 00000000000..c00b3370664 --- /dev/null +++ b/packages/spec/src/system/i18n-resolver.object-list-views.test.ts @@ -0,0 +1,322 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * Pin: `translateObject` localizes an object's EMBEDDED `listViews` from the + * catalog keys `os i18n extract` already writes for them — + * `objects.._views..{label, description, bulkActions.*}` + * (`pushViewEntries` in `packages/cli/src/utils/i18n-extract.ts`, called with + * the record key) — and lets an explicit override beat that catalog by the + * same comparison `translateView` applies to a served view document + * (ADR-0029 D9.2a). + * + * Before this, the extractor wrote those keys and every shipped platform bundle + * carried them (`sys_account._views.mine.label` → `我的链接`), while the served + * object document kept the authored `My Links`: extract and serve disagreed, + * and only a client re-translating with its own bundle showed the tab in the + * reader's language. + * + * The fixture is `sys_account` as `@objectstack/platform-objects` ships it + * (`identity/sys-account.object.ts`), its three list views transcribed with + * the copy the catalog addresses, and the bundle carries the shipped `en` / + * `zh-CN` `_views` leaves verbatim. Both are PARSED by the authoring schemas, + * so a view address or catalog key either schema does not carry cannot pass + * here. `spec` cannot import `platform-objects` (the dependency runs the other + * way), which is why the shape is transcribed rather than imported. + */ + +import { describe, it, expect } from 'vitest'; +import { translateObject, translateMetadataDocument } from './i18n-resolver'; +import { TranslationBundleSchema, type TranslationBundle } from './translation.zod'; +import { ObjectListViewSchema } from '../ui/view.zod'; + +const clone = (v: T): T => JSON.parse(JSON.stringify(v)); + +/** One embedded list view, parsed by the schema `ObjectSchema.listViews` uses. */ +const view = (raw: Record): any => ObjectListViewSchema.parse(raw); + +/** `sys_account` as the package ships it — the PACKAGED base. */ +const PACKAGED: any = { + name: 'sys_account', + label: 'Account', + listViews: { + mine: view({ + type: 'grid', + name: 'mine', + label: 'My Links', + data: { provider: 'object', object: 'sys_account' }, + columns: ['provider_id', 'account_id', 'created_at', 'updated_at'], + }), + by_provider: view({ + type: 'grid', + name: 'by_provider', + label: 'By Provider', + data: { provider: 'object', object: 'sys_account' }, + columns: ['provider_id', 'user_id', 'account_id', 'created_at'], + }), + all_links: view({ + type: 'grid', + name: 'all_links', + label: 'All', + data: { provider: 'object', object: 'sys_account' }, + columns: ['provider_id', 'user_id', 'account_id', 'created_at', 'updated_at'], + }), + }, +}; + +/** + * The shipped `_views` leaves (`en.objects.generated.ts` / + * `zh-CN.objects.generated.ts`). `en` repeats the authored strings, which is + * what extract writes for the source locale — and exactly as able to overwrite + * an edit as a translation is. + */ +const BUNDLE: TranslationBundle = TranslationBundleSchema.parse({ + en: { + objects: { + sys_account: { + label: 'Account', + _views: { mine: { label: 'My Links' }, by_provider: { label: 'By Provider' }, all_links: { label: 'All' } }, + }, + }, + }, + 'zh-CN': { + objects: { + sys_account: { + label: '身份链接', + _views: { mine: { label: '我的链接' }, by_provider: { label: '按提供方' }, all_links: { label: '全部' } }, + }, + }, + }, +}); + +const zh = (doc: any, packagedBase?: unknown): any => + translateObject(doc, BUNDLE, { locale: 'zh-CN', packagedBase }); + +describe('translateObject — embedded listViews read the `_views` keys the extractor writes', () => { + it("serves sys_account's `mine` tab translated in zh-CN — and every sibling tab", () => { + const out = zh(clone(PACKAGED), PACKAGED); + expect(out.listViews.mine.label).toBe('我的链接'); + expect(out.listViews.by_provider.label).toBe('按提供方'); + expect(out.listViews.all_links.label).toBe('全部'); + // The object's own label was already translated; it still is. + expect(out.label).toBe('身份链接'); + }); + + it('serves the same tabs with no packaged base known (the pre-override answer: the catalog applies)', () => { + expect(zh(clone(PACKAGED)).listViews.mine.label).toBe('我的链接'); + }); + + it('keys on the listViews RECORD key — the extractor\'s key — never on the view\'s own `name`', () => { + // `pushViewEntries` is handed the record key (`Object.entries(obj.listViews)`), + // so that is the one address a translation exists under. A view whose + // optional `name` says something else is still the tab under its key. + const doc = clone(PACKAGED); + doc.listViews.mine.name = 'my_accounts'; + expect(zh(doc, PACKAGED).listViews.mine.label).toBe('我的链接'); + }); + + it('the control — a view with no catalog entry keeps its authored label', () => { + const doc = clone(PACKAGED); + doc.listViews.recent = view({ + type: 'grid', + label: 'Recently Linked', + data: { provider: 'object', object: 'sys_account' }, + columns: ['provider_id'], + }); + const out = zh(doc); + expect(out.listViews.recent.label).toBe('Recently Linked'); + // …and the tabs that DO have entries are translated beside it. + expect(out.listViews.mine.label).toBe('我的链接'); + }); + + it('the control — an object whose catalog carries no `_views` hands back the very same listViews', () => { + const doc = { ...clone(PACKAGED), name: 'sys_untranslated' }; + const out = translateObject(doc, BUNDLE, { locale: 'zh-CN' }) as any; + expect(out.listViews).toBe(doc.listViews); + expect(out.listViews.mine.label).toBe('My Links'); + }); + + it('no bundle at all hands back the very same listViews', () => { + const doc = clone(PACKAGED); + expect((translateObject(doc, undefined, { locale: 'zh-CN' }) as any).listViews).toBe(doc.listViews); + }); + + it('an object with no listViews gains none', () => { + const doc = { name: 'sys_account', label: 'Account' }; + expect('listViews' in translateObject(doc, BUNDLE, { locale: 'zh-CN' })).toBe(false); + }); + + it('walks the request locale the same way every other resolver does (BCP-47 base language)', () => { + expect(translateObject(clone(PACKAGED), BUNDLE, { locale: 'zh' }).listViews.mine.label).toBe('我的链接'); + }); + + it('a request for the default locale answers with the authored label, never a fallback chain entry', () => { + // The #15711 rule `localeChain` carries: the shared lookup is what makes + // the embedded tab obey it too. + const out = translateObject(clone(PACKAGED), BUNDLE, { + locale: 'fr-FR', + defaultLocale: 'fr-FR', + fallbackChain: ['zh-CN'], + }) as any; + expect(out.listViews.mine.label).toBe('My Links'); + }); + + it('does not mutate either input document', () => { + const doc = clone(PACKAGED); + doc.listViews.mine.label = 'My Links (edited)'; + const before = clone(doc); + const packagedBefore = clone(PACKAGED); + zh(doc, PACKAGED); + expect(doc).toEqual(before); + expect(PACKAGED).toEqual(packagedBefore); + }); + + it('reaches the generic dispatcher — translateMetadataDocument("object") translates the tabs', () => { + // The REST boundary never calls `translateObject` directly. + const out = translateMetadataDocument('object', clone(PACKAGED), BUNDLE, { locale: 'zh-CN', packagedBase: PACKAGED }); + expect(out.listViews.mine.label).toBe('我的链接'); + }); +}); + +describe('translateObject — embedded listViews: description and bulk-action copy', () => { + const WITH_COPY: any = { + name: 'sys_account', + listViews: { + mine: view({ + type: 'grid', + label: 'My Links', + description: 'Accounts linked to you', + data: { provider: 'object', object: 'sys_account' }, + columns: ['provider_id'], + bulkActionDefs: [ + { + name: 'mark_reviewed', + label: 'Mark reviewed', + operation: 'update', + patch: { reviewed: true }, + confirmText: 'Mark the selected accounts reviewed?', + confirmLabel: 'Mark them', + params: [{ name: 'note', type: 'text', label: 'Note', help: 'Kept in the audit log', placeholder: 'Optional' }], + }, + ], + }), + }, + }; + + const COPY_BUNDLE: TranslationBundle = TranslationBundleSchema.parse({ + 'zh-CN': { + objects: { + sys_account: { + _views: { + mine: { + label: '我的链接', + description: '与你关联的账户', + bulkActions: { + mark_reviewed: { + label: '标记已复核', + confirmText: '确定将所选账户标记为已复核吗?', + confirmLabel: '确认标记', + params: { note: { label: '备注', help: '记录在审计日志中', placeholder: '选填' } }, + }, + }, + }, + }, + }, + }, + }, + }); + + const translate = (doc: any, packagedBase?: unknown): any => + translateObject(doc, COPY_BUNDLE, { locale: 'zh-CN', packagedBase }); + const def = (doc: any) => doc.listViews.mine.bulkActionDefs[0]; + + it('translates the description and the bulk-action defs authored ON the embedded view', () => { + // An embedded view carries its defs on itself — the authored address the + // extractor reads (`view.bulkActionDefs`) — not under the `config` a served + // view document nests them in. + const out = translate(clone(WITH_COPY), WITH_COPY); + expect(out.listViews.mine.description).toBe('与你关联的账户'); + expect(def(out)).toMatchObject({ label: '标记已复核', confirmText: '确定将所选账户标记为已复核吗?', confirmLabel: '确认标记' }); + expect(def(out).params[0]).toMatchObject({ label: '备注', help: '记录在审计日志中', placeholder: '选填' }); + // Nothing else on the def moved. + expect(def(out)).toMatchObject({ operation: 'update', patch: { reviewed: true } }); + }); + + it('an edited def string beats the catalog; the untouched ones on it stay translated', () => { + const doc = clone(WITH_COPY); + def(doc).label = 'Close out'; + def(doc).params[0].help = 'Shown to admins'; + const out = translate(doc, WITH_COPY); + expect(def(out).label).toBe('Close out'); + expect(def(out).confirmText).toBe('确定将所选账户标记为已复核吗?'); + expect(def(out).params[0].help).toBe('Shown to admins'); + expect(def(out).params[0].label).toBe('备注'); + }); + + it('an edited description beats the catalog; the label beside it stays translated', () => { + const doc = clone(WITH_COPY); + doc.listViews.mine.description = 'Our accounts'; + const out = translate(doc, WITH_COPY); + expect(out.listViews.mine.description).toBe('Our accounts'); + expect(out.listViews.mine.label).toBe('我的链接'); + }); +}); + +describe('translateObject — embedded listViews: the catalog loses to an explicit override (ADR-0029 D9.2a)', () => { + const edited = (label: string) => { + const doc = clone(PACKAGED); + doc.listViews.mine.label = label; + return doc; + }; + + it("an org overlay's edited tab label is served — in zh-CN and in the source locale", () => { + for (const locale of ['zh-CN', 'en']) { + const out = translateObject(edited('My Links (edited)'), BUNDLE, { locale, packagedBase: PACKAGED }) as any; + expect(out.listViews.mine.label, locale).toBe('My Links (edited)'); + } + }); + + it('judges each tab SEPARATELY — the unedited siblings stay translated', () => { + const out = zh(edited('My Links (edited)'), PACKAGED); + expect(out.listViews.by_provider.label).toBe('按提供方'); + expect(out.listViews.all_links.label).toBe('全部'); + }); + + it('NO packaged base supplied → the catalog applies (unknown is not authored)', () => { + expect(zh(edited('My Links (edited)')).listViews.mine.label).toBe('我的链接'); + expect(zh(edited('My Links (edited)'), null).listViews.mine.label).toBe('我的链接'); + }); + + it('a reset (the served tab equals the packaged one again) restores the translated shipped label', () => { + expect(zh(edited('My Links (edited)'), PACKAGED).listViews.mine.label).toBe('My Links (edited)'); + expect(zh(clone(PACKAGED), PACKAGED).listViews.mine.label).toBe('我的链接'); + }); + + it('the ruled edge — an edit back to exactly the shipped string is a no-op, and a non-string label is never an override', () => { + expect(zh(edited('My Links'), PACKAGED).listViews.mine.label).toBe('我的链接'); + const map = clone(PACKAGED); + map.listViews.mine.label = { en: 'Mine', 'zh-CN': '我的' }; + expect(zh(map, PACKAGED).listViews.mine.label).toBe('我的链接'); + }); + + it('a tab the package does not ship was authored after the fact and keeps its label', () => { + // A base that IS known but carries no such view: every string on it + // diverged, the rule a dashboard widget or a bulk-action def the package + // never shipped follows. The catalog entry under the same bare key + // translates some OTHER view and must not reach this one. + const base = { ...clone(PACKAGED), listViews: { by_provider: PACKAGED.listViews.by_provider } }; + const out = zh(clone(PACKAGED), base); + expect(out.listViews.mine.label).toBe('My Links'); + expect(out.listViews.by_provider.label).toBe('按提供方'); + // A base with no listViews at all: the same answer for every tab. + expect(zh(clone(PACKAGED), { name: 'sys_account' }).listViews.mine.label).toBe('My Links'); + }); + + it('the object-scalar rule and the tab rule are independent comparisons', () => { + const doc = edited('My Links (edited)'); + doc.label = 'Linked Identity'; + const out = zh(doc, PACKAGED); + expect(out.label).toBe('Linked Identity'); + expect(out.listViews.mine.label).toBe('My Links (edited)'); + expect(out.listViews.by_provider.label).toBe('按提供方'); + }); +}); diff --git a/packages/spec/src/system/i18n-resolver.ts b/packages/spec/src/system/i18n-resolver.ts index 509e963e94d..e5370986e64 100644 --- a/packages/spec/src/system/i18n-resolver.ts +++ b/packages/spec/src/system/i18n-resolver.ts @@ -24,7 +24,9 @@ * `` is the BARE authoring key (`listViews.`, or the default * list/form key) — never the `.` identity the registry assigns a * served view document. `resolveViewLabel` derives the bare key from that - * identity, so the same bundle serves both hand-built and served views (#4854). + * identity, so the same bundle serves both hand-built and served views (#4854), + * and `translateObject` reads the same keys for the views an object document + * embeds in its `listViews`, by their record key. * * For object-less actions (no `objectName`), helpers fall back to: * @@ -401,6 +403,32 @@ function viewTranslationKey(view: ViewLike, objectName: string): string { return view.name.startsWith(prefix) ? view.name.slice(prefix.length) : view.name; } +/** + * One string off a view's `objects.._views.` node, across the + * locale chain — `undefined` when no locale carries it. + * + * The ONE read of that address. Both view channels go through it: a served + * view document ({@link resolveViewLabel}, {@link resolveViewDescription}, + * keyed by {@link viewTranslationKey}) and a view embedded in an object + * document's `listViews` ({@link translateObject}, keyed by its record key). + * Both keys are the bare authoring key the i18n extractor's `pushViewEntries` + * writes, so one lookup serves both — ⛔ never a second key convention. + */ +function lookupViewText( + bundle: TranslationBundle | undefined, + objectName: string, + viewKey: string, + attr: 'label' | 'description', + opts?: ResolveOptions, +): string | undefined { + if (!bundle) return undefined; + for (const code of localeChain(opts)) { + const candidate = pickData(bundle, code)?.objects?.[objectName]?._views?.[viewKey]?.[attr]; + if (typeof candidate === 'string' && candidate.length > 0) return candidate; + } + return undefined; +} + /** * Resolve a translated view label, falling back to the literal `view.label` * (or `view.name`) when no translation is available. @@ -412,14 +440,8 @@ export function resolveViewLabel( ): string { const fallback = view.label ?? view.name; const objectName = viewObjectName(view); - if (!bundle || !objectName) return fallback; - const key = viewTranslationKey(view, objectName); - for (const code of localeChain(opts)) { - const data = pickData(bundle, code); - const candidate = data?.objects?.[objectName]?._views?.[key]?.label; - if (typeof candidate === 'string' && candidate.length > 0) return candidate; - } - return fallback; + if (!objectName) return fallback; + return lookupViewText(bundle, objectName, viewTranslationKey(view, objectName), 'label', opts) ?? fallback; } /** @@ -432,16 +454,9 @@ export function resolveViewDescription( opts?: ResolveOptions, ): string | undefined { const objectName = viewObjectName(view); - if (bundle && objectName) { - const key = viewTranslationKey(view, objectName); - for (const code of localeChain(opts)) { - const data = pickData(bundle, code); - const candidate = - data?.objects?.[objectName]?._views?.[key]?.description; - if (typeof candidate === 'string' && candidate.length > 0) return candidate; - } - } - return view.description; + if (!objectName) return view.description; + return lookupViewText(bundle, objectName, viewTranslationKey(view, objectName), 'description', opts) + ?? view.description; } /** @@ -2329,6 +2344,12 @@ export interface ObjectLike { fields?: Record | ObjectFieldLike[]; /** Actions declared inline on the object (`Object.actions`). */ actions?: ActionLike[]; + /** + * List views embedded in the object document (`Object.listViews`), keyed by + * view name. Each entry is narrowed at runtime to the copy + * {@link translateObject} overlays; the record key is the `_views` key. + */ + listViews?: Record; } export interface ObjectFieldLike { @@ -2745,13 +2766,96 @@ function valueOverridesPackagedBase(base: unknown, key: string, value: unknown): return (base as Record)[key] !== value; } +/** + * Overlay the catalog onto an object document's EMBEDDED `listViews` — each + * view's `label`, `description` and `bulkActionDefs[]` copy, the same strings + * {@link translateView} overlays on a served view document — returning the + * SAME record reference when nothing matched, so {@link translateObject} can + * leave `listViews` off its copy. + * + * ## The address is the extractor's, by construction + * + * The i18n extractor walks `obj.listViews` and hands `pushViewEntries` each + * RECORD key, so `objects.._views..*` is the one address a + * translation for an embedded view exists under. That key is already bare — + * there is no `.` prefix to strip, as there is on a served view + * document's registry name — so it goes straight to {@link lookupViewText} and + * {@link translateBulkActionDefs}, the helpers {@link translateView} reads + * through. The view's own optional `name` is never consulted. + * + * An embedded view carries its `bulkActionDefs` on itself, the authored + * address the extractor reads; a served view document nests them under + * `config`, which is why `translateView` reads them there. Nothing is + * translated that `translateView` would not translate: `emptyState` has no + * server-side reader on either channel. + * + * ## The catalog loses to an explicit override — ADR-0029 D9.2a + * + * The catalog translates the PACKAGED view, so every string is judged by + * {@link valueOverridesPackagedBase} (⛔ never a second comparison) against the + * same view in the packaged object's `listViews` — the base the serving layer + * supplies as {@link TranslateDocumentOptions.packagedBase}. A view the base + * does not carry was authored after the fact, and every string on it counts as + * diverged; no base supplied means nothing is inferred and the catalog applies. + */ +function translateObjectListViews( + listViews: unknown, + bundle: TranslationBundle | undefined, + objectName: string, + opts: TranslateDocumentOptions | undefined, +): unknown { + const views = asRecord(listViews); + if (views === undefined || !bundle) return listViews; + const base = asRecord(opts?.packagedBase); + // The base's own `listViews` — `{}` when the base is known but carries none, + // so every view on the served document then counts as authored. + const packagedViews = base === undefined ? undefined : asRecord(base.listViews) ?? {}; + let changed = false; + const next: Record = {}; + for (const [viewKey, raw] of Object.entries(views)) { + const view = asRecord(raw); + if (view === undefined) { + next[viewKey] = raw; + continue; + } + const packagedView = packagedViews === undefined ? undefined : asRecord(packagedViews[viewKey]) ?? {}; + const text = (attr: 'label' | 'description') => + valueOverridesPackagedBase(packagedView, attr, view[attr]) + ? undefined + : lookupViewText(bundle, objectName, viewKey, attr, opts); + const label = text('label'); + const description = text('description'); + const defs = translateBulkActionDefs(view.bulkActionDefs, bundle, objectName, viewKey, opts, packagedView); + if (label === undefined && description === undefined && defs === view.bulkActionDefs) { + next[viewKey] = raw; + continue; + } + changed = true; + next[viewKey] = { + ...view, + ...(label !== undefined ? { label } : {}), + ...(description !== undefined ? { description } : {}), + ...(defs !== view.bulkActionDefs ? { bulkActionDefs: defs } : {}), + }; + } + return changed ? next : listViews; +} + /** * Apply the active locale to an object metadata document. Translates the * object's `label` / `pluralLabel` / `description`, walks each field to - * translate its `label`, `help`, and per-option `label`s, and walks any - * inline-declared `actions` through {@link translateAction}. The input document + * translate its `label`, `help`, and per-option `label`s, walks any + * inline-declared `actions` through {@link translateAction}, and overlays the + * copy of each EMBEDDED `listViews` entry from the `_views` keys the i18n + * extractor writes for it ({@link translateObjectListViews}). The input document * is not mutated; a structural clone of the touched branches is returned. * + * The embedded views were the second long-standing hole: `os i18n extract` + * wrote `objects.._views..*` for them and every shipped platform + * bundle carried the leaves, while the served object went out with the + * authored tab labels — extract and serve disagreed, and only a client + * re-translating with its own bundle showed a tab in the reader's language. + * * Field maps come in two shapes across the codebase: a `Record` * (preferred — the canonical authored shape) and an `Array` (some REST * responses flatten the record). Both are supported; the function returns the @@ -2786,6 +2890,10 @@ function valueOverridesPackagedBase(base: unknown, key: string, value: unknown): * The rule is scoped to the three SCALARS, which are what the ruling covers: * `fields` is a key-keyed spread whose per-field labels have their own * (per-field) catalog keys, and no read has been measured to diverge on them. + * The embedded `listViews` follow the VIEW translator's application of the + * same rule (ADR-0029 D9.2a, as {@link translateView} applies it), judged per + * view against the packaged object's own `listViews` — see + * {@link translateObjectListViews}. */ export function translateObject( doc: T, @@ -2866,6 +2974,8 @@ export function translateObject( }) : undefined; + const listViews = translateObjectListViews(doc.listViews, bundle, objectName, opts); + return { ...doc, ...(label !== undefined ? { label } : {}), @@ -2873,6 +2983,7 @@ export function translateObject( ...(description !== undefined ? { description } : {}), ...(fields !== undefined ? { fields } : {}), ...(actions !== undefined ? { actions } : {}), + ...(listViews !== doc.listViews ? { listViews } : {}), }; }