mirror of
https://github.com/hansjone/dsh-im-ops.git
synced 2026-10-12 10:30:44 +08:00
feat(i18n): add English support for host-side chat messages
Wrap all user-facing chat text in src/channels with a host-side t() translator (Chinese is the identity default, so untranslated text is always sent verbatim in Chinese). English is selected via the host plugin config `language: en` or the DSH_IM_LANGUAGE environment variable, and the settings UI keeps following the DSH web locale. - add src/channels/shared/i18n.mjs with setImHostLanguage/t() and a Chinese-key -> English dictionary split by area (shared + channels) - complete the English dictionary for every t() key in src/channels, including the feishu channel (welcome/help, repair flow, interactive cards, watch flow) and the shared command/approval/stream text - wrap previously untranslated chat-facing strings in feishu bridge, feishu runtime, editable-message-stream, harness-client, connection-test and image-prompt - dedupe dictionary entries that existed in multiple channel files and canonicalize conflicting translations into the shared dictionaries - extend the i18n unit test to decode string-literal escapes so keys containing \n are checked correctly - document the language switch in README.md, README.en.md and CHANGELOG.md, and rebuild lib/index.js Added English support for bot chat messages across all nine IM channels and AI Office without changing any Chinese output.
This commit is contained in:
parent
3ff8c2a519
commit
dcd7cd64d8
78 changed files with 2283 additions and 992 deletions
|
|
@ -2,6 +2,8 @@ import { createHash } from 'node:crypto';
|
|||
import { mkdir, readFile, rename, unlink, writeFile } from 'node:fs/promises';
|
||||
import { dirname } from 'node:path';
|
||||
|
||||
import { t } from '../shared/i18n.mjs';
|
||||
|
||||
const EMPTY_DOCUMENT = Object.freeze({ version: 2, bots: Object.freeze([]) });
|
||||
const BOT_ID_PATTERN = /^whatsapp_[a-f0-9]{24}$/;
|
||||
const AUTH_DIRECTORY_PATTERN = /^[a-f0-9-]{36}$/;
|
||||
|
|
@ -30,7 +32,7 @@ export function deriveWhatsappBotId(accountJid) {
|
|||
|
||||
export function maskWhatsappAccount(accountJid) {
|
||||
const digits = normalizeWhatsappAccountJid(accountJid)?.split('@')[0] ?? '';
|
||||
if (!digits) return 'WhatsApp账号';
|
||||
if (!digits) return t('WhatsApp账号');
|
||||
if (digits.length <= 7) return `${digits.slice(0, 2)}•••${digits.slice(-2)}`;
|
||||
return `${digits.slice(0, 4)}••••${digits.slice(-4)}`;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
import { t } from '../shared/i18n.mjs';
|
||||
import { createTextBridgeStatus, TextHarnessBridge } from '../shared/text-harness-bridge.mjs';
|
||||
|
||||
export const WHATSAPP_DESCRIPTOR = Object.freeze({
|
||||
key: 'whatsapp',
|
||||
label: 'WhatsApp',
|
||||
connectionLabel: ' Web 关联设备',
|
||||
// Translated lazily: t() must run after setImHostLanguage, not at import time.
|
||||
get connectionLabel() { return t(' Web 关联设备'); },
|
||||
});
|
||||
|
||||
export class WhatsappHarnessBridge extends TextHarnessBridge {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import { randomUUID } from 'node:crypto';
|
||||
|
||||
import { connectionTestMessage } from '../shared/connection-test.mjs';
|
||||
import { t } from '../shared/i18n.mjs';
|
||||
import {
|
||||
deriveWhatsappBotId,
|
||||
maskWhatsappAccount,
|
||||
|
|
@ -83,8 +84,8 @@ export class WhatsappController {
|
|||
this.#errors.set(config.botId, safeError(
|
||||
error?.code === 'relink-required' ? 'relink-required' : 'connection-failed',
|
||||
error?.code === 'relink-required'
|
||||
? 'WhatsApp 关联设备已失效,请移除后重新扫码。'
|
||||
: 'WhatsApp 连接未就绪,插件会自动重试。',
|
||||
? t('WhatsApp 关联设备已失效,请移除后重新扫码。')
|
||||
: t('WhatsApp 连接未就绪,插件会自动重试。'),
|
||||
));
|
||||
this.#logger.warn?.(`[dsh-im:whatsapp] bot ${config.botId} failed to initialize`);
|
||||
} finally {
|
||||
|
|
@ -172,7 +173,7 @@ export class WhatsappController {
|
|||
await record.task?.catch(() => undefined);
|
||||
if (!TERMINAL_ATTEMPT_STATES.has(record.state)) {
|
||||
record.state = 'cancelled';
|
||||
record.error = safeError('cancelled', '扫码接入已取消。');
|
||||
record.error = safeError('cancelled', t('扫码接入已取消。'));
|
||||
}
|
||||
await this.#deleteAuth(record.authDirectory).catch(() => undefined);
|
||||
}
|
||||
|
|
@ -192,8 +193,8 @@ export class WhatsappController {
|
|||
this.#errors.set(botId, safeError(
|
||||
error?.code === 'relink-required' ? 'relink-required' : 'connection-failed',
|
||||
error?.code === 'relink-required'
|
||||
? 'WhatsApp 关联设备已失效,请移除后重新扫码。'
|
||||
: 'WhatsApp 连接仍未就绪,请稍后重试。',
|
||||
? t('WhatsApp 关联设备已失效,请移除后重新扫码。')
|
||||
: t('WhatsApp 连接仍未就绪,请稍后重试。'),
|
||||
));
|
||||
throw error;
|
||||
} finally {
|
||||
|
|
@ -209,14 +210,14 @@ export class WhatsappController {
|
|||
return this.#withBotTransition(botId, async () => {
|
||||
const runtime = this.#runtimes.get(botId);
|
||||
if (!runtime?.status?.ready || typeof runtime.sendConnectionTest !== 'function') {
|
||||
const error = new Error('WhatsApp机器人尚未连接');
|
||||
const error = new Error(t('WhatsApp机器人尚未连接'));
|
||||
error.code = 'test-target-unavailable';
|
||||
throw error;
|
||||
}
|
||||
return runtime.sendConnectionTest(
|
||||
connectionTestMessage(
|
||||
`${config.name}(${maskWhatsappAccount(config.accountJid)})`,
|
||||
'WhatsApp机器人',
|
||||
t('WhatsApp机器人'),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
|
@ -275,8 +276,8 @@ export class WhatsappController {
|
|||
bot: { name: config.name, idMasked: maskWhatsappAccount(config.accountJid) },
|
||||
health: {
|
||||
status: connected ? 'healthy' : state === 'error' ? 'error' : 'offline',
|
||||
summary: connected ? 'WhatsApp Web 关联设备运行正常'
|
||||
: state === 'error' ? 'WhatsApp 连接未就绪' : 'WhatsApp 连接当前离线',
|
||||
summary: connected ? t('WhatsApp Web 关联设备运行正常')
|
||||
: state === 'error' ? t('WhatsApp 连接未就绪') : t('WhatsApp 连接当前离线'),
|
||||
lastCheckedAt: runtimeStatus?.lastCheckedAt ?? null,
|
||||
lastConnectedAt: runtimeStatus?.lastConnectedAt ?? null,
|
||||
},
|
||||
|
|
@ -340,7 +341,7 @@ export class WhatsappController {
|
|||
await this.#startRuntime(config);
|
||||
this.#errors.delete(botId);
|
||||
} catch (error) {
|
||||
this.#errors.set(botId, safeError('connection-failed', 'WhatsApp 已绑定,消息连接暂未就绪。'));
|
||||
this.#errors.set(botId, safeError('connection-failed', t('WhatsApp 已绑定,消息连接暂未就绪。')));
|
||||
this.#logger.warn?.(`[dsh-im:whatsapp] bot ${botId} did not reconnect after QR binding`);
|
||||
}
|
||||
if (previous?.authDirectory && previous.authDirectory !== config.authDirectory) {
|
||||
|
|
@ -363,11 +364,11 @@ export class WhatsappController {
|
|||
await this.#deleteAuth(record.authDirectory).catch(() => undefined);
|
||||
if (previous) await this.#startRuntime(previous).catch(() => undefined);
|
||||
record.state = 'cancelled';
|
||||
record.error = safeError('cancelled', '扫码接入已取消。');
|
||||
record.error = safeError('cancelled', t('扫码接入已取消。'));
|
||||
} else {
|
||||
await this.#deleteAuth(record.authDirectory).catch(() => undefined);
|
||||
record.state = 'failed';
|
||||
record.error = safeError('activation-failed', 'WhatsApp 已扫码,但无法保存关联设备。');
|
||||
record.error = safeError('activation-failed', t('WhatsApp 已扫码,但无法保存关联设备。'));
|
||||
this.#logger.error?.('[dsh-im:whatsapp] unable to persist linked-device session');
|
||||
}
|
||||
} finally {
|
||||
|
|
@ -380,10 +381,10 @@ export class WhatsappController {
|
|||
if (TERMINAL_ATTEMPT_STATES.has(record.state)) return;
|
||||
if (record.controller.signal.aborted || error?.name === 'AbortError') {
|
||||
record.state = 'cancelled';
|
||||
record.error = safeError('cancelled', '扫码接入已取消。');
|
||||
record.error = safeError('cancelled', t('扫码接入已取消。'));
|
||||
} else {
|
||||
record.state = 'failed';
|
||||
record.error = safeError('qr-connect-failed', '无法连接 WhatsApp,请重新生成二维码。');
|
||||
record.error = safeError('qr-connect-failed', t('无法连接 WhatsApp,请重新生成二维码。'));
|
||||
}
|
||||
if (this.#activeAttemptId === record.id) this.#activeAttemptId = null;
|
||||
await record.session?.close().catch(() => undefined);
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import {
|
|||
} from '@whiskeysockets/baileys';
|
||||
|
||||
import { splitMessageText } from '../shared/editable-message-stream.mjs';
|
||||
import { t } from '../shared/i18n.mjs';
|
||||
import { ImagePromptError } from '../shared/image-prompt.mjs';
|
||||
import { trackOutboundArtifactProviderPromise } from '../shared/semantic/artifact.mjs';
|
||||
import { createWhatsappBridgeStatus, WhatsappHarnessBridge } from './whatsapp-bridge.mjs';
|
||||
|
|
@ -107,7 +108,7 @@ async function downloadWhatsappImage(message, download, {
|
|||
throw new ImagePromptError(
|
||||
'image-download-failed',
|
||||
`WhatsApp image download timed out after ${IMAGE_DOWNLOAD_TIMEOUT_MS} ms`,
|
||||
'图片下载失败,请重新发送后再试。',
|
||||
t('图片下载失败,请重新发送后再试。'),
|
||||
);
|
||||
}
|
||||
throw error;
|
||||
|
|
@ -126,7 +127,7 @@ async function downloadWhatsappImage(message, download, {
|
|||
throw new ImagePromptError(
|
||||
'image-too-large',
|
||||
`WhatsApp image exceeded ${maxBytes} bytes`,
|
||||
'图片超过 5 MB,请压缩后重试。',
|
||||
t('图片超过 5 MB,请压缩后重试。'),
|
||||
);
|
||||
}
|
||||
chunks.push(data);
|
||||
|
|
@ -137,7 +138,7 @@ async function downloadWhatsappImage(message, download, {
|
|||
throw new ImagePromptError(
|
||||
'image-download-failed',
|
||||
`WhatsApp image stream timed out after ${IMAGE_DOWNLOAD_TIMEOUT_MS} ms`,
|
||||
'图片下载失败,请重新发送后再试。',
|
||||
t('图片下载失败,请重新发送后再试。'),
|
||||
);
|
||||
}
|
||||
throw error;
|
||||
|
|
@ -640,7 +641,7 @@ export class WhatsappRuntime {
|
|||
|
||||
async sendConnectionTest(text) {
|
||||
if (!this.#status.ready || !this.#client) {
|
||||
const error = new Error('WhatsApp机器人尚未连接');
|
||||
const error = new Error(t('WhatsApp机器人尚未连接'));
|
||||
error.code = 'test-target-unavailable';
|
||||
throw error;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ import makeWASocket, {
|
|||
useMultiFileAuthState,
|
||||
} from '@whiskeysockets/baileys';
|
||||
|
||||
import { t } from '../shared/i18n.mjs';
|
||||
|
||||
const SILENT_LOGGER = Object.freeze({
|
||||
level: 'silent',
|
||||
trace() {},
|
||||
|
|
@ -57,7 +59,7 @@ function normalizeIdentity(socket, authState) {
|
|||
return {
|
||||
accountJid,
|
||||
name: typeof source?.name === 'string' && source.name.trim()
|
||||
? source.name.trim().slice(0, 100) : 'WhatsApp机器人',
|
||||
? source.name.trim().slice(0, 100) : t('WhatsApp机器人'),
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue