ai-i18n-sync
v1.0.0
Published
AI-powered i18n translation sync tool. Detect missing keys → Translate → Sync.
Downloads
20
Maintainers
Readme
ai-i18n
AI 驱动的国际化翻译同步工具 —— 检测缺失 key → AI 翻译 → 同步写入,一条命令搞定。
English | 中文
特性
- 增量翻译 — 只翻译缺失的 key,不重复翻译已有内容
- 多 AI 模型 — OpenAI / DeepSeek / Claude / 通义千问 / Gemini / Ollama
- 完整性检查 — 一键检查所有语言文件的 key 缺失和多余
- 术语表 — 确保专业术语翻译一致("订单" 始终翻译为 "Order")
- 嵌套 key — 完整支持
common.buttons.submit嵌套结构 - 保留格式 — key 顺序与主文件一致,最小化 git diff
- 快速翻译 —
translate命令秒级翻译单条文本 - 零依赖 — 无 required dependencies,
npx直接用
快速开始
# 1. 配置 API Key
echo 'DEEPSEEK_API_KEY=sk-xxx' >> .env.local
# 2. 同步翻译
npx ai-i18n-sync sync --source locales/zh.json --targets en,ja
# 3. 检查完整性
npx ai-i18n-sync check安装
# 方式一:项目级安装
npm install -D ai-i18n-sync
# 方式二:全局安装
npm install -g ai-i18n-sync
# 方式三:npx 直接用
npx ai-i18n-sync安装后可使用短名 ai-i18n 替代 ai-i18n-sync。
命令
sync — 同步翻译
以主语言文件为基准,AI 翻译缺失的 key 并写入目标文件。
npx ai-i18n sync --source locales/zh.json --targets en,ja
npx ai-i18n sync --source zh.json --targets en.json --dry-run
npx ai-i18n sync --context "跨境电商平台"| 参数 | 说明 |
|------|------|
| --source <file> | 主语言文件路径 |
| --targets <list> | 目标语言(逗号分隔:en,ja,ko 或文件路径) |
| --context <text> | 翻译上下文(提升翻译准确度) |
| --dry-run | 只展示,不写入文件 |
运行效果
$ npx ai-i18n sync --source locales/zh.json --targets en
🔍 正在扫描 i18n 文件...
📄 Source: zh.json (142 keys)
📄 Target: en.json (130 keys) ⚠️ 12 missing
📋 发现 12 个缺失 key (en.json):
+ mall.order.refund_pending "退款审核中"
+ mall.order.refund_approved "退款已通过"
+ mall.coupon.claim_success "领取成功"
... (9 more)
🤖 正在翻译 12 个 key → en...
mall.order.refund_pending: "退款审核中" → "Refund Under Review"
mall.order.refund_approved: "退款已通过" → "Refund Approved"
mall.coupon.claim_success: "领取成功" → "Claimed Successfully"
... (9 more)
✅ 已同步 12 个 key → en.json
🎉 翻译同步完成,共 12 个 key。check — 检查完整性
npx ai-i18n check
npx ai-i18n check --strict # 多余 key 也报错🔍 正在检查 i18n 完整性...
zh.json 142 keys (source)
en.json 130 keys ⚠️ 12 missing
ja.json 128 keys ⚠️ 14 missing
ko.json 142 keys ✅
❌ 存在缺失 key,运行 `ai-i18n sync` 修复。translate — 快速翻译
npx ai-i18n translate "用户管理" --to en,ja
npx ai-i18n translate "退款审核" --to en --context "电商平台"🤖 翻译: "用户管理"
en: User Management
ja: ユーザー管理init — 初始化配置
npx ai-i18n init # 生成 .ai-i18n.json + .ai-i18n-glossary.json配置文件
运行 ai-i18n init 生成 .ai-i18n.json:
{
"sourceLocale": "zh", // 主语言
"targetLocales": ["en"], // 目标语言列表
"localesDir": "src/locales", // i18n 文件目录
"filePattern": "{locale}.json", // 文件命名规则
"nested": true, // 嵌套结构
"sortKeys": false, // 输出时 key 排序
"context": "", // 翻译上下文
"glossary": ".ai-i18n-glossary.json", // 术语表路径
"batchSize": 30, // 每次 API 调用翻译的 key 数
"model": "" // 指定模型
}术语表
.ai-i18n-glossary.json 确保专业术语翻译一致:
{
"zh-en": {
"订单": "Order",
"优惠券": "Coupon",
"购物车": "Shopping Cart",
"退款": "Refund"
},
"zh-ja": {
"订单": "注文",
"优惠券": "クーポン"
}
}支持的 AI 模型
| Provider | 默认模型 | 环境变量 |
|----------|---------|---------|
| OpenAI | gpt-4o-mini | OPENAI_API_KEY |
| DeepSeek | deepseek-chat | DEEPSEEK_API_KEY |
| Claude | claude-sonnet-4-20250514 | ANTHROPIC_API_KEY |
| 通义千问 | qwen-plus | DASHSCOPE_API_KEY |
| Gemini | gemini-2.0-flash | GEMINI_API_KEY |
| Ollama | qwen2.5-coder | 无需 Key |
兼容 AI_REVIEW_* 系列环境变量。
工具链
ai-review-pipeline ── 代码质量审查
ai-git-msg ── 提交信息生成
ai-i18n-sync ── 国际化翻译同步English
What is this?
An AI-powered i18n translation sync tool. Detects missing keys → AI translates → Writes to target files. Supports glossary for consistent terminology.
Quick Start
echo 'DEEPSEEK_API_KEY=sk-xxx' >> .env.local
npx ai-i18n-sync sync --source locales/zh.json --targets en,jaCommands
| Command | Description |
|---------|-------------|
| sync | Translate and sync missing keys to target locales |
| check | Check locale files for missing/extra keys |
| translate | Quickly translate a text string |
| init | Initialize config and glossary files |
Install
npm install -D ai-i18n-sync
npx ai-i18n-syncLicense
MIT
