diff --git a/.changeset/38266-spaced-override-holes.md b/.changeset/38266-spaced-override-holes.md new file mode 100644 index 00000000000..29bd5a5150a --- /dev/null +++ b/.changeset/38266-spaced-override-holes.md @@ -0,0 +1,10 @@ +--- +'@objectstack/spec': minor +--- + +fix(spec): a translated validation or operation message fills {{ label }} holes written with spaces, as the Console does + +Clause-②: yes (widening) +Metadata-protocol: semantic + +A deployment that overrides a built-in validation message (`validation.field.`) or operation message (`errors.`) in a translation can now write a placeholder with spaces inside the braces, `{{ label }}`, and the server fills it. Before, `interpolateValidationMessage` (used by `renderValidationMessage` and `renderOperationMessage`) filled only `{{label}}`, so the same translation showed a value in the Console and raw braces in a 400 or 409 answer, such as an import row refusal or a duplicate-record error. Whitespace (spaces or tabs) between the braces and the name is now allowed. A placeholder with no value is still left exactly as written, spaces included, and single braces (`{label}`) are still not a placeholder. The built-in messages render byte-for-byte as before, and no change is needed in your bundles: `{{label}}` works as before. diff --git a/packages/spec/src/system/operation-message.test.ts b/packages/spec/src/system/operation-message.test.ts index 5698f75f4e6..3bb526674cb 100644 --- a/packages/spec/src/system/operation-message.test.ts +++ b/packages/spec/src/system/operation-message.test.ts @@ -66,6 +66,27 @@ describe('operation message catalog', () => { .toBe('不能删除:还有 1 条下级记录。'); }); + it('a deployment override written with spaces inside the braces is filled, as the Console fills it', () => { + const translate = (key: string) => + key === 'errors.duplicate_record_field' ? '{{ field }} taken' : key; + expect(renderOperationMessage({ messageKey: 'duplicate_record_field', params: { field: 'Email' } }, { translate })) + .toBe('Email taken'); + }); + + it.each([ + ['no spaces', 'Hi {{name}}!', 'Hi Ada!'], + ['spaces inside the braces', 'Hi {{ name }}!', 'Hi Ada!'], + ['a tab and two spaces', 'Hi {{ name\t}}!', 'Hi Ada!'], + ['single braces (not a placeholder)', 'Hi {name}!', 'Hi {name}!'], + ['a spaced hole with no value', 'Hi {{ missing }}!', 'Hi {{ missing }}!'], + ])('a deployment override is filled the way the Console does: %s', (_form, template, console) => { + // Each expected value is what the Console's i18next answers for the same + // template and params; a hole with no value stays exactly as written. + const translate = (key: string) => (key === 'errors.duplicate_record_field' ? template : key); + expect(renderOperationMessage({ messageKey: 'duplicate_record_field', params: { name: 'Ada' } }, { translate })) + .toBe(console); + }); + it('an override key that misses (echoed back) falls through to the built-in', () => { const translate = (key: string) => key; expect(renderOperationMessage({ messageKey: 'delete_restricted', params: PARAMS }, { locale: 'zh-CN', translate })) diff --git a/packages/spec/src/system/operation-message.ts b/packages/spec/src/system/operation-message.ts index 7dac498e598..516abd6633e 100644 --- a/packages/spec/src/system/operation-message.ts +++ b/packages/spec/src/system/operation-message.ts @@ -114,10 +114,12 @@ * * ## Interpolation * - * `{{name}}` placeholders, matching `II18nService.t()`'s convention. An unknown - * placeholder is left verbatim so a broken override is visible rather than - * silently blank — {@link interpolateValidationMessage} is shared with the - * sibling catalog so the two cannot drift on this. + * `{{name}}` placeholders, matching `II18nService.t()`'s convention; whitespace + * inside the braces is allowed (`{{ name }}`), as the Console reads the same + * translation string. An unknown placeholder is left exactly as written so a + * broken override is visible rather than silently blank — + * {@link interpolateValidationMessage} is shared with the sibling catalog so + * the two cannot drift on this. */ import { resolveBundleLocale } from './i18n-resolver'; diff --git a/packages/spec/src/system/validation-message.override-once.test.ts b/packages/spec/src/system/validation-message.override-once.test.ts index 84620d83681..ee7284d50d9 100644 --- a/packages/spec/src/system/validation-message.override-once.test.ts +++ b/packages/spec/src/system/validation-message.override-once.test.ts @@ -14,9 +14,9 @@ import { * field label by a second pass. * * The translator is shaped as `FileI18nAdapter.t` answers: handed `params`, it - * fills `{{name}}` itself. Asked for the template with params, its answer would - * already carry the user's `{{label}}`, and the renderer's own pass would then - * expand it. Asked without params, it returns the template and the renderer + * fills `{{name}}` itself (inner whitespace allowed). Asked for the template + * with params, its answer would already carry the user's `{{label}}`, and the + * renderer's own pass would then expand it. Asked without params, it returns the template and the renderer * fills it exactly once. */ describe('renderValidationMessage — a deployment override is interpolated once', () => { @@ -25,7 +25,7 @@ describe('renderValidationMessage — a deployment override is interpolated once const interpolatingTranslate = (k: string, _locale: string, params?: Record) => { if (k !== key) return k; return params && Object.keys(params).length > 0 - ? overrideTemplate.replace(/\{\{(\w+)\}\}/g, (m, name: string) => (params[name] != null ? String(params[name]) : m)) + ? overrideTemplate.replace(/\{\{\s*(\w+)\s*\}\}/g, (m, name: string) => (params[name] != null ? String(params[name]) : m)) : overrideTemplate; }; const written = '{{label}} plan'; @@ -41,6 +41,15 @@ describe('renderValidationMessage — a deployment override is interpolated once .toBe(`套餐的取值「${written}」无效,请从 basic, pro 中选择`); }); + it('prints a written value containing a spaced placeholder as written on the override path', () => { + // The renderer now reads `{{ label }}` as a placeholder in a TEMPLATE; a + // value that carries one must still be printed as written, not expanded. + const spacedWritten = '{{ label }} plan'; + const spacedInput = { ...input, params: { value: spacedWritten, allowed: 'basic, pro' } }; + expect(renderValidationMessage(spacedInput, { locale: 'zh-CN', translate: interpolatingTranslate })) + .toBe(`套餐的取值「${spacedWritten}」无效,请从 basic, pro 中选择`); + }); + it('CONTROL — the built-in path, which was already single-pass, prints it as written too', () => { expect(renderValidationMessage(input, { locale: 'zh-CN' })).toBe( BUILTIN_VALIDATION_MESSAGES['zh-CN'].invalid_option_value diff --git a/packages/spec/src/system/validation-message.test.ts b/packages/spec/src/system/validation-message.test.ts index f63ea541faf..814296246ad 100644 --- a/packages/spec/src/system/validation-message.test.ts +++ b/packages/spec/src/system/validation-message.test.ts @@ -224,6 +224,36 @@ describe('interpolateValidationMessage', () => { expect(renderValidationMessage({ messageKey: 'no_such_key', label: 'Qty' })) .toBe('Qty (no_such_key)'); }); + + // One translation string feeds this renderer, the server `t()` and the + // Console's i18next, so a hole the Console fills must be filled here too, or + // a 400 shows the braces. Each expected value is what i18next (the Console's + // default `{{ }}` interpolation) answers for the same template and params. + const spellings: Array<[string, string, string]> = [ + ['no spaces', 'Hi {{name}}!', 'Hi Ada!'], + ['spaces inside the braces', 'Hi {{ name }}!', 'Hi Ada!'], + ['a tab and two spaces', 'Hi {{ name\t}}!', 'Hi Ada!'], + ['single braces (not a placeholder)', 'Hi {name}!', 'Hi {name}!'], + ['a spaced hole with no value', 'Hi {{ missing }}!', 'Hi {{ missing }}!'], + ]; + + it.each(spellings)('fills the way the Console does: %s', (_form, template, console) => { + expect(interpolateValidationMessage(template, { name: 'Ada' })).toBe(console); + }); + + it.each(spellings)('a deployment override is filled the way the Console does: %s', (_form, template, console) => { + const key = validationMessageTranslationKey('required'); + const translate = (k: string) => (k === key ? template : k); + expect(renderValidationMessage({ messageKey: 'required', label: 'Qty', params: { name: 'Ada' } }, { translate })) + .toBe(console); + }); + + it('a spaced override names the field label, as the import runner renders it', () => { + const key = validationMessageTranslationKey('required'); + const translate = (k: string) => (k === key ? '{{ label }} is needed' : k); + expect(renderValidationMessage({ messageKey: 'required', label: 'Qty' }, { translate })) + .toBe('Qty is needed'); + }); }); /** diff --git a/packages/spec/src/system/validation-message.ts b/packages/spec/src/system/validation-message.ts index 4b1ba4c832e..965a5ad4af9 100644 --- a/packages/spec/src/system/validation-message.ts +++ b/packages/spec/src/system/validation-message.ts @@ -111,8 +111,11 @@ * in the caller's locale) and `{{field}}` (its API name). A few are filled from * the refusing door's message-only parameters, which name a fact of the * platform rather than a constraint of the field and are never shipped on the - * error (`{{firstYear}}` / `{{lastYear}}` above). An unknown placeholder is - * left verbatim so a broken override is visible rather than silently blank. + * error (`{{firstYear}}` / `{{lastYear}}` above). Whitespace inside the braces + * is allowed (`{{ label }}`), as the Console's i18next and the server `t()` + * read the same translation string, so a deployment override renders the same + * on both sides. An unknown placeholder is left exactly as written (spaces + * included) so a broken override is visible rather than silently blank. */ import { resolveBundleLocale } from './i18n-resolver'; @@ -380,16 +383,19 @@ export const BUILTIN_VALIDATION_MESSAGES: Record> export const VALIDATION_MESSAGE_FALLBACK_LOCALE = 'en'; /** - * Fill `{{name}}` placeholders from `params`. An unresolved placeholder is left - * verbatim (`{{min}}`) rather than blanked, so a malformed override or a missing - * param is visible in the message instead of producing "must be ≥ .". + * Fill `{{name}}` placeholders from `params`. Whitespace inside the braces is + * allowed (`{{ name }}`), matching the Console's i18next and the server `t()`; + * single braces (`{name}`) are not a placeholder. An unresolved placeholder is + * left exactly as written (`{{min}}`, `{{ min }}`) rather than blanked, so a + * malformed override or a missing param is visible in the message instead of + * producing "must be ≥ .". */ export function interpolateValidationMessage( template: string, params: Record | undefined, ): string { if (!params) return template; - return template.replace(/\{\{(\w+)\}\}/g, (whole, name: string) => { + return template.replace(/\{\{\s*(\w+)\s*\}\}/g, (whole, name: string) => { const value = params[name]; return value === undefined || value === null ? whole : String(value); });