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
11 changes: 11 additions & 0 deletions .changeset/38092-spaced-translation-holes.md
Original file line number Diff line number Diff line change
@@ -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.
7 changes: 7 additions & 0 deletions packages/core/src/fallbacks/fallbacks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/fallbacks/memory-i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown> {
Expand Down
26 changes: 26 additions & 0 deletions packages/services/service-i18n/src/file-i18n-adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
});
12 changes: 8 additions & 4 deletions packages/services/service-i18n/src/file-i18n-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, unknown>): 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;
});
}

Expand All @@ -99,7 +103,7 @@ function interpolate(template: string, params: Record<string, unknown>): 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()
*
Expand Down
Loading