汉化 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 的命令提示
  • 源码参考
相关推荐
一碗面42134 分钟前
Claude Code Subagent:从 Prompt 进化到 AI 团队协作系统
ai编程·claude code
魏杨杨10 小时前
一个程序员眼中的 AI 核心概念,讲透 LLM 、Agent 、MCP 、Skill 、RAG...
ai·.net·agent·claude code
prog_610319 小时前
【笔记】用cursor手搓cursor(六)deepseek v4
人工智能·笔记·agent·deepseek·claude code
清风丿缠绕心扉1 天前
cc-connect 连接 Claude Code 与飞书全流程
飞书·claude code·cc-connect
梦想的初衷~1 天前
claude code、codex双AI协同高水平论文撰写与质量校准:数据分析→论文初稿→交叉审稿全流程
人工智能·生物信息·实战教程·临床医学·claude code·codex cli·认知颠覆
Canicer2 天前
【国内安装 Claude Code CLI版本纯净完整指南】
ai·agent·claude code
weixin_492722822 天前
Baklib 隆重推出 CLI命令行工具
cli·baklib
嘛也学不会3 天前
claude code 出现Claude Code Web Fetch问题
claude code·claude code web fetch