Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .changeset/35384-step-label-and-notification-object-label.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
'@objectstack/spec': patch
'@objectstack/service-messaging': patch
'@objectstack/plugin-approvals': patch
---

fix(i18n): an approval step label and an approval notification's object label read as the metadata translators serve them

Clause-②: yes (widening)
Metadata-protocol: none

In a deployment whose `i18n.sourceLocale` differs from its `i18n.defaultLocale` (an app authored in English that defaults its users to Chinese), an English reader was shown the Chinese approval step name, and an English approval-notification recipient read the Chinese object name inside an English sentence. Both labels were read through `II18nService.t()`, which falls back to the `defaultLocale` row; `/meta` serves the authored text. Both now resolve the way the metadata route resolves a label: the reader's negotiated locale, the declared fallback chain, and the authored language (the declared `sourceLocale`, else `defaultLocale`).

- **`@objectstack/spec`** adds `resolveFlowApprovalStepLabel(bundle, flowName, nodeId, literal?, opts?)` (`@objectstack/spec/system`), the one reading of `flows.<flow>.approvals.<node_id>.label`: the requested locale, then the declared fallback chain (skipped for the authored locale), then `literal`. It takes the flow's name and the node's id, so a reader holding only an approval request row needs no flow document.
- **`@objectstack/service-messaging`** accepts a new `NotificationTextKeys.params` value, `{ object: '<object API name>', fallback }` (`NotificationObjectLabelParam`): the label of that object in the recipient's language, resolved as `GET /meta/object/:name` serves it, by `translateObject` over the object's served definition and its packaged base, so a renamed packaged object is named by its rename. It renders the same way on the inline fan-out, the dispatcher (digests included) and the inbox reader. `fallback` is delivered when nothing resolves. The `{ key, fallback }` value is still accepted and still renders through `t()`, so stored deliveries and inbox rows render as before.
- **`@objectstack/plugin-approvals`** names a notification's object with that typed param, and reads the approval step label through `resolveFlowApprovalStepLabel`, so `t()` carries catalog sentences only.

Upgrade `@objectstack/plugin-approvals` and `@objectstack/service-messaging` together: an older messaging service refuses the typed param at `emit()`.
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,81 @@ describe('#24978 — end to end: a real messaging service renders each approver\
]);
});
});

/**
* [#35384] The object an approval notification names is labelled as
* `GET /meta/object/:name` serves it: the subject param is the messaging
* service's typed object-label param (`{ object, fallback }`), resolved
* per recipient by `translateObject` with the metadata route's resolve options
* and the object's packaged base, never through `t()`.
*
* The deployment: authored in English (`sourceLocale: 'en'`), defaulting its
* users to Chinese (`defaultLocale` and the declared fallback `zh-CN`), with a
* `zh-CN` pack for the object. `t('objects.crm_deal.label', 'en')` falls to the
* `zh-CN` row, so before this an English approver read `商机` inside an
* English sentence. CONTROL: the Chinese approver still reads `商机`.
*/
describe('[#35384] the object an approval notification names reads as /meta serves it', () => {
/** The en-authored, zh-CN-default deployment's i18n service. */
const sourceLocaleI18n = () => {
const i18n = i18nWithBundle('zh-CN');
i18n.setFallbackLocale('zh-CN');
i18n.setSourceLocale('en');
// The served adapter's shape (`FileI18nAdapter`): the declared fallback, read back.
return Object.assign(i18n, { getFallbackLocale: () => 'zh-CN' });
};

/** Open one request on a fresh engine; `rename` extends the packaged `Deal` into a new label first. */
const openOn = async (rename?: string) => {
const engine = await bootEngine([
{ id: 'u_sub', name: 'Sun Submitter', email: 'sun@example.com', locale: 'zh-CN' },
{ id: 'u_zh', name: 'Zhang', email: 'zhang@example.com', locale: 'zh-CN' },
{ id: 'u_en', name: 'Ada', email: 'ada@example.com', locale: 'en' },
]);
try {
if (rename) engine.registry.registerObjectExtension({ extend: 'crm_deal', label: rename } as never, 'com.acme.crm.custom');
const i18n = sourceLocaleI18n();
const getData = () => engine as any;
const messaging = new MessagingService({
logger: silent,
getData,
textRenderer: new NotificationTextRenderer({
getData, getI18n: () => i18n, getDefaultLocale: () => i18n.getDefaultLocale(), logger: silent,
}),
});
messaging.registerChannel(createInboxChannel({ getData }));
let now = Date.parse('2026-10-01T09:00:00.000Z');
const svc = approvalService(engine, () => new Date(now += 1000));
svc.attachMessaging(messaging as any);
await engine.insert('crm_deal', { id: 'D_1', title: 'Big deal', organization_id: ORG }, { context: SYSTEM } as any);
await svc.openNodeRequest({
object: 'crm_deal', recordId: 'D_1', runId: 'run_1', nodeId: 'review', flowName: FLOW.name,
config: { approvers: [{ type: 'user', value: 'u_zh' }, { type: 'user', value: 'u_en' }], behavior: 'first_response' } as any,
submitterId: 'u_sub', record: { id: 'D_1', title: 'Big deal' },
}, asUser('u_sub'));
const bodyOf = async (userId: string) => {
const rows = (await engine.find('sys_inbox_message', { where: { user_id: userId }, fields: ['body_md'] }, { context: SYSTEM })) as any[];
return rows.map((r) => r.body_md);
};
return { en: await bodyOf('u_en'), zh: await bodyOf('u_zh'), i18n };
} finally {
try { await engine.destroy(); } catch { /* noop */ }
}
};

it("the defect's precondition: t() answers an en reader with the zh-CN row", () => {
expect(sourceLocaleI18n().t('objects.crm_deal.label', 'en')).toBe('商机');
});

it('an en approver reads the authored label; CONTROL: a zh-CN approver reads the zh-CN row', async () => {
const { en, zh } = await openOn();
expect(en).toEqual(['A decision on Deal Big deal is waiting on you.']);
expect(zh).toEqual(['商机 Big deal 正在等待您的审批决定。']);
});

it('a renamed packaged object is named by its rename, in every language, as /meta names it', async () => {
const { en, zh } = await openOn('Opportunity');
expect(en).toEqual(['A decision on Opportunity Big deal is waiting on you.']);
expect(zh).toEqual(['Opportunity Big deal 正在等待您的审批决定。']);
});
});
41 changes: 29 additions & 12 deletions packages/plugins/plugin-approvals/src/approval-notification-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,34 @@
* the one recipient the text is built for may read it, else the record id
* (`ApprovalService.notify` resolves it per recipient); the #23556 / #23594
* ruling B form, label and id for every reader, stands only on the
* reader-less action-link pages. The label rides as a keyed param — `objects.<object>.label`, the key the reader-language
* labels (`reader-locale-labels.ts`) and objectql's refusals name an object
* by — so a recipient whose language pack holds it reads it in their language;
* the schema label (the API name when there is none) stands otherwise.
* reader-less action-link pages. [#35384] The label rides as the messaging
* service's typed object-label param, `{ object: <object API name>, fallback }`,
* which it resolves per recipient as `GET /meta/object/:name` serves the
* object (`translateObject` with the metadata route's resolve options and the
* packaged base), the way the reader-language labels (`reader-locale-labels.ts`)
* name an object; so a recipient in the deployment's `sourceLocale` reads the
* authored label, a renamed packaged object its rename, and the schema label
* (the API name when there is none) stands when nothing resolves.
*/

import { objectLabelKey } from '@objectstack/spec/system';
import { enMessages, type ApprovalMessageId } from './translations/messages.js';

/** One `{{hole}}` value: the text itself, or a catalog key with the text it falls back to. */
export type ApprovalTextParam = string | { readonly key: string; readonly fallback: string };
/**
* One `{{hole}}` value: the text itself, a catalog key with the text it falls
* back to, or [#35384] an object's label by the object's name with the text it
* falls back to (the messaging service's `NotificationObjectLabelParam`).
*/
export type ApprovalTextParam =
| string
| { readonly key: string; readonly fallback: string }
| ApprovalObjectLabelParam;

/** [#35384] The structural mirror of the messaging service's `NotificationObjectLabelParam`. */
export interface ApprovalObjectLabelParam {
/** The object's API name, never its label. */
readonly object: string;
readonly fallback: string;
}

/**
* The keyed half — the structural mirror of the messaging service's
Expand All @@ -68,7 +85,7 @@ export interface ApprovalNotificationText {
* not an interface, so it spreads into a sentence's params as it stands.
*/
export type ApprovalSubjectParams = {
readonly object: { readonly key: string; readonly fallback: string };
readonly object: ApprovalObjectLabelParam;
readonly record: string;
};

Expand All @@ -78,9 +95,9 @@ export function approvalMessageKey(id: ApprovalMessageId): string {
}

/**
* The record of `objectName` a notification names: the object's label — keyed
* by `objects.<object>.label`, falling back to `objectLabel` (the schema
* label), else the API name — and `record`, the name its recipient reads for
* The record of `objectName` a notification names: the object's label — the
* typed object-label param naming `objectName`, falling back to `objectLabel`
* (the schema label), else the API name — and `record`, the name its recipient reads for
* it (its title as they may read it, else its id).
*/
export function subjectParams(
Expand All @@ -89,7 +106,7 @@ export function subjectParams(
record: string,
): ApprovalSubjectParams {
return {
object: { key: objectLabelKey(objectName), fallback: objectLabel ?? objectName },
object: { object: objectName, fallback: objectLabel ?? objectName },
record,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ const deliveredIn = (
const params: Record<string, string> = {};
for (const [name, param] of Object.entries(notice.params)) {
if (typeof param === 'string') { params[name] = param; continue; }
// [#35384] An object-label param is resolved off the object's metadata, which no notice here names.
if ('object' in param) { params[name] = param.fallback; continue; }
const looked = param.key.startsWith('messages.') ? catalog[param.key.slice('messages.'.length)] : pack[param.key];
params[name] = looked ?? param.fallback;
}
Expand Down
59 changes: 42 additions & 17 deletions packages/plugins/plugin-approvals/src/reader-locale-labels.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@ describe('[#24863] the pick', () => {

it('negotiates through `resolveBundleLocale` even for a service that resolves exactly', () => {
// A service that answers only the exact tag it holds, as `FileI18nAdapter` does.
// [#35384] Asked of the step's label, the one label still read through `t()`.
// [#35384] Asked of the step's label, now read off the service's translations.
const packs: Record<string, Record<string, unknown>> = { en: {}, 'zh-CN': packOf({ [approvalStepLabelKey('f', 'n')]: '步骤' }) };
const exact: ReaderLabelI18n = {
t: (key, locale) => (locale === 'zh-CN' && key === approvalStepLabelKey('f', 'n') ? '步骤' : key),
t: (key) => key,
getLocales: () => ['en', 'zh-CN'],
getTranslations: (locale) => packs[locale] ?? {},
};
expect(readerLabelsFor(exact, 'zh')!.stepLabel('f', 'n')).toBe('步骤');
});
Expand Down Expand Up @@ -144,13 +146,19 @@ describe('[#24863] the pick', () => {
expect(readerLabelsFor(undefined, 'zh-CN')).toBeUndefined();
});

it('asks the service once per key per read', () => {
it('asks the service once per read, and never through t()', () => {
const asked: string[] = [];
const counting: ReaderLabelI18n = { t: (key) => { asked.push(key); return key; } };
let reads = 0;
const counting: ReaderLabelI18n = {
t: (key) => { asked.push(key); return key; },
getLocales: () => ['zh-CN'],
getTranslations: () => { reads += 1; return packOf(ZH); },
};
const labels = readerLabelsFor(counting, 'zh-CN')!;
labels.stepLabel('f', 'n');
labels.stepLabel('f', 'n');
expect(asked).toEqual([approvalStepLabelKey('f', 'n')]);
expect(labels.stepLabel('account_approval', 'manager_review')).toBe('经理审核');
labels.stepLabel('account_approval', 'manager_review');
expect(reads).toBe(1);
expect(asked).toEqual([]);
});
});

Expand Down Expand Up @@ -285,11 +293,13 @@ describe('[#35384] the object label in the declared sourceLocale reads as author
});

/**
* [#35384] The flow's and the position's labels are metadata too, so they are
* resolved by the spec translators of their types — `translateFlow` (the
* flow runner's reading of `flows.<flow>.label`; `/meta` serves a flow
* document untranslated) and `translatePosition` (what `GET /meta/position`
* serves) — with `/meta`'s `ResolveOptions`, and never through `t()`.
* [#35384] The flow's, the approval step's and the position's labels are
* metadata too, so they are resolved by the spec readers of their keys —
* `translateFlow` (the flow runner's reading of `flows.<flow>.label`; `/meta`
* serves a flow document untranslated), `resolveFlowApprovalStepLabel` (the
* one reading of `flows.<flow>.approvals.<node_id>.label`) and
* `translatePosition` (what `GET /meta/position` serves) — with `/meta`'s
* `ResolveOptions`, and never through `t()`.
*
* The scenario: an app authored in English (`sourceLocale: 'en'`) that
* defaults its users to Chinese (`defaultLocale: 'zh-CN'`, declared fallback
Expand All @@ -301,7 +311,7 @@ describe('[#35384] the object label in the declared sourceLocale reads as author
* read by an `en` reader, and with no `sourceLocale` declared an `en` reader
* walks the declared fallback to the `zh-CN` rows.
*/
describe('[#35384] the flow and position labels in the declared sourceLocale read as authored', () => {
describe('[#35384] the flow, approval step and position labels in the declared sourceLocale read as authored', () => {
const EN = { [flowLabelKey('other_flow')]: 'Other flow' };
const declared = (sourceLocale: string | undefined, en: Record<string, string> = EN) => {
const i18n = i18nWith({ 'zh-CN': ZH, en });
Expand All @@ -320,6 +330,7 @@ describe('[#35384] the flow and position labels in the declared sourceLocale rea
it('the defect\'s precondition: t() answers an en reader with the zh-CN rows', () => {
const i18n = declared('en');
expect(i18n.t(flowLabelKey('account_approval'), 'en')).toBe('客户审批');
expect(i18n.t(approvalStepLabelKey('account_approval', 'manager_review'), 'en')).toBe('经理审核');
expect(i18n.t(positionLabelKey('crm_sales_manager'), 'en')).toBe('销售经理');
});

Expand All @@ -328,41 +339,55 @@ describe('[#35384] the flow and position labels in the declared sourceLocale rea
});

for (const locale of ['en', 'en-US']) {
it(`an ${locale} reader picks no defaultLocale row: the flow and position labels stand as authored`, () => {
it(`an ${locale} reader picks no defaultLocale row: the flow, step and position labels stand as authored`, () => {
const labels = readerLabelsFor(declared('en'), locale)!;
expect(labels.flowLabel('account_approval')).toBeUndefined();
expect(labels.stepLabel('account_approval', 'manager_review')).toBeUndefined();
expect(labels.positionLabel('crm_sales_manager')).toBeUndefined();
const row = served();
localizeRequestLabels(row, labels);
expect(row.process_label).toBe('Account Approval');
expect(row.step_label).toBe('Manager Review');
expect(row.current_step_label).toBe('Manager Review');
});
}

it('CONTROL: a zh-CN reader still reads the zh-CN rows', () => {
const labels = readerLabelsFor(declared('en'), 'zh-CN')!;
expect(labels.flowLabel('account_approval')).toBe('客户审批');
expect(labels.stepLabel('account_approval', 'manager_review')).toBe('经理审核');
expect(labels.positionLabel('crm_sales_manager')).toBe('销售经理');
});

it('CONTROL: an en pack entry is what an en and an en-US reader read', () => {
const en = { [flowLabelKey('account_approval')]: 'Account sign-off', [positionLabelKey('crm_sales_manager')]: 'Sales lead' };
const en = {
[flowLabelKey('account_approval')]: 'Account sign-off',
[approvalStepLabelKey('account_approval', 'manager_review')]: 'Manager sign-off',
[positionLabelKey('crm_sales_manager')]: 'Sales lead',
};
for (const locale of ['en', 'en-US']) {
const labels = readerLabelsFor(declared('en', en), locale)!;
expect(labels.flowLabel('account_approval')).toBe('Account sign-off');
expect(labels.stepLabel('account_approval', 'manager_review')).toBe('Manager sign-off');
expect(labels.positionLabel('crm_sales_manager')).toBe('Sales lead');
}
});

it('CONTROL: with no sourceLocale declared, an en reader walks the declared fallback to the zh-CN rows', () => {
const labels = readerLabelsFor(declared(undefined), 'en')!;
expect(labels.flowLabel('account_approval')).toBe('客户审批');
expect(labels.stepLabel('account_approval', 'manager_review')).toBe('经理审核');
expect(labels.positionLabel('crm_sales_manager')).toBe('销售经理');
});

it('reads neither label through t(): a service that cannot hand its translations over picks none', () => {
const tOnly: ReaderLabelI18n = { t: (key) => (key === flowLabelKey('f') ? '流程' : key === positionLabelKey('p') ? '岗位' : key) };
it('reads no label through t(): a service that cannot hand its translations over picks none', () => {
const tOnly: ReaderLabelI18n = {
t: (key) => (key === flowLabelKey('f') ? '流程' : key === positionLabelKey('p') ? '岗位'
: key === approvalStepLabelKey('f', 'n') ? '步骤' : key),
};
const labels = readerLabelsFor(tOnly, 'zh-CN')!;
expect(labels.flowLabel('f')).toBeUndefined();
expect(labels.stepLabel('f', 'n')).toBeUndefined();
expect(labels.positionLabel('p')).toBeUndefined();
});

Expand Down
Loading
Loading