From b479f23db4ed3fcf9d18c44cca690ec4e9d364e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=81=D0=B5=D0=BB=D1=8C?= Date: Sun, 15 Nov 2026 15:46:46 +0500 Subject: [PATCH] PEP-98: people's and agents' text is never cut by loose templates; chat bodies translated only for system messages Co-Authored-By: Claude Opus 5.5 --- apps/web/app/(app)/chats/[chat]/page.tsx | 2 +- packages/shared/src/i18n/index.ts | 9 ++++++++- packages/testkit/test/i18n.test.ts | 2 ++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/apps/web/app/(app)/chats/[chat]/page.tsx b/apps/web/app/(app)/chats/[chat]/page.tsx index 181b01be..e5255c54 100644 --- a/apps/web/app/(app)/chats/[chat]/page.tsx +++ b/apps/web/app/(app)/chats/[chat]/page.tsx @@ -285,7 +285,7 @@ async function MessageItem({ m, q, back, showThread, masterView, mine = false }: return (
{head} -

{t.text(m.text)}

+

{m.authorKind === "system" ? t.text(m.text) : m.text}

{m.url && ( {m.url} diff --git a/packages/shared/src/i18n/index.ts b/packages/shared/src/i18n/index.ts index 3fed928c..961d6a81 100644 --- a/packages/shared/src/i18n/index.ts +++ b/packages/shared/src/i18n/index.ts @@ -112,6 +112,8 @@ interface Template { names: string[]; out: string; weight: number; + /** Starts with a placeholder: anchored only by text in the middle ("{a} из {b}"). */ + loose: boolean; } const CYR = /[А-Яа-яЁё]/; @@ -142,7 +144,7 @@ function compile(locale: Locale) { } } if (weight < 2) continue; // "{a} {b}" would match anything - const tpl: Template = { re: new RegExp(src + "$"), names, out, weight }; + const tpl: Template = { re: new RegExp(src + "$"), names, out, weight, loose: parts[0] === "" }; const head = parts[0]!.slice(0, 2); if (head.length === 2) { const list = c.byHead.get(head) ?? []; @@ -164,10 +166,15 @@ function matchTemplate(locale: Locale, s: string, depth: number): string | undef const mt = tpl.re.exec(s); if (!mt) continue; const vars: Record = {}; + let ok = true; tpl.names.forEach((name, i) => { const v = mt[i + 1]!; vars[name] = depth < 2 ? (piece(locale, v, depth + 1) ?? v) : v; + // A template anchored only in the middle must not cut free Russian text (a person's or an + // agent's sentence "Какой формат из двух?" is not "{done} из {total}"). + if (tpl.loose && CYR.test(vars[name]!)) ok = false; }); + if (!ok) continue; return fill(tpl.out, vars); } } diff --git a/packages/testkit/test/i18n.test.ts b/packages/testkit/test/i18n.test.ts index 4009c916..5829ef55 100644 --- a/packages/testkit/test/i18n.test.ts +++ b/packages/testkit/test/i18n.test.ts @@ -62,6 +62,8 @@ describe("язык интерфейса", () => { assert.equal(localizeText("en", "Проекты\n✅ Принять"), "Projects\n✅ Accept"); assert.equal(localizeText("en", "Тестовая стори PEP-7 в статусе в работе"), "Test story PEP-7 is in progress"); assert.equal(localizeText("en", "Совсем неизвестная строка"), "Совсем неизвестная строка"); + // Free text of a person or an agent is not cut by a template anchored only in the middle. + assert.equal(localizeText("en", "Какой формат из двух?"), "Какой формат из двух?"); assert.equal(localizeText("ru", "Проекты"), "Проекты"); assert.equal(tn("en", 1, "{n} вопросов ждут тебя", "{n} вопросов ждут тебя", "{n} вопросов ждут тебя"), "1 questions waiting for you"); assert.equal(tn("ru", 3, "{n} файл", "{n} файла", "{n} файлов"), "3 файла");