diff --git a/.changeset/38092-spaced-translation-holes.md b/.changeset/38092-spaced-translation-holes.md new file mode 100644 index 00000000000..22df3697269 --- /dev/null +++ b/.changeset/38092-spaced-translation-holes.md @@ -0,0 +1,11 @@ +--- +'@objectstack/core': minor +'@objectstack/service-i18n': minor +--- + +fix(i18n): server t() fills {{ name }} holes written with spaces, as the Console does + +Clause-②: yes (widening) +Metadata-protocol: semantic + +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. diff --git a/packages/core/src/fallbacks/fallbacks.test.ts b/packages/core/src/fallbacks/fallbacks.test.ts index c00ea462c59..08ba8dbd601 100644 --- a/packages/core/src/fallbacks/fallbacks.test.ts +++ b/packages/core/src/fallbacks/fallbacks.test.ts @@ -224,6 +224,13 @@ describe('createMemoryI18n', () => { expect(i18n.t('greeting', 'en', { name: 'World' })).toBe('Hello, World!'); }); + it('fills a hole written with spaces inside the braces, as the Console does', () => { + const i18n = createMemoryI18n(); + i18n.loadTranslations('en', { greeting: 'Hello, {{ name }}!', missing: 'Hello, {{ who }}!' }); + expect(i18n.t('greeting', 'en', { name: 'World' })).toBe('Hello, World!'); + expect(i18n.t('missing', 'en', { name: 'World' })).toBe('Hello, {{ who }}!'); + }); + it('should fall back to default locale', () => { const i18n = createMemoryI18n(); i18n.loadTranslations('en', { hello: 'Hello' }); diff --git a/packages/core/src/fallbacks/memory-i18n.ts b/packages/core/src/fallbacks/memory-i18n.ts index c3efb8ed37b..ce76a77706b 100644 --- a/packages/core/src/fallbacks/memory-i18n.ts +++ b/packages/core/src/fallbacks/memory-i18n.ts @@ -184,8 +184,10 @@ export function createMemoryI18n() { if (value == null) return key; if (!params) return value; - // Interpolation format: {{paramName}} — matches FileI18nAdapter convention - return value.replace(/\{\{(\w+)\}\}/g, (_, name) => String(params[name] ?? `{{${name}}}`)); + // Interpolation format: {{paramName}}, inner whitespace allowed + // ({{ paramName }}) as the Console's i18next reads the same string — + // matches FileI18nAdapter. A hole with no value stays exactly as written. + return value.replace(/\{\{\s*(\w+)\s*\}\}/g, (hole, name) => (params[name] == null ? hole : String(params[name]))); }, getTranslations(locale: string): Record { diff --git a/packages/services/service-i18n/src/file-i18n-adapter.test.ts b/packages/services/service-i18n/src/file-i18n-adapter.test.ts index 4902105fbc1..86c1b6dd119 100644 --- a/packages/services/service-i18n/src/file-i18n-adapter.test.ts +++ b/packages/services/service-i18n/src/file-i18n-adapter.test.ts @@ -590,3 +590,29 @@ describe('the retired `messageFormat` guidance names the placeholder `t()` fills expect(i18n.t('greeting', 'en', { variable: 'Ada' })).toBe('Hello, {variable}!'); }); }); + +describe('both server `t()` fill a bundle string the way the Console does', () => { + // One bundle string feeds the server `t()` and the Console's i18next, so a + // hole the Console fills must be filled here too, or a server message shows + // the braces. Each `console` value below is what i18next (the Console's + // default `{{ }}` interpolation) answered for the same template and params. + const servers: Array<[string, () => II18nService]> = [ + ['FileI18nAdapter', () => new FileI18nAdapter()], + ['createMemoryI18n', () => createMemoryI18n() as unknown as II18nService], + ]; + const forms: 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 }}!'], + ]; + + for (const [server, make] of servers) { + it.each(forms)(`${server}: %s`, (_form, template, console) => { + const i18n = make(); + i18n.loadTranslations('en', { greeting: template }); + expect(i18n.t('greeting', 'en', { name: 'Ada' })).toBe(console); + }); + } +}); diff --git a/packages/services/service-i18n/src/file-i18n-adapter.ts b/packages/services/service-i18n/src/file-i18n-adapter.ts index 5f8a571b2fa..ec80b9ce309 100644 --- a/packages/services/service-i18n/src/file-i18n-adapter.ts +++ b/packages/services/service-i18n/src/file-i18n-adapter.ts @@ -78,15 +78,19 @@ export function deepMerge( /** * Interpolate parameters into a translated string. - * Replaces `{{paramName}}` with the corresponding value from params. + * Replaces `{{paramName}}` with the corresponding value from params. Whitespace + * inside the braces is allowed (`{{ paramName }}`), as the Console's i18next + * reads the same bundle string, so one translation renders the same on both + * sides. A placeholder with no value is left exactly as written, spaces + * included. Single braces (`{paramName}`) are not a placeholder. * * @param template - Template string with `{{key}}` placeholders * @param params - Parameter map * @returns Interpolated string */ function interpolate(template: string, params: Record): string { - return template.replace(/\{\{(\w+)\}\}/g, (_match, key: string) => { - return params[key] != null ? String(params[key]) : `{{${key}}}`; + return template.replace(/\{\{\s*(\w+)\s*\}\}/g, (match, key: string) => { + return params[key] != null ? String(params[key]) : match; }); } @@ -99,7 +103,7 @@ function interpolate(template: string, params: Record): string * * Supports: * - Dot-notation key resolution (e.g. 'objects.account.label') - * - Parameter interpolation via `{{paramName}}` syntax + * - Parameter interpolation via `{{paramName}}` syntax (inner whitespace allowed) * - Fallback locale for missing translations * - Runtime translation loading via loadTranslations() *