汉化 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 的命令提示
  • 源码参考
相关推荐
IT 行者17 小时前
Claude Code 源码解读 06:权限系统与 Hooks——安全与自动化的基石
ai编程·源码解读·claude code
数据知道18 小时前
claw-code 源码分析:大型移植的测试哲学——如何用 unittest 门禁守住「诚实未完成」的口碑?
开发语言·python·ai·claude code·claw code
数据知道18 小时前
claw-code 源码分析:结构化输出与重试——`structured_output` 一类开关如何改变「可解析性」与失败语义?
算法·ai·claude code·claw code
小程故事多_8019 小时前
AI Coding 工程化革命,Superpowers 管流程,ui-ux-pro-max 管质感
人工智能·ui·架构·aigc·ai编程·ux·claude code
SpikeKing20 小时前
VibeCoding - 配置 Claude Code 接入 Claude 模型
claude·vibecoding·claude code
fly_over21 小时前
Claude Code 从零复刻教程 第 1 篇:项目初始化与 CLI 骨架
学习·源码·ai编程·工具·命令行·claude code
阿酷tony1 天前
Claude Code 与 OpenAI Codex 的对比
claude·claude code
数据知道1 天前
claw-code 源码详细分析:compat-harness——对接编辑器生态时,兼容层该吞掉哪些「历史包袱」?
ai·编辑器·claude code·claw code
樂油2 天前
Claude Code的安装并连接VScode(使用CC Switch或ollama连接)
vscode·ollama·claude code·cc switch
How_doyou_do2 天前
claude code源码万字深入分析
agent·claude code