feat(i18n): add global language resolution and localize user-facing text

This commit is contained in:
zhayujie
2026-05-31 16:49:35 +08:00
parent 2ec6ea8045
commit fcf4eb78dc
15 changed files with 748 additions and 289 deletions

View File

@@ -14,7 +14,7 @@ CHINA_MIRROR = "https://registry.npmmirror.com/-/binary/playwright"
# stream(msg, fg=None) — fg is "yellow" | "green" | "red" | None
StreamFn = Callable[[str, Optional[str]], None]
# on_phase(msg) — coarse-grained progress for chat channels (Chinese)
# on_phase(msg) — coarse-grained progress for chat channels (localized via i18n)
PhaseFn = Callable[[str], None]
@@ -112,16 +112,25 @@ def run_install_browser(
stream: Optional callback ``(message, fg)`` for each line. ``fg`` is
``yellow`` / ``green`` / ``red`` or None. Defaults to colored click output.
on_phase: Optional callback for coarse progress (e.g. push to chat);
messages are short Chinese status lines.
messages are short status lines localized via i18n.
Returns:
0 on success, 1 on fatal failure (pip or chromium install failed).
"""
from cli.utils import get_cli_language
from common import i18n
get_cli_language() # resolve cow_lang so i18n.t reflects config
_t = i18n.t
stream = stream or _default_stream
python = sys.executable
legacy_mode = False
_phase(on_phase, "🔧 开始安装浏览器工具依赖(约几分钟,请耐心等待)…")
_phase(on_phase, _t(
"🔧 开始安装浏览器工具依赖(约几分钟,请耐心等待)…",
"🔧 Installing browser tool dependencies (a few minutes, please wait)…",
))
glibc = _get_glibc_version()
if glibc and glibc < GLIBC_THRESHOLD:
@@ -136,27 +145,36 @@ def run_install_browser(
stream("")
_phase(
on_phase,
f" 检测到 glibc {glibc_str}(较旧),将安装兼容版 Playwright {PLAYWRIGHT_LEGACY_VERSION}",
_t(
f" 检测到 glibc {glibc_str}(较旧),将安装兼容版 Playwright {PLAYWRIGHT_LEGACY_VERSION}",
f" Detected glibc {glibc_str} (older); installing compatible Playwright {PLAYWRIGHT_LEGACY_VERSION}.",
),
)
target_version = PLAYWRIGHT_LEGACY_VERSION if legacy_mode else PLAYWRIGHT_VERSION
_phase(on_phase, "📦 [1/3] 正在安装 Playwright Python 包…")
_phase(on_phase, _t("📦 [1/3] 正在安装 Playwright Python 包…", "📦 [1/3] Installing Playwright Python package…"))
stream("[1/3] Installing playwright Python package...", "yellow")
ret = _pip_install(f"playwright=={target_version}", stream)
if ret != 0:
stream("Failed to install playwright package.", "red")
_phase(on_phase, "❌ [1/3] Playwright Python 包安装失败。")
_phase(on_phase, _t("❌ [1/3] Playwright Python 包安装失败。", "❌ [1/3] Failed to install Playwright Python package."))
return 1
installed = _get_installed_version()
if installed:
stream(f" playwright {installed} installed.", "green")
stream("")
_phase(on_phase, f"✅ [1/3] Playwright 包已安装({installed or target_version})。")
_phase(on_phase, _t(
f"✅ [1/3] Playwright 包已安装({installed or target_version})。",
f"✅ [1/3] Playwright package installed ({installed or target_version}).",
))
if sys.platform == "linux":
_phase(on_phase, "🔧 [2/3] 正在安装 Linux 系统依赖与轻量中文字体(文泉驿正黑,部分步骤可能需要 sudo")
_phase(on_phase, _t(
"🔧 [2/3] 正在安装 Linux 系统依赖与轻量中文字体(文泉驿正黑,部分步骤可能需要 sudo",
"🔧 [2/3] Installing Linux system deps and a lightweight CJK font (WenQuanYi Zen Hei; some steps may need sudo)…",
))
stream("[2/3] Installing system dependencies (Linux)...", "yellow")
ret = subprocess.call([python, "-m", "playwright", "install-deps", "chromium"])
if ret != 0:
@@ -183,14 +201,23 @@ def run_install_browser(
stream(" CJK font (wqy-zenhei) installed.", "green")
_phase(
on_phase,
"✅ [2/3] Linux 依赖与字体步骤已执行(若有权限问题请查看服务器日志或手动执行提示命令)。",
_t(
"✅ [2/3] Linux 依赖与字体步骤已执行(若有权限问题请查看服务器日志或手动执行提示命令)。",
"✅ [2/3] Linux deps and font steps executed (on permission issues, check the server log or run the suggested commands manually).",
),
)
else:
stream(f"[2/3] Skipping system deps (not needed on {sys.platform}).", "yellow")
_phase(on_phase, f" [2/3] 当前系统({sys.platform})跳过 Linux 专用依赖。")
_phase(on_phase, _t(
f" [2/3] 当前系统({sys.platform})跳过 Linux 专用依赖。",
f" [2/3] Skipping Linux-specific deps on this platform ({sys.platform}).",
))
stream("")
_phase(on_phase, "🌐 [3/3] 正在下载并安装 Chromium体积较大请耐心等待")
_phase(on_phase, _t(
"🌐 [3/3] 正在下载并安装 Chromium体积较大请耐心等待",
"🌐 [3/3] Downloading and installing Chromium (large download, please wait)…",
))
stream("[3/3] Installing Chromium browser...", "yellow")
cmd = [python, "-m", "playwright", "install", "chromium"]
@@ -209,27 +236,33 @@ def run_install_browser(
if use_mirror:
env["PLAYWRIGHT_DOWNLOAD_HOST"] = CHINA_MIRROR
stream(f" (using China mirror: {CHINA_MIRROR})", None)
_phase(on_phase, "📡 检测到国内 pip 源配置Chromium 将优先走国内镜像下载。")
_phase(on_phase, _t(
"📡 检测到国内 pip 源配置Chromium 将优先走国内镜像下载。",
"📡 Detected a China pip mirror; Chromium will be downloaded from the China mirror first.",
))
ret = subprocess.call(cmd, env=env)
if ret != 0 and use_mirror:
stream(" Mirror download failed, retrying with official CDN...", "yellow")
_phase(on_phase, "⚠️ 镜像下载失败,正在改用官方源重试…")
_phase(on_phase, _t(
"⚠️ 镜像下载失败,正在改用官方源重试…",
"⚠️ Mirror download failed; retrying with the official CDN…",
))
env_no_mirror = os.environ.copy()
env_no_mirror.pop("PLAYWRIGHT_DOWNLOAD_HOST", None)
ret = subprocess.call(cmd, env=env_no_mirror)
if ret != 0:
stream("Failed to install Chromium.", "red")
_phase(on_phase, "❌ [3/3] Chromium 安装失败。")
_phase(on_phase, _t("❌ [3/3] Chromium 安装失败。", "❌ [3/3] Failed to install Chromium."))
return 1
stream("")
_phase(on_phase, "✅ [3/3] Chromium 已安装。")
_phase(on_phase, _t("✅ [3/3] Chromium 已安装。", "✅ [3/3] Chromium installed."))
stream("Verifying browser installation...", None)
_phase(on_phase, "🔍 正在验证 Playwright 能否正常加载…")
_phase(on_phase, _t("🔍 正在验证 Playwright 能否正常加载…", "🔍 Verifying that Playwright loads correctly…"))
ret = subprocess.call(
[python, "-c", "from playwright.sync_api import sync_playwright; print('OK')"],
stderr=subprocess.DEVNULL,
@@ -240,14 +273,20 @@ def run_install_browser(
" Consider upgrading your OS or using Docker.",
"yellow",
)
_phase(on_phase, "⚠️ 验证未完全通过:本机可能仍无法使用浏览器工具,请查看日志或升级系统。")
_phase(on_phase, _t(
"⚠️ 验证未完全通过:本机可能仍无法使用浏览器工具,请查看日志或升级系统。",
"⚠️ Verification did not fully pass: the browser tool may still not work here; check the log or upgrade your system.",
))
else:
stream(" Verification passed.", "green")
_phase(on_phase, "✅ 验证通过。")
_phase(on_phase, _t("✅ 验证通过。", "✅ Verification passed."))
stream("")
stream("Browser tool ready! Restart CowAgent to enable it.", "green")
_phase(on_phase, "🎉 全部步骤结束。请重启 CowAgent 后使用 browser 工具。")
_phase(on_phase, _t(
"🎉 全部步骤结束。请重启 CowAgent 后使用 browser 工具。",
"🎉 All steps finished. Restart CowAgent to use the browser tool.",
))
return 0

View File

@@ -275,7 +275,11 @@ def update(ctx):
def status():
"""Show CowAgent running status."""
from cli import __version__
from cli.utils import load_config_json
from cli.utils import load_config_json, get_cli_language
from common import i18n
get_cli_language() # resolve cow_lang so i18n.t reflects config
_t = i18n.t
pid = _read_pid()
if pid:
@@ -283,17 +287,17 @@ def status():
else:
click.echo(click.style("● CowAgent is not running", fg="red"))
click.echo(f" 版本: v{__version__}")
click.echo(_t(f" 版本: v{__version__}", f" Version: v{__version__}"))
cfg = load_config_json()
if cfg:
channel = cfg.get("channel_type", "unknown")
if isinstance(channel, list):
channel = ", ".join(channel)
click.echo(f" 通道: {channel}")
click.echo(f" 模型: {cfg.get('model', 'unknown')}")
click.echo(_t(f" 通道: {channel}", f" Channel: {channel}"))
click.echo(_t(f" 模型: {cfg.get('model', 'unknown')}", f" Model: {cfg.get('model', 'unknown')}"))
mode = "Chat" if cfg.get("agent") is False else "Agent"
click.echo(f" 模式: {mode}")
click.echo(_t(f" 模式: {mode}", f" Mode: {mode}"))
@click.command()

View File

@@ -517,18 +517,24 @@ def _install_targz_bytes(content: bytes, name: str, skills_dir: str, result: Ins
def _print_install_success(name: str, source: str):
"""Print a unified install success message with description and source."""
from cli.utils import get_cli_language
from common import i18n
get_cli_language() # resolve cow_lang so i18n.t reflects config
_t = i18n.t
skills_dir = get_skills_dir()
config = load_skills_config()
display = config.get(name, {}).get("display_name", "")
desc = _read_skill_description(os.path.join(skills_dir, name))
click.echo(click.style(f"{name}", fg="green"))
if display and display != name:
click.echo(f" 名称: {display}")
click.echo(_t(f" 名称: {display}", f" Name: {display}"))
if desc:
if len(desc) > 60:
desc = desc[:57] + ""
click.echo(f" 描述: {desc}")
click.echo(f" 来源: {source}")
click.echo(_t(f" 描述: {desc}", f" Description: {desc}"))
click.echo(_t(f" 来源: {source}", f" Source: {source}"))
def _validate_skill_name(name: str):

View File

@@ -40,6 +40,22 @@ def load_config_json() -> dict:
return {}
def get_cli_language() -> str:
"""Resolve the CLI UI language using the shared i18n detector.
Reads the `cow_lang` field from config.json (defaults to "auto") and runs
the same detection used by the running app, so CLI output matches.
"""
ensure_sys_path()
try:
from common import i18n
configured = load_config_json().get("cow_lang", "auto")
return i18n.resolve_language(configured)
except Exception:
return "en"
def load_skills_config() -> dict:
"""Load skills_config.json from the custom skills directory."""
path = os.path.join(get_skills_dir(), "skills_config.json")