汉化 Claude Code 的命令提示
前言
- 当我们在使用 Claude Code 时,发现命令提示是英文的,这给我们的使用带来了不便,所以我们就需要将命令提示翻译成中文,方便我们的使用
- 这样对于非英文用户来说,就更方便了,每次都需要翻译命令提示,这会增加我们的工作量
思路
- Claude Code 是基于 nodejs 的交互式工具,所以当我们通过 nodejs 安装 Claude Code 时,我们可以下载了 Claude Code 的 cli, 汉化下 cli 里的内容,这样使用的时候就显示中文了。
实现
- 读取文件内容
- 替换文件内容
- 写入文件内容
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-h5-ui
# 检查是否安装成功
auto -v
# 汉化 Claude Code 命令
auto ai zh
# 恢复成英文 Claude Code 命令
auto ai zh-restore总结
- 我们可以通过以上步骤,将 Claude Code 的命令提示翻译成中文,方便我们的使用
- 通过这个思路,这样你就可以自己去翻译那些英文的命令提示,不限于 Claude Code 的命令提示
- 源码参考