UNPKG

@alauda-fe/i18n-tools

Version:

基于 Azure OpenAI 的 JSON i18n 文件翻译和英文语法检查工具集

143 lines (131 loc) 5.3 kB
#!/usr/bin/env node import { program } from "commander"; import { fileURLToPath } from "url"; import { dirname, join } from "path"; import { readFileSync } from "fs"; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); const packagePath = join(__dirname, "..", "package.json"); const packageJson = JSON.parse(readFileSync(packagePath, "utf8")); program .name("i18n-tools") .description("Azure OpenAI JSON翻译和语法检查工具") .version(packageJson.version); // 添加翻译命令 program .command("translate") .description("翻译JSON文件") .option("-s, --source <lang>", "源语言目录,默认为en", "en") .requiredOption("-t, --target <lang>", "目标语言目录(必填)") .option( "-k, --token <key>", "Azure OpenAI API 密钥(或设置环境变量 OPENAI_API_KEY)" ) .option("--custom-prompt <path>", "自定义系统提示词文件路径") .option("--extra-rules <text>", "额外的处理规则(会添加到默认规则之后)") .action(async (options) => { const { translateCommand } = await import("../lib/translate.js"); await translateCommand(options); }); // 添加语法检查命令 program .command("grammar-check") .description("检查和修正英文语法") .requiredOption("-f, --file <path>", "要检查的文件路径(必填)") .option( "-k, --token <key>", "Azure OpenAI API 密钥(或设置环境变量 OPENAI_API_KEY)" ) .option("--dry-run", "预览模式,不修改文件", false) .option("--custom-prompt <path>", "自定义系统提示词文件路径") .option("--extra-rules <text>", "额外的检查规则(会添加到默认规则之后)") .action(async (options) => { const { grammarCheckCommand } = await import("../lib/grammar-check.js"); await grammarCheckCommand(options); }); // 添加批量语法检查命令 program .command("batch-grammar-check") .description("批量检查目录中的所有JSON文件") .requiredOption("-d, --dir <path>", "要检查的目录路径(必填)") .option( "-k, --token <key>", "Azure OpenAI API 密钥(或设置环境变量 OPENAI_API_KEY)" ) .option("--dry-run", "预览模式,不修改文件", false) .option("--parallel <num>", "并行处理数量", "1") .option("--custom-prompt <path>", "自定义系统提示词文件路径") .option("--extra-rules <text>", "额外的检查规则(会添加到默认规则之后)") .action(async (options) => { const { batchGrammarCheckCommand } = await import( "../lib/grammar-check.js" ); await batchGrammarCheckCommand(options); }); // 添加重试失败翻译命令 program .command("retry-failed") .description("重试失败的翻译条目") .option("-s, --source <lang>", "源语言目录,默认为en", "en") .requiredOption("-t, --target <lang>", "目标语言目录(必填)") .option( "-k, --token <key>", "Azure OpenAI API 密钥(或设置环境变量 OPENAI_API_KEY)" ) .option("--custom-prompt <path>", "自定义系统提示词文件路径") .option("--extra-rules <text>", "额外的处理规则(会添加到默认规则之后)") .action(async (options) => { const { retryFailedCommand } = await import("../lib/translate.js"); await retryFailedCommand(options); }); // 添加增量翻译命令 program .command("incremental-translate") .description("基于new和snapshot目录进行增量翻译") .option("-s, --source <lang>", "源语言目录,默认为en", "en") .requiredOption("-t, --target <lang>", "目标语言目录(必填)") .option( "-k, --token <key>", "Azure OpenAI API 密钥(或设置环境变量 OPENAI_API_KEY)" ) .option("--custom-prompt <path>", "自定义系统提示词文件路径") .option("--extra-rules <text>", "额外的处理规则(会添加到默认规则之后)") .action(async (options) => { const { incrementalTranslateCommand } = await import("../lib/translate.js"); await incrementalTranslateCommand(options); }); // 添加更新快照命令 program .command("update-snapshot") .description("将new目录的文件复制到snapshot目录") .option("-s, --source <lang>", "源语言目录,默认为en", "en") .action(async (options) => { const { updateSnapshotCommand } = await import("../lib/translate.js"); await updateSnapshotCommand(options); }); // 添加同步命令 program .command("sync") .description("从GitLab仓库同步i18n文件到本地 (默认只下载level=core的仓库)") .option("-r, --repo <name>", "指定要下载的仓库名 (可多个)") .option("-l, --level <level>", "根据 level 字段过滤仓库 (默认: core)") .option("-b, --branch <branch>", "指定分支") .option( "-t, --token <token>", "GitLab Access Token (或设置环境变量 GITLAB_TOKEN)" ) .action(async (options) => { // 将单个 repo 值转换为数组 if (options.repo) { if (Array.isArray(options.repo)) { options.repos = options.repo; } else { options.repos = [options.repo]; } } else { options.repos = []; } const { syncCommand } = await import("../lib/sync.js"); await syncCommand(options); }); program.parse();