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 <noreply@anthropic.com>
This commit is contained in:
Асель 2026-11-15 15:46:46 +05:00
parent 905aeb912d
commit b479f23db4
3 changed files with 11 additions and 2 deletions

View File

@ -285,7 +285,7 @@ async function MessageItem({ m, q, back, showThread, masterView, mine = false }:
return (
<article className={cn("flex flex-col gap-1 rounded-lg border bg-card px-3 py-2", m.kind === "user" && "border-primary/30 bg-primary/5", m.kind === "system" && "border-dashed bg-transparent")}>
{head}
<p className="m-0 text-sm break-words whitespace-pre-wrap">{t.text(m.text)}</p>
<p className="m-0 text-sm break-words whitespace-pre-wrap">{m.authorKind === "system" ? t.text(m.text) : m.text}</p>
{m.url && (
<a href={m.url} target="_blank" rel="noreferrer" className="truncate text-xs">
{m.url}

View File

@ -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<string, string> = {};
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);
}
}

View File

@ -62,6 +62,8 @@ describe("язык интерфейса", () => {
assert.equal(localizeText("en", "<b>Проекты</b>\n✅ Принять"), "<b>Projects</b>\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 файла");