汉化 Claude Code 的命令提示

前言

  • 当我们在使用 Claude Code 时,发现命令提示是英文的,这给我们的使用带来了不便,所以我们就需要将命令提示翻译成中文,方便我们的使用
  • 这样对于非英文用户来说,就更方便了,每次都需要翻译命令提示,这会增加我们的工作量

思路

  • Claude Code 是基于 nodejs 的交互式工具,所以当我们通过 nodejs 安装 Claude Code 时,我们可以下载了 Claude Code 的 cli, 汉化下 cli 里的内容,这样使用的时候就显示中文了。

实现

  1. 读取文件内容
  2. 替换文件内容
  3. 写入文件内容
ts 复制代码
import { writeFileSync, readFileSync, existsSync, cpSync } from 'fs'
import { execSync } from 'child_process'
import { resolve } from 'path'

const getCliPath = () => {
  const pkgname = '@anthropic-ai/claude-code'
  try {
    const log = execSync(`npm list -g ${pkgname} --depth=0`)
    const result = log.toString().trim().includes(pkgname)
    if (!result) {
      console.error(`请使用 nodejs 安装 ${pkgname}`)
      process.exit(1)
    }
    const npmRoot = execSync('npm root -g').toString().trim()
    const cliPath = resolve(npmRoot, pkgname, 'cli.js')
    const cliPathBak = resolve(npmRoot, pkgname, 'cli.bak.js')
    !existsSync(cliPathBak) && cpSync(cliPath, cliPathBak)
    return { cliPath, cliPathBak }
  } catch {
    console.error(`请使用 nodejs 安装 ${pkgname}`)
    process.exit(1)
  }
}

/**
 * 汉化 Claude Code 命令
 */
export const useZH = async () => {
  const { cliPath } = getCliPath()
  const content = readFileSync(cliPath).toString()
  const keyword = (await import('./keyword.js')).default
  const newContent = Object.entries(keyword).reduce((prev, [key, value]) => {
    const escapedKey = key.replace(/\n/g, '\\\\n').replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
    const newValue = value.replace(/\n/g, '\\n')
    const res =
      escapedKey[0] === '`'
        ? prev.replace(new RegExp(escapedKey), value)
        : prev.replace(new RegExp(`\"${escapedKey}\"`, 'g'), `\"${newValue}\"`).replace(new RegExp(`(\'${escapedKey}\')`, 'g'), `\'${newValue}\'`)
    return res
  }, content)

  writeFileSync(cliPath, newContent)
}

/**
 * 恢复成英文 Claude Code 命令
 */
export const useZHRestore = () => {
  const { cliPath, cliPathBak } = getCliPath()
  cpSync(cliPathBak, cliPath)
}

效果

演示

sh 复制代码
# 全局安装
npm i -g mine-auto-cli

# 检查是否安装成功
auto -v

# 汉化 Claude Code 命令
auto ai zh

# 恢复成英文 Claude Code 命令
auto ai zh-restore

总结

  • 我们可以通过以上步骤,将 Claude Code 的命令提示翻译成中文,方便我们的使用
  • 通过这个思路,这样你就可以自己去翻译那些英文的命令提示,不限于 Claude Code 的命令提示
  • 源码参考
相关推荐
码哥字节16 小时前
Token Saver 省 99% token 是真的,但有个前提没人告诉你
mcp·claude code·token saver
Orange_sparkle3 天前
从 Docker 到 Doris:建立后端基础设施全景图
运维·docker·ai·容器·claude code
Akiyama_Mio-Kon3 天前
计算机每日时报(2026-08-29):AI 开始碰现实机器,编码助手先补数据与账单边界
github copilot·claude code·aws drs·chatgpt images
Akiyama_Mio-Kon3 天前
Claude Code 修复凭据文件云上传:给 AI 编程工具补一张“工作区出境”门禁
devsecops·terraform·ai 编程·claude code·凭据安全·ai agent 安全·云会话
乱世刀疤5 天前
Claude Code系统级命令全解析
人工智能·claude code
StarRocks_labs5 天前
从 6000+ Commit 中识别升级风险:一个 StarRocks AI 升级扫描工具的实现
starrocks·ai·commit·分析·claude code
乱世刀疤5 天前
Claude Code提高工作效率案例:将视频培训转化为文本资料
人工智能·claude code
学心理学的程序员9 天前
anydoc:Firecrawl 出的 Rust 文档转 Markdown,4ms 转换、14 种格式干翻 MarkItDown
开发语言·后端·rust·开源·firecrawl·claude code·anydoc
peijiping11 天前
AI多智能体解惑:父子子智能体 vs 团队智能体,为什么主流IDE默认只用前者?
人工智能·ai agent·claude code
creator_Li11 天前
Claude Code 源码分析(九):子 Agent 如何分叉、继续与回到父会话
claude code