Skip to content

Commit d085e6c

Browse files
fix(i18n): server t() fills {{ name }} holes written with spaces, as the Console does (#38265)
<!-- claude-projects-attribution: {"github_login":"os-tesla"} --> _Requested by **Tesla** · [project thread](https://claude.ai/code/project/chan_01AXsQkLFKRdQCqQ3RTr6Jao?thread=cmsg_01AXsQkLFKRdQCqQ3RTr6Jao5sbi7ZRGktseLEY4Auk4YX)_ Fixes #38092 repo:objectstack#3 · os-tesla · session_01L9qibyUSf2nKJWW6qBemSU Clause-②: yes (widening) Metadata-protocol: semantic Skills-docs: none — skills/objectstack-i18n already teaches the {{variable}} form; the spaced form is tolerated to match the Console, not a new form to teach ## 维护者速读 - 翻译里把占位符写成花括号内带空格的形式时,服务端发出的通知和报错现在会填上值,和控制台显示一致;以前服务端原样印出花括号。 - 替写翻译的搭建者解决:同一句翻译在控制台正常、在服务端消息里露出花括号。 - 只改服务端两处翻译替换的规则,不带空格的写法照旧;出问题最多是某句消息填值不对,可以直接撤回。 - 无 ## What changes - `packages/services/service-i18n/src/file-i18n-adapter.ts`: `interpolate` matches `{{`, optional whitespace, a word, optional whitespace, `}}`, so `{{ name }}` and tab-spaced holes are filled like `{{name}}`. A hole with no value is written back exactly as matched (`{{ missing }}`), where the old code re-spelled it as `{{missing}}`. Docblocks say so. - `packages/core/src/fallbacks/memory-i18n.ts`: the in-memory fallback `t()` gets the same pattern and the same write-back. - Single braces (`{name}`) are still not a placeholder on either side. - Changeset: `@objectstack/core`, `@objectstack/service-i18n` minor (`Metadata-protocol: semantic`: on the server a translation string's `{{ name }}` now means a placeholder instead of literal text). Not changed: `packages/spec` (the validation / operation message override interpolator has the same gap; the seat files it as its own card), ui, locale packs, HotCRM, skills. ## Tests - `packages/services/service-i18n/src/file-i18n-adapter.test.ts`, a new top-level `describe`: the same bundle string goes to `FileI18nAdapter.t()` and `createMemoryI18n().t()`, and each answer must equal what the Console's i18next answered for that template (measured, table below): `{{name}}`, `{{ name }}`, a tab plus two spaces, single braces, and a spaced hole with no value. - `packages/core/src/fallbacks/fallbacks.test.ts`: one test in core's own suite (`{{ name }}` filled, a spaced hole with no value kept as written), so core alone catches a regression. Per-form readings (params `{ name: 'Ada' }`): | Template | Console (i18next 26.4.0) | main `FileI18nAdapter` / `createMemoryI18n` | this PR, both | |:--|:--|:--|:--| | `Hi {{name}}!` | `Hi Ada!` | `Hi Ada!` | `Hi Ada!` | | `Hi {{ name }}!` | `Hi Ada!` | `Hi {{ name }}!` | `Hi Ada!` | | tab and two spaces | `Hi Ada!` | as written | `Hi Ada!` | | `Hi {name}!` | `Hi {name}!` | `Hi {name}!` | `Hi {name}!` | | `Hi {{ missing }}!` | `Hi {{ missing }}!` | `Hi {{ missing }}!` | `Hi {{ missing }}!` | ## Acceptance notes - Premise measured on `origin/main` `bd5407ec` before any edit: a probe fed one template to real i18next 26.4.0 (the Console's dependency, its default `{{ }}` interpolation), `createMemoryI18n().t()` and `FileI18nAdapter.t()`. Readings are the "Console" and "main" columns above. - Same gap, not fixed here (seat ruling, claim 7237673531): `interpolateValidationMessage` in `packages/spec/src/system/validation-message.ts` fills translated validation and operation message overrides and still reads only `{{label}}`. The probe measured a `{{ label }}` override printed as written. The seat files it as a separate card. - Three more copies of the old pattern fill only the platform's own English catalogs, which carry no spaced holes, so no author reaches them: `plugin-audit` `activity-summary-message.ts`, `plugin-approvals` `approval-notification-text.ts`, core `security/change-record-owner.ts`. Their docblocks say they follow "the convention both `t()` use", which is now only true for unspaced holes. Left as is. - One widening, nothing narrowed: every string that rendered before renders the same, except that a spaced hole with a value is now filled. ## Verification - Two-minute check: `pnpm --filter @objectstack/service-i18n exec vitest run src/file-i18n-adapter.test.ts` shows 61 passed, 10 of them the new block. - Read first: `interpolate` in `file-i18n-adapter.ts`, then the `t()` return line in `memory-i18n.ts`. <details><summary>Local readings at cc1fe06</summary> - `pnpm --filter @objectstack/service-i18n test`: Test Files 4 passed, Tests 87 passed; `typecheck` exit 0. - `pnpm --filter @objectstack/core typecheck && pnpm --filter @objectstack/core test`: Test Files 158 passed, Tests 3807 passed. - Ablations through `scripts/ablation-replace.mjs`, each restore blob == HEAD and `git diff HEAD` empty: - adapter pattern reverted: 2 failed (`FileI18nAdapter` spaced, tab). - memory pattern reverted, core rebuilt and marker read in `dist/`: core suite 1 failed; adapter suite 2 failed (`createMemoryI18n` spaced, tab). After restore and a forced core rebuild, `dist/index.js` carries the new pattern on the memory line. - adapter write-back re-spelled as `{{key}}`: 1 failed (spaced hole with no value). - `dispatch-gates --ran` with recorded exit codes (`gates-ran-cc1fe06c.list`): 67 derived, 65 exit 0, 2 NOT MEASURED (exit 3: `check-plugin-teardown-shape --self-test`, fixture commit outside the shallow clone; `check:dual-build-cjs-loads`, needs a full `pnpm build`), 0 unrun. `check:lean-entry-closure` exit 0 after an objectql build. - `overlap-check.mjs` VERDICT clean exit 0 against origin/main 98bd8de. </details> --- _Generated by [Claude Code](https://claude.ai/code/session_01L9qibxWAZYhFuv668TruUa)_ Co-authored-by: Claude <noreply@anthropic.com>
1 parent baa38c0 commit d085e6c

5 files changed

Lines changed: 56 additions & 6 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
'@objectstack/core': minor
3+
'@objectstack/service-i18n': minor
4+
---
5+
6+
fix(i18n): server t() fills {{ name }} holes written with spaces, as the Console does
7+
8+
Clause-②: yes (widening)
9+
Metadata-protocol: semantic
10+
11+
Server-side translations now fill a placeholder written with spaces inside the braces, `{{ name }}`, the same way the Console does. Before, `FileI18nAdapter.t()` and the kernel's in-memory `createMemoryI18n().t()` filled only `{{name}}` and printed `{{ name }}` as written, so one translation showed a value in the Console and raw braces in a server message (a notification, an error). Whitespace (spaces or tabs) between the braces and the name is now allowed on the server too. A placeholder with no value is still left exactly as written, spaces included, and single braces (`{name}`) are still not a placeholder. No change is needed in your bundles: `{{name}}` works as before.

‎packages/core/src/fallbacks/fallbacks.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,13 @@ describe('createMemoryI18n', () => {
224224
expect(i18n.t('greeting', 'en', { name: 'World' })).toBe('Hello, World!');
225225
});
226226

227+
it('fills a hole written with spaces inside the braces, as the Console does', () => {
228+
const i18n = createMemoryI18n();
229+
i18n.loadTranslations('en', { greeting: 'Hello, {{ name }}!', missing: 'Hello, {{ who }}!' });
230+
expect(i18n.t('greeting', 'en', { name: 'World' })).toBe('Hello, World!');
231+
expect(i18n.t('missing', 'en', { name: 'World' })).toBe('Hello, {{ who }}!');
232+
});
233+
227234
it('should fall back to default locale', () => {
228235
const i18n = createMemoryI18n();
229236
i18n.loadTranslations('en', { hello: 'Hello' });

‎packages/core/src/fallbacks/memory-i18n.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,10 @@ export function createMemoryI18n() {
184184

185185
if (value == null) return key;
186186
if (!params) return value;
187-
// Interpolation format: {{paramName}} — matches FileI18nAdapter convention
188-
return value.replace(/\{\{(\w+)\}\}/g, (_, name) => String(params[name] ?? `{{${name}}}`));
187+
// Interpolation format: {{paramName}}, inner whitespace allowed
188+
// ({{ paramName }}) as the Console's i18next reads the same string —
189+
// matches FileI18nAdapter. A hole with no value stays exactly as written.
190+
return value.replace(/\{\{\s*(\w+)\s*\}\}/g, (hole, name) => (params[name] == null ? hole : String(params[name])));
189191
},
190192

191193
getTranslations(locale: string): Record<string, unknown> {

‎packages/services/service-i18n/src/file-i18n-adapter.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,3 +590,29 @@ describe('the retired `messageFormat` guidance names the placeholder `t()` fills
590590
expect(i18n.t('greeting', 'en', { variable: 'Ada' })).toBe('Hello, {variable}!');
591591
});
592592
});
593+
594+
describe('both server `t()` fill a bundle string the way the Console does', () => {
595+
// One bundle string feeds the server `t()` and the Console's i18next, so a
596+
// hole the Console fills must be filled here too, or a server message shows
597+
// the braces. Each `console` value below is what i18next (the Console's
598+
// default `{{ }}` interpolation) answered for the same template and params.
599+
const servers: Array<[string, () => II18nService]> = [
600+
['FileI18nAdapter', () => new FileI18nAdapter()],
601+
['createMemoryI18n', () => createMemoryI18n() as unknown as II18nService],
602+
];
603+
const forms: Array<[string, string, string]> = [
604+
['no spaces', 'Hi {{name}}!', 'Hi Ada!'],
605+
['spaces inside the braces', 'Hi {{ name }}!', 'Hi Ada!'],
606+
['a tab and two spaces', 'Hi {{ name\t}}!', 'Hi Ada!'],
607+
['single braces (not a placeholder)', 'Hi {name}!', 'Hi {name}!'],
608+
['a spaced hole with no value', 'Hi {{ missing }}!', 'Hi {{ missing }}!'],
609+
];
610+
611+
for (const [server, make] of servers) {
612+
it.each(forms)(`${server}: %s`, (_form, template, console) => {
613+
const i18n = make();
614+
i18n.loadTranslations('en', { greeting: template });
615+
expect(i18n.t('greeting', 'en', { name: 'Ada' })).toBe(console);
616+
});
617+
}
618+
});

‎packages/services/service-i18n/src/file-i18n-adapter.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,19 @@ export function deepMerge(
7878

7979
/**
8080
* Interpolate parameters into a translated string.
81-
* Replaces `{{paramName}}` with the corresponding value from params.
81+
* Replaces `{{paramName}}` with the corresponding value from params. Whitespace
82+
* inside the braces is allowed (`{{ paramName }}`), as the Console's i18next
83+
* reads the same bundle string, so one translation renders the same on both
84+
* sides. A placeholder with no value is left exactly as written, spaces
85+
* included. Single braces (`{paramName}`) are not a placeholder.
8286
*
8387
* @param template - Template string with `{{key}}` placeholders
8488
* @param params - Parameter map
8589
* @returns Interpolated string
8690
*/
8791
function interpolate(template: string, params: Record<string, unknown>): string {
88-
return template.replace(/\{\{(\w+)\}\}/g, (_match, key: string) => {
89-
return params[key] != null ? String(params[key]) : `{{${key}}}`;
92+
return template.replace(/\{\{\s*(\w+)\s*\}\}/g, (match, key: string) => {
93+
return params[key] != null ? String(params[key]) : match;
9094
});
9195
}
9296

@@ -99,7 +103,7 @@ function interpolate(template: string, params: Record<string, unknown>): string
99103
*
100104
* Supports:
101105
* - Dot-notation key resolution (e.g. 'objects.account.label')
102-
* - Parameter interpolation via `{{paramName}}` syntax
106+
* - Parameter interpolation via `{{paramName}}` syntax (inner whitespace allowed)
103107
* - Fallback locale for missing translations
104108
* - Runtime translation loading via loadTranslations()
105109
*

0 commit comments

Comments
 (0)