diff --git a/changelog.d/3114-room-invite-card-zh.fixed.md b/changelog.d/3114-room-invite-card-zh.fixed.md new file mode 100644 index 000000000..9759d7020 --- /dev/null +++ b/changelog.d/3114-room-invite-card-zh.fixed.md @@ -0,0 +1 @@ +Pasted room-invite cards now read Chinese in a zh window: the lead line, the people line, the countdown, the two clock answers, the Clear button and the nameless fallback. A card already on screen follows a language switch, and the room name, counts and clock the server sent stay as they came ([#3114](https://github.com/ThinkFlowLab/FlyBot/issues/3114)). diff --git a/core/client.js b/core/client.js index d34c634bd..9a9bfa9a0 100644 --- a/core/client.js +++ b/core/client.js @@ -151,6 +151,14 @@ var ZH = { "Scan with FlyBot to join this group": "用 FlyBot 扫码加入这个群", "Copy link": "复制链接", "{time} left to join": "还剩 {time} 可加入", + // #3114: the room-invite preview card, which is drawn wherever an invite is pasted. `This link + // has expired` is its own sentence, not the `This link has expired.` above: a card that borrowed + // that entry would draw a full stop the English card never had. + "Lead: {lead}": "总负责人:{lead}", + "{members} in the room": "房间里 {members} 人", + "{members} / {max} in the room": "房间里 {members} / {max} 人", + "This link has expired": "这条链接已过期", + "Open while the room is open": "房间开着的时候都能加入", "Works until you withdraw it.": "在你撤回之前一直有效。", "Withdraw link": "撤回链接", "1 earlier link still works.": "还有 1 个之前的链接仍然有效。", @@ -3514,27 +3522,42 @@ function invitePreviewOf(seen) { const people2 = Number.isFinite(members) && Number.isFinite(maxMembers) ? `${members} / ${maxMembers} in the room` : Number.isFinite(members) ? `${members} in the room` : ""; const expiresAt = typeof seen.expiresAt === "string" ? seen.expiresAt : ""; if (!name && !lead && !people2 && !expiresAt) return null; - return { name, lead, people: people2, expiresAt, clockOffsetMs: Number(seen.clockOffsetMs) || 0 }; + return { + name, + lead, + people: people2, + expiresAt, + clockOffsetMs: Number(seen.clockOffsetMs) || 0, + members: Number.isFinite(members) ? members : null, + maxMembers: Number.isFinite(maxMembers) ? maxMembers : null + }; } function inviteCountdown(expiresAt, offsetMs = 0) { const at = Date.parse(expiresAt) - (Number(offsetMs) || 0); - if (!Number.isFinite(at)) return "Open while the room is open"; + if (!Number.isFinite(at)) return t("Open while the room is open"); const left = Math.max(0, at - Date.now()); - if (left <= 0) return "This link has expired"; + if (left <= 0) return t("This link has expired"); const total = Math.max(1, Math.ceil(left / 1e3)); - return `${Math.floor(total / 60)}:${String(total % 60).padStart(2, "0")} left to join`; + return t("{time} left to join", { time: `${Math.floor(total / 60)}:${String(total % 60).padStart(2, "0")}` }); } -function RoomInvitePreview({ h: h52, preview, kind = "face-link-preview", busy = false, joinLabel = "Join", clearLabel = "Clear", showClear = true, onJoin, onClear, now } = {}) { +var invitePeople = (preview) => { + const members = inviteMemberCount(preview.members); + if (!Number.isFinite(members)) return preview.people || ""; + const max = inviteMemberCount(preview.maxMembers); + return Number.isFinite(max) ? t("{members} / {max} in the room", { members, max }) : t("{members} in the room", { members }); +}; +function RoomInvitePreview({ h: h52, preview, kind = "face-link-preview", busy = false, joinLabel = t("Join"), clearLabel = t("Clear"), showClear = true, onJoin, onClear, now } = {}) { if (!preview || typeof h52 !== "function") return null; const at = `${kind}`; const child = (name) => `${at}-${name}`; const countdown = now === void 0 ? inviteCountdown(preview.expiresAt, preview.clockOffsetMs) : inviteCountdownAt(preview.expiresAt, preview.clockOffsetMs, now); + const people2 = invitePeople(preview); return h52( "div", { className: "fs-preview", "data-flybot": at, role: "status" }, - h52("div", { className: "fs-preview-name", "data-flybot": child("name") }, preview.name || "Face-to-face room"), - preview.lead ? h52("div", { className: "fs-preview-meta", "data-flybot": child("lead") }, `Lead: ${preview.lead}`) : null, - preview.people ? h52("div", { className: "fs-preview-meta", "data-flybot": child("members") }, preview.people) : null, + h52("div", { className: "fs-preview-name", "data-flybot": child("name") }, preview.name || t("Face-to-face room")), + preview.lead ? h52("div", { className: "fs-preview-meta", "data-flybot": child("lead") }, t("Lead: {lead}", { lead: preview.lead })) : null, + people2 ? h52("div", { className: "fs-preview-meta", "data-flybot": child("members") }, people2) : null, preview.expiresAt ? h52("div", { className: "fs-preview-meta", "data-flybot": child("expiry") }, countdown) : null, onJoin || onClear ? h52( "div", @@ -3558,11 +3581,11 @@ function RoomInvitePreview({ h: h52, preview, kind = "face-link-preview", busy = } function inviteCountdownAt(expiresAt, offsetMs, now) { const at = Date.parse(expiresAt) - (Number(offsetMs) || 0); - if (!Number.isFinite(at)) return "Open while the room is open"; + if (!Number.isFinite(at)) return t("Open while the room is open"); const left = Math.max(0, at - Number(now || 0)); - if (left <= 0) return "This link has expired"; + if (left <= 0) return t("This link has expired"); const total = Math.max(1, Math.ceil(left / 1e3)); - return `${Math.floor(total / 60)}:${String(total % 60).padStart(2, "0")} left to join`; + return t("{time} left to join", { time: `${Math.floor(total / 60)}:${String(total % 60).padStart(2, "0")}` }); } // src/client/approval-claims.js diff --git a/core/src/client/i18n.js b/core/src/client/i18n.js index c82089bdc..80a7370d6 100644 --- a/core/src/client/i18n.js +++ b/core/src/client/i18n.js @@ -117,6 +117,14 @@ const ZH = { 'Scan with FlyBot to join this group': '用 FlyBot 扫码加入这个群', 'Copy link': '复制链接', '{time} left to join': '还剩 {time} 可加入', + // #3114: the room-invite preview card, which is drawn wherever an invite is pasted. `This link + // has expired` is its own sentence, not the `This link has expired.` above: a card that borrowed + // that entry would draw a full stop the English card never had. + 'Lead: {lead}': '总负责人:{lead}', + '{members} in the room': '房间里 {members} 人', + '{members} / {max} in the room': '房间里 {members} / {max} 人', + 'This link has expired': '这条链接已过期', + 'Open while the room is open': '房间开着的时候都能加入', 'Works until you withdraw it.': '在你撤回之前一直有效。', 'Withdraw link': '撤回链接', '1 earlier link still works.': '还有 1 个之前的链接仍然有效。', diff --git a/core/src/client/room-invite-preview.js b/core/src/client/room-invite-preview.js index ec9500a64..f0623b1d7 100644 --- a/core/src/client/room-invite-preview.js +++ b/core/src/client/room-invite-preview.js @@ -7,6 +7,11 @@ // draw the same card. Everything the card says comes from a server reply, so the strings are // cleaned here, at the last place before a person reads them — the same rules the server applies to // a room name (`cleanFaceName`): one line, no controls, brackets, markup or @, capped. +// +// The words around those values follow the window's language, looked up per draw: a caller keeps +// the preview in its own state, so a translated sentence must never be built into what it stores. +import { t } from './i18n.js' + export const INVITE_PREVIEW_NAME_MAX = 60 // Untrusted fence markers are stripped to a fixed point: one pass can remove a match and assemble a @@ -54,33 +59,51 @@ export function invitePreviewOf(seen) { : Number.isFinite(members) ? `${members} in the room` : '' const expiresAt = typeof seen.expiresAt === 'string' ? seen.expiresAt : '' if (!name && !lead && !people && !expiresAt) return null - return { name, lead, people, expiresAt, clockOffsetMs: Number(seen.clockOffsetMs) || 0 } + // The counts travel beside the sentence: a caller stores this preview, so the sentence it holds + // stays the English one, and the card builds the line in the window's language when it draws. + return { + name, lead, people, expiresAt, clockOffsetMs: Number(seen.clockOffsetMs) || 0, + members: Number.isFinite(members) ? members : null, + maxMembers: Number.isFinite(maxMembers) ? maxMembers : null, + } } // Every home counts down to the server's one deadline (#458): `offsetMs` is how far the server's // clock runs ahead of this one (0 unless the skew is real). m:ss, so the line visibly ticks. export function inviteCountdown(expiresAt, offsetMs = 0) { const at = Date.parse(expiresAt) - (Number(offsetMs) || 0) - if (!Number.isFinite(at)) return 'Open while the room is open' + if (!Number.isFinite(at)) return t('Open while the room is open') const left = Math.max(0, at - Date.now()) - if (left <= 0) return 'This link has expired' + if (left <= 0) return t('This link has expired') const total = Math.max(1, Math.ceil(left / 1000)) - return `${Math.floor(total / 60)}:${String(total % 60).padStart(2, '0')} left to join` + return t('{time} left to join', { time: `${Math.floor(total / 60)}:${String(total % 60).padStart(2, '0')}` }) +} + +// The people line is built where the card is drawn, from the counts as they arrived (#3114): a +// preview that only carries the sentence (today's `people`) still draws that sentence unchanged. +const invitePeople = (preview) => { + const members = inviteMemberCount(preview.members) + if (!Number.isFinite(members)) return preview.people || '' + const max = inviteMemberCount(preview.maxMembers) + return Number.isFinite(max) + ? t('{members} / {max} in the room', { members, max }) + : t('{members} in the room', { members }) } // `kind` prefixes every `data-flybot` name, so each surface keeps the hook its tests and its CSS // already use while sharing this one tree of elements. -export function RoomInvitePreview({ h, preview, kind = 'face-link-preview', busy = false, joinLabel = 'Join', clearLabel = 'Clear', showClear = true, onJoin, onClear, now } = {}) { +export function RoomInvitePreview({ h, preview, kind = 'face-link-preview', busy = false, joinLabel = t('Join'), clearLabel = t('Clear'), showClear = true, onJoin, onClear, now } = {}) { if (!preview || typeof h !== 'function') return null const at = `${kind}` const child = (name) => `${at}-${name}` const countdown = now === undefined ? inviteCountdown(preview.expiresAt, preview.clockOffsetMs) : inviteCountdownAt(preview.expiresAt, preview.clockOffsetMs, now) + const people = invitePeople(preview) return h('div', { className: 'fs-preview', 'data-flybot': at, role: 'status' }, - h('div', { className: 'fs-preview-name', 'data-flybot': child('name') }, preview.name || 'Face-to-face room'), - preview.lead ? h('div', { className: 'fs-preview-meta', 'data-flybot': child('lead') }, `Lead: ${preview.lead}`) : null, - preview.people ? h('div', { className: 'fs-preview-meta', 'data-flybot': child('members') }, preview.people) : null, + h('div', { className: 'fs-preview-name', 'data-flybot': child('name') }, preview.name || t('Face-to-face room')), + preview.lead ? h('div', { className: 'fs-preview-meta', 'data-flybot': child('lead') }, t('Lead: {lead}', { lead: preview.lead })) : null, + people ? h('div', { className: 'fs-preview-meta', 'data-flybot': child('members') }, people) : null, preview.expiresAt ? h('div', { className: 'fs-preview-meta', 'data-flybot': child('expiry') }, countdown) : null, onJoin || onClear ? h('div', { className: 'fs-preview-foot', 'data-flybot': child('foot') }, @@ -103,9 +126,9 @@ export function RoomInvitePreview({ h, preview, kind = 'face-link-preview', busy // then reproducible in a test without freezing the machine's clock. export function inviteCountdownAt(expiresAt, offsetMs, now) { const at = Date.parse(expiresAt) - (Number(offsetMs) || 0) - if (!Number.isFinite(at)) return 'Open while the room is open' + if (!Number.isFinite(at)) return t('Open while the room is open') const left = Math.max(0, at - Number(now || 0)) - if (left <= 0) return 'This link has expired' + if (left <= 0) return t('This link has expired') const total = Math.max(1, Math.ceil(left / 1000)) - return `${Math.floor(total / 60)}:${String(total % 60).padStart(2, '0')} left to join` + return t('{time} left to join', { time: `${Math.floor(total / 60)}:${String(total % 60).padStart(2, '0')}` }) } diff --git a/core/test/room-invite-preview.test.js b/core/test/room-invite-preview.test.js index 860d10914..8bb4c27d8 100644 --- a/core/test/room-invite-preview.test.js +++ b/core/test/room-invite-preview.test.js @@ -3,6 +3,7 @@ // surface gets depends only on `kind` — not on which surface asked. import assert from 'node:assert/strict' import { test } from 'node:test' +import { DICTIONARY, installLocale } from '../src/client/i18n.js' import { RoomInvitePreview, cleanInviteName, inviteCountdown, inviteCountdownAt, inviteMemberCount, invitePreviewOf, @@ -87,3 +88,66 @@ test('#876: the deadline line is shared copy, and an expired one says so', () => assert.equal(inviteCountdown('not a date'), 'Open while the room is open') assert.equal(cleanInviteName(' ', 'a room'), 'a room') }) + +// #3114 cut 30: the card is drawn in the window's language, and it is drawn afresh each time — +// a plugin keeps the preview it was handed in its own `invite` state, so a translated string must +// never be built there. These cases read the client's real dictionary, not a copy of it. +const fill = (text, params) => text.replace(/\{(\w+)\}/g, (match, name) => (name in (params ?? {}) ? String(params[name]) : match)) +const withLocale = (tag, run) => { + const off = installLocale({ + register: () => () => {}, + bind: () => (key, params) => fill(tag === 'zh' && Object.hasOwn(DICTIONARY.zh, key) ? DICTIONARY.zh[key] : key, params), + getLocale: () => ({ active: tag }), + }) + try { return run() } finally { off() } +} +const deadline = '2099-01-01T00:00:00.000Z' +// A fixed `now` one minute and five seconds before the deadline: the render is then reproducible +// without freezing the machine's clock (#1839). +const now = Date.parse(deadline) - 65_000 +const draw = (card, at = now) => RoomInvitePreview({ h, preview: card, now: at, onJoin() {}, onClear() {} }) +const line = (tree, kind) => byKind(tree, `face-link-preview-${kind}`)[0].children.join('') +const foot = (tree) => byKind(tree, 'face-link-preview-foot')[0].children.map(button => button.children[0]) + +test('#3114 cut 30: a zh window reads the lead, the people and the deadline in Chinese', () => { + const card = invitePreviewOf({ name: 'Saturday room', lead: 'Chief', members: 3, maxMembers: 8, expiresAt: deadline }) + const tree = withLocale('zh', () => draw(card)) + assert.equal(line(tree, 'name'), 'Saturday room') + assert.equal(line(tree, 'lead'), '总负责人:Chief') + assert.equal(line(tree, 'members'), '房间里 3 / 8 人') + assert.match(line(tree, 'expiry'), /^还剩 \d+:\d\d 可加入$/) + assert.deepEqual(foot(tree), ['清空', '加入']) +}) + +test('#3114 cut 30: the nameless fallback and the two clock answers read Chinese too', () => { + const card = invitePreviewOf({ lead: 'Chief', expiresAt: deadline }) + const tree = withLocale('zh', () => draw(card)) + assert.equal(line(tree, 'name'), '面对面房间') + withLocale('zh', () => { + assert.equal(inviteCountdown('not a date'), '房间开着的时候都能加入') + assert.equal(inviteCountdownAt('2000-01-01T00:00:00.000Z', 0, Date.parse('2025-01-01T00:00:00.000Z')), '这条链接已过期') + }) +}) + +test('#3114 cut 30: the room name, the counts and the clock the server sent stay as they came', () => { + const card = invitePreviewOf({ name: 'Saturday room', members: '3', maxMembers: '8', expiresAt: deadline }) + assert.equal(card.people, '3 / 8 in the room', 'the English line is still what a reader of the data gets') + const tree = withLocale('zh', () => draw(card)) + assert.equal(line(tree, 'name'), 'Saturday room', 'a name the server sent is not translated') + assert.equal(line(tree, 'members'), '房间里 3 / 8 人') + assert.match(line(tree, 'expiry'), /^还剩 \d+:\d\d 可加入$/, 'the digits are still the count the server sent, m:ss') + const single = withLocale('zh', () => draw(invitePreviewOf({ members: 2 }))) + assert.equal(line(single, 'members'), '房间里 2 人') +}) + +test('#3114 cut 30: a card built in English follows a language switch when it is drawn', () => { + const card = invitePreviewOf({ name: 'Saturday room', lead: 'Chief', members: 3, maxMembers: 8, expiresAt: deadline }) + assert.equal(line(draw(card), 'members'), '3 / 8 in the room') + const chinese = withLocale('zh', () => draw(card)) + assert.equal(line(chinese, 'lead'), '总负责人:Chief') + assert.equal(line(chinese, 'members'), '房间里 3 / 8 人') + assert.match(line(chinese, 'expiry'), /^还剩 \d+:\d\d 可加入$/) + const back = draw(card) + assert.equal(line(back, 'lead'), 'Lead: Chief') + assert.equal(line(back, 'members'), '3 / 8 in the room') +}) diff --git a/patches/ds-bot/0001-core-fixes.patch b/patches/ds-bot/0001-core-fixes.patch index 45766e6a1..e6cf9882b 100644 --- a/patches/ds-bot/0001-core-fixes.patch +++ b/patches/ds-bot/0001-core-fixes.patch @@ -802,7 +802,7 @@ diff --git a/src/client/i18n.js b/src/client/i18n.js // Shared 'Main Bot': '主 Bot', Working: '工作中', -@@ -12,6 +12,69 @@ const ZH = { +@@ -12,6 +12,77 @@ const ZH = { 'Unread activity': '有未读', Search: '搜索', 'New chat': '新对话', @@ -856,6 +856,14 @@ diff --git a/src/client/i18n.js b/src/client/i18n.js + 'Scan with FlyBot to join this group': '用 FlyBot 扫码加入这个群', + 'Copy link': '复制链接', + '{time} left to join': '还剩 {time} 可加入', ++ // #3114: the room-invite preview card, which is drawn wherever an invite is pasted. `This link ++ // has expired` is its own sentence, not the `This link has expired.` above: a card that borrowed ++ // that entry would draw a full stop the English card never had. ++ 'Lead: {lead}': '总负责人:{lead}', ++ '{members} in the room': '房间里 {members} 人', ++ '{members} / {max} in the room': '房间里 {members} / {max} 人', ++ 'This link has expired': '这条链接已过期', ++ 'Open while the room is open': '房间开着的时候都能加入', + 'Works until you withdraw it.': '在你撤回之前一直有效。', + 'Withdraw link': '撤回链接', + '1 earlier link still works.': '还有 1 个之前的链接仍然有效。', diff --git a/patches/ds-bot/0008-client-bundle.patch b/patches/ds-bot/0008-client-bundle.patch index 064c1b433..dde84df05 100644 --- a/patches/ds-bot/0008-client-bundle.patch +++ b/patches/ds-bot/0008-client-bundle.patch @@ -62,7 +62,7 @@ diff --git a/client.js b/client.js // Shared "Main Bot": "主 Bot", Working: "工作中", -@@ -49,6 +49,69 @@ var ZH = { +@@ -49,6 +49,77 @@ var ZH = { "Unread activity": "有未读", Search: "搜索", "New chat": "新对话", @@ -116,6 +116,14 @@ diff --git a/client.js b/client.js + "Scan with FlyBot to join this group": "用 FlyBot 扫码加入这个群", + "Copy link": "复制链接", + "{time} left to join": "还剩 {time} 可加入", ++ // #3114: the room-invite preview card, which is drawn wherever an invite is pasted. `This link ++ // has expired` is its own sentence, not the `This link has expired.` above: a card that borrowed ++ // that entry would draw a full stop the English card never had. ++ "Lead: {lead}": "总负责人:{lead}", ++ "{members} in the room": "房间里 {members} 人", ++ "{members} / {max} in the room": "房间里 {members} / {max} 人", ++ "This link has expired": "这条链接已过期", ++ "Open while the room is open": "房间开着的时候都能加入", + "Works until you withdraw it.": "在你撤回之前一直有效。", + "Withdraw link": "撤回链接", + "1 earlier link still works.": "还有 1 个之前的链接仍然有效。", @@ -2431,7 +2439,7 @@ diff --git a/client.js b/client.js var formatTokens = (count) => count >= 1e6 ? `${(count / 1e6).toFixed(1)}M` : count >= 1e3 ? `${(count / 1e3).toFixed(1)}K` : String(count); function usageOf(list, ids) { let input = 0; -@@ -1624,537 +1624,3200 @@ function usageOf(list, ids) { +@@ -1624,537 +1624,3215 @@ function usageOf(list, ids) { const hit = input > 0 ? Math.round(cacheRead / input * 100) : 0; return t("{tokens} tok · Cache hit {hit}%", { tokens: formatTokens(input + output), hit }); } @@ -3889,27 +3897,42 @@ diff --git a/client.js b/client.js + const people2 = Number.isFinite(members) && Number.isFinite(maxMembers) ? `${members} / ${maxMembers} in the room` : Number.isFinite(members) ? `${members} in the room` : ""; + const expiresAt = typeof seen.expiresAt === "string" ? seen.expiresAt : ""; + if (!name && !lead && !people2 && !expiresAt) return null; -+ return { name, lead, people: people2, expiresAt, clockOffsetMs: Number(seen.clockOffsetMs) || 0 }; ++ return { ++ name, ++ lead, ++ people: people2, ++ expiresAt, ++ clockOffsetMs: Number(seen.clockOffsetMs) || 0, ++ members: Number.isFinite(members) ? members : null, ++ maxMembers: Number.isFinite(maxMembers) ? maxMembers : null ++ }; +} +function inviteCountdown(expiresAt, offsetMs = 0) { + const at = Date.parse(expiresAt) - (Number(offsetMs) || 0); -+ if (!Number.isFinite(at)) return "Open while the room is open"; ++ if (!Number.isFinite(at)) return t("Open while the room is open"); + const left = Math.max(0, at - Date.now()); -+ if (left <= 0) return "This link has expired"; ++ if (left <= 0) return t("This link has expired"); + const total = Math.max(1, Math.ceil(left / 1e3)); -+ return `${Math.floor(total / 60)}:${String(total % 60).padStart(2, "0")} left to join`; ++ return t("{time} left to join", { time: `${Math.floor(total / 60)}:${String(total % 60).padStart(2, "0")}` }); +} -+function RoomInvitePreview({ h: h52, preview, kind = "face-link-preview", busy = false, joinLabel = "Join", clearLabel = "Clear", showClear = true, onJoin, onClear, now } = {}) { ++var invitePeople = (preview) => { ++ const members = inviteMemberCount(preview.members); ++ if (!Number.isFinite(members)) return preview.people || ""; ++ const max = inviteMemberCount(preview.maxMembers); ++ return Number.isFinite(max) ? t("{members} / {max} in the room", { members, max }) : t("{members} in the room", { members }); ++}; ++function RoomInvitePreview({ h: h52, preview, kind = "face-link-preview", busy = false, joinLabel = t("Join"), clearLabel = t("Clear"), showClear = true, onJoin, onClear, now } = {}) { + if (!preview || typeof h52 !== "function") return null; + const at = `${kind}`; + const child = (name) => `${at}-${name}`; + const countdown = now === void 0 ? inviteCountdown(preview.expiresAt, preview.clockOffsetMs) : inviteCountdownAt(preview.expiresAt, preview.clockOffsetMs, now); ++ const people2 = invitePeople(preview); + return h52( + "div", + { className: "fs-preview", "data-flybot": at, role: "status" }, -+ h52("div", { className: "fs-preview-name", "data-flybot": child("name") }, preview.name || "Face-to-face room"), -+ preview.lead ? h52("div", { className: "fs-preview-meta", "data-flybot": child("lead") }, `Lead: ${preview.lead}`) : null, -+ preview.people ? h52("div", { className: "fs-preview-meta", "data-flybot": child("members") }, preview.people) : null, ++ h52("div", { className: "fs-preview-name", "data-flybot": child("name") }, preview.name || t("Face-to-face room")), ++ preview.lead ? h52("div", { className: "fs-preview-meta", "data-flybot": child("lead") }, t("Lead: {lead}", { lead: preview.lead })) : null, ++ people2 ? h52("div", { className: "fs-preview-meta", "data-flybot": child("members") }, people2) : null, + preview.expiresAt ? h52("div", { className: "fs-preview-meta", "data-flybot": child("expiry") }, countdown) : null, + onJoin || onClear ? h52( + "div", @@ -3933,11 +3956,11 @@ diff --git a/client.js b/client.js +} +function inviteCountdownAt(expiresAt, offsetMs, now) { + const at = Date.parse(expiresAt) - (Number(offsetMs) || 0); -+ if (!Number.isFinite(at)) return "Open while the room is open"; ++ if (!Number.isFinite(at)) return t("Open while the room is open"); + const left = Math.max(0, at - Number(now || 0)); -+ if (left <= 0) return "This link has expired"; ++ if (left <= 0) return t("This link has expired"); + const total = Math.max(1, Math.ceil(left / 1e3)); -+ return `${Math.floor(total / 60)}:${String(total % 60).padStart(2, "0")} left to join`; ++ return t("{time} left to join", { time: `${Math.floor(total / 60)}:${String(total % 60).padStart(2, "0")}` }); +} + +// src/client/approval-claims.js diff --git a/patches/ds-bot/files/src/client/room-invite-preview.js b/patches/ds-bot/files/src/client/room-invite-preview.js index ec9500a64..f0623b1d7 100644 --- a/patches/ds-bot/files/src/client/room-invite-preview.js +++ b/patches/ds-bot/files/src/client/room-invite-preview.js @@ -7,6 +7,11 @@ // draw the same card. Everything the card says comes from a server reply, so the strings are // cleaned here, at the last place before a person reads them — the same rules the server applies to // a room name (`cleanFaceName`): one line, no controls, brackets, markup or @, capped. +// +// The words around those values follow the window's language, looked up per draw: a caller keeps +// the preview in its own state, so a translated sentence must never be built into what it stores. +import { t } from './i18n.js' + export const INVITE_PREVIEW_NAME_MAX = 60 // Untrusted fence markers are stripped to a fixed point: one pass can remove a match and assemble a @@ -54,33 +59,51 @@ export function invitePreviewOf(seen) { : Number.isFinite(members) ? `${members} in the room` : '' const expiresAt = typeof seen.expiresAt === 'string' ? seen.expiresAt : '' if (!name && !lead && !people && !expiresAt) return null - return { name, lead, people, expiresAt, clockOffsetMs: Number(seen.clockOffsetMs) || 0 } + // The counts travel beside the sentence: a caller stores this preview, so the sentence it holds + // stays the English one, and the card builds the line in the window's language when it draws. + return { + name, lead, people, expiresAt, clockOffsetMs: Number(seen.clockOffsetMs) || 0, + members: Number.isFinite(members) ? members : null, + maxMembers: Number.isFinite(maxMembers) ? maxMembers : null, + } } // Every home counts down to the server's one deadline (#458): `offsetMs` is how far the server's // clock runs ahead of this one (0 unless the skew is real). m:ss, so the line visibly ticks. export function inviteCountdown(expiresAt, offsetMs = 0) { const at = Date.parse(expiresAt) - (Number(offsetMs) || 0) - if (!Number.isFinite(at)) return 'Open while the room is open' + if (!Number.isFinite(at)) return t('Open while the room is open') const left = Math.max(0, at - Date.now()) - if (left <= 0) return 'This link has expired' + if (left <= 0) return t('This link has expired') const total = Math.max(1, Math.ceil(left / 1000)) - return `${Math.floor(total / 60)}:${String(total % 60).padStart(2, '0')} left to join` + return t('{time} left to join', { time: `${Math.floor(total / 60)}:${String(total % 60).padStart(2, '0')}` }) +} + +// The people line is built where the card is drawn, from the counts as they arrived (#3114): a +// preview that only carries the sentence (today's `people`) still draws that sentence unchanged. +const invitePeople = (preview) => { + const members = inviteMemberCount(preview.members) + if (!Number.isFinite(members)) return preview.people || '' + const max = inviteMemberCount(preview.maxMembers) + return Number.isFinite(max) + ? t('{members} / {max} in the room', { members, max }) + : t('{members} in the room', { members }) } // `kind` prefixes every `data-flybot` name, so each surface keeps the hook its tests and its CSS // already use while sharing this one tree of elements. -export function RoomInvitePreview({ h, preview, kind = 'face-link-preview', busy = false, joinLabel = 'Join', clearLabel = 'Clear', showClear = true, onJoin, onClear, now } = {}) { +export function RoomInvitePreview({ h, preview, kind = 'face-link-preview', busy = false, joinLabel = t('Join'), clearLabel = t('Clear'), showClear = true, onJoin, onClear, now } = {}) { if (!preview || typeof h !== 'function') return null const at = `${kind}` const child = (name) => `${at}-${name}` const countdown = now === undefined ? inviteCountdown(preview.expiresAt, preview.clockOffsetMs) : inviteCountdownAt(preview.expiresAt, preview.clockOffsetMs, now) + const people = invitePeople(preview) return h('div', { className: 'fs-preview', 'data-flybot': at, role: 'status' }, - h('div', { className: 'fs-preview-name', 'data-flybot': child('name') }, preview.name || 'Face-to-face room'), - preview.lead ? h('div', { className: 'fs-preview-meta', 'data-flybot': child('lead') }, `Lead: ${preview.lead}`) : null, - preview.people ? h('div', { className: 'fs-preview-meta', 'data-flybot': child('members') }, preview.people) : null, + h('div', { className: 'fs-preview-name', 'data-flybot': child('name') }, preview.name || t('Face-to-face room')), + preview.lead ? h('div', { className: 'fs-preview-meta', 'data-flybot': child('lead') }, t('Lead: {lead}', { lead: preview.lead })) : null, + people ? h('div', { className: 'fs-preview-meta', 'data-flybot': child('members') }, people) : null, preview.expiresAt ? h('div', { className: 'fs-preview-meta', 'data-flybot': child('expiry') }, countdown) : null, onJoin || onClear ? h('div', { className: 'fs-preview-foot', 'data-flybot': child('foot') }, @@ -103,9 +126,9 @@ export function RoomInvitePreview({ h, preview, kind = 'face-link-preview', busy // then reproducible in a test without freezing the machine's clock. export function inviteCountdownAt(expiresAt, offsetMs, now) { const at = Date.parse(expiresAt) - (Number(offsetMs) || 0) - if (!Number.isFinite(at)) return 'Open while the room is open' + if (!Number.isFinite(at)) return t('Open while the room is open') const left = Math.max(0, at - Number(now || 0)) - if (left <= 0) return 'This link has expired' + if (left <= 0) return t('This link has expired') const total = Math.max(1, Math.ceil(left / 1000)) - return `${Math.floor(total / 60)}:${String(total % 60).padStart(2, '0')} left to join` + return t('{time} left to join', { time: `${Math.floor(total / 60)}:${String(total % 60).padStart(2, '0')}` }) } diff --git a/patches/ds-bot/files/test/room-invite-preview.test.js b/patches/ds-bot/files/test/room-invite-preview.test.js index 860d10914..8bb4c27d8 100644 --- a/patches/ds-bot/files/test/room-invite-preview.test.js +++ b/patches/ds-bot/files/test/room-invite-preview.test.js @@ -3,6 +3,7 @@ // surface gets depends only on `kind` — not on which surface asked. import assert from 'node:assert/strict' import { test } from 'node:test' +import { DICTIONARY, installLocale } from '../src/client/i18n.js' import { RoomInvitePreview, cleanInviteName, inviteCountdown, inviteCountdownAt, inviteMemberCount, invitePreviewOf, @@ -87,3 +88,66 @@ test('#876: the deadline line is shared copy, and an expired one says so', () => assert.equal(inviteCountdown('not a date'), 'Open while the room is open') assert.equal(cleanInviteName(' ', 'a room'), 'a room') }) + +// #3114 cut 30: the card is drawn in the window's language, and it is drawn afresh each time — +// a plugin keeps the preview it was handed in its own `invite` state, so a translated string must +// never be built there. These cases read the client's real dictionary, not a copy of it. +const fill = (text, params) => text.replace(/\{(\w+)\}/g, (match, name) => (name in (params ?? {}) ? String(params[name]) : match)) +const withLocale = (tag, run) => { + const off = installLocale({ + register: () => () => {}, + bind: () => (key, params) => fill(tag === 'zh' && Object.hasOwn(DICTIONARY.zh, key) ? DICTIONARY.zh[key] : key, params), + getLocale: () => ({ active: tag }), + }) + try { return run() } finally { off() } +} +const deadline = '2099-01-01T00:00:00.000Z' +// A fixed `now` one minute and five seconds before the deadline: the render is then reproducible +// without freezing the machine's clock (#1839). +const now = Date.parse(deadline) - 65_000 +const draw = (card, at = now) => RoomInvitePreview({ h, preview: card, now: at, onJoin() {}, onClear() {} }) +const line = (tree, kind) => byKind(tree, `face-link-preview-${kind}`)[0].children.join('') +const foot = (tree) => byKind(tree, 'face-link-preview-foot')[0].children.map(button => button.children[0]) + +test('#3114 cut 30: a zh window reads the lead, the people and the deadline in Chinese', () => { + const card = invitePreviewOf({ name: 'Saturday room', lead: 'Chief', members: 3, maxMembers: 8, expiresAt: deadline }) + const tree = withLocale('zh', () => draw(card)) + assert.equal(line(tree, 'name'), 'Saturday room') + assert.equal(line(tree, 'lead'), '总负责人:Chief') + assert.equal(line(tree, 'members'), '房间里 3 / 8 人') + assert.match(line(tree, 'expiry'), /^还剩 \d+:\d\d 可加入$/) + assert.deepEqual(foot(tree), ['清空', '加入']) +}) + +test('#3114 cut 30: the nameless fallback and the two clock answers read Chinese too', () => { + const card = invitePreviewOf({ lead: 'Chief', expiresAt: deadline }) + const tree = withLocale('zh', () => draw(card)) + assert.equal(line(tree, 'name'), '面对面房间') + withLocale('zh', () => { + assert.equal(inviteCountdown('not a date'), '房间开着的时候都能加入') + assert.equal(inviteCountdownAt('2000-01-01T00:00:00.000Z', 0, Date.parse('2025-01-01T00:00:00.000Z')), '这条链接已过期') + }) +}) + +test('#3114 cut 30: the room name, the counts and the clock the server sent stay as they came', () => { + const card = invitePreviewOf({ name: 'Saturday room', members: '3', maxMembers: '8', expiresAt: deadline }) + assert.equal(card.people, '3 / 8 in the room', 'the English line is still what a reader of the data gets') + const tree = withLocale('zh', () => draw(card)) + assert.equal(line(tree, 'name'), 'Saturday room', 'a name the server sent is not translated') + assert.equal(line(tree, 'members'), '房间里 3 / 8 人') + assert.match(line(tree, 'expiry'), /^还剩 \d+:\d\d 可加入$/, 'the digits are still the count the server sent, m:ss') + const single = withLocale('zh', () => draw(invitePreviewOf({ members: 2 }))) + assert.equal(line(single, 'members'), '房间里 2 人') +}) + +test('#3114 cut 30: a card built in English follows a language switch when it is drawn', () => { + const card = invitePreviewOf({ name: 'Saturday room', lead: 'Chief', members: 3, maxMembers: 8, expiresAt: deadline }) + assert.equal(line(draw(card), 'members'), '3 / 8 in the room') + const chinese = withLocale('zh', () => draw(card)) + assert.equal(line(chinese, 'lead'), '总负责人:Chief') + assert.equal(line(chinese, 'members'), '房间里 3 / 8 人') + assert.match(line(chinese, 'expiry'), /^还剩 \d+:\d\d 可加入$/) + const back = draw(card) + assert.equal(line(back, 'lead'), 'Lead: Chief') + assert.equal(line(back, 'members'), '3 / 8 in the room') +})