@agentscope-ai/i18n 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,11 +1,11 @@
1
- # @agentscope-ai/i18n
1
+ # @ali/agentscope-ai-i18n
2
2
 
3
3
  前端代码仓库中文国际化工具,支持 AI 智能翻译和多语言管理。
4
4
 
5
5
  ## 特性
6
6
 
7
7
  - 自动检测并提取中文文本
8
- - 支持 AI Agent 翻译和 MT 机器翻译两种模式
8
+ - 支持 AI Agent 翻译和 MT 机器翻译两种引擎
9
9
  - 支持 JSX/TSX/TS/JS 文件解析
10
10
  - 智能生成翻译 key(MT 模式下保留文件路径结构)
11
11
  - 支持翻译 key 使用检查与自动清理
@@ -19,7 +19,7 @@
19
19
  ## 安装
20
20
 
21
21
  ```bash
22
- npm install @agentscope-ai/i18n
22
+ npm install @ali/agentscope-ai-i18n
23
23
  ```
24
24
 
25
25
  ## 快速开始
@@ -32,7 +32,7 @@ npx i18n init-config
32
32
 
33
33
  # 3. 在 .env 文件中填写 DASHSCOPE_APIKEY
34
34
 
35
- # 4. 初始化翻译
35
+ # 4. 初始化翻译(默认使用 MT 引擎)
36
36
  npx i18n init
37
37
 
38
38
  # 5. 后续新增文案时,增量翻译
@@ -44,14 +44,14 @@ npx i18n patch
44
44
  | 命令 | 说明 |
45
45
  |------|------|
46
46
  | `npx i18n init-config` | 生成 `i18n.config.js` 配置文件 |
47
- | `npx i18n init` | 初始化翻译(MT 模式,等同于 `init-mt`) |
48
- | `npx i18n init-agent` | 初始化翻译(百炼 Agent 批量模式) |
49
- | `npx i18n init-mt` | 初始化翻译(MT 机器翻译模式) |
50
- | `npx i18n patch` | 增量翻译(MT 模式,等同于 `patch-mt`) |
51
- | `npx i18n patch-agent` | 增量翻译(百炼 Agent 批量模式) |
52
- | `npx i18n patch-mt` | 增量翻译(MT 机器翻译模式) |
47
+ | `npx i18n init` | 初始化翻译(默认 MT 引擎) |
48
+ | `npx i18n init --engine agent` | 初始化翻译(Agent 批量引擎) |
49
+ | `npx i18n patch` | 增量翻译(默认 MT 引擎) |
50
+ | `npx i18n patch --engine agent` | 增量翻译(Agent 批量引擎) |
53
51
  | `npx i18n check` | 检查翻译 key 使用状态 |
54
52
  | `npx i18n reverse` | 还原国际化代码为中文 |
53
+ | `npx i18n translate` | 三步工作流 Step 1:提取+翻译(不改源码) |
54
+ | `npx i18n replace` | 三步工作流 Step 3:替换源码 |
55
55
  | `npx i18n medusa` | 从 Medusa 平台拉取翻译文件 |
56
56
  | `npx i18n install-skill` | 安装 IDE AI 助手的 i18n-helper skill |
57
57
 
@@ -83,51 +83,27 @@ npx i18n init-config
83
83
  require('dotenv').config();
84
84
 
85
85
  module.exports = {
86
- // 不需要翻译的文件和路径列表
87
86
  doNotTranslateFiles: ['node_modules', 'dist', '.git'],
88
-
89
- // 要处理的文件类型
90
87
  fileType: '.tsx,.ts,.js,.jsx',
91
-
92
- // 需要翻译的文件目录
93
88
  targetPath: './src',
94
-
95
- // 翻译文件输出目录
96
89
  localesFilePath: './src/i18n/locales',
97
-
98
- // i18n实例文件输出目录
99
90
  i18nFilePath: './src/i18n',
100
-
101
- // 翻译key的前缀
102
91
  keyPrefix: 'app',
103
-
104
- // i18n函数导入路径
105
92
  functionImportPath: '@/i18n',
106
93
 
107
- // DashScope API配置
108
94
  dashScope: {
109
- // Agent 模式使用的工作流 appId
110
95
  keyTranslateAppId: '',
111
-
112
- // MT 模式使用的翻译工作流 appId(按语言配置)
113
96
  valueTranslateAppId: {
114
97
  'en-us': '',
115
98
  'ja-jp': '',
116
99
  },
117
-
118
- // API密钥(从 .env 读取)
119
100
  apiKey: process.env.DASHSCOPE_APIKEY,
120
-
121
- // Agent 批量翻译配置
122
101
  batchSize: 50,
123
-
124
- // MT 翻译配置
125
- mtConcurrency: 1, // 并发数
126
- mtDelay: 1000, // 批次间延迟(ms)
127
- mtBatchSize: 25, // 每批翻译条数
102
+ mtConcurrency: 1,
103
+ mtDelay: 1000,
104
+ mtBatchSize: 25,
128
105
  },
129
106
 
130
- // Medusa 翻译平台配置(可选)
131
107
  medusa: {
132
108
  appName: '',
133
109
  appId: '',
@@ -147,54 +123,41 @@ module.exports = {
147
123
 
148
124
  ## 命令详解
149
125
 
150
- ### init / init-mt 初始化翻译(MT 模式)
126
+ ### init — 初始化翻译
151
127
 
152
128
  ```bash
153
- npx i18n init
129
+ npx i18n init # 默认使用 MT 引擎
130
+ npx i18n init --engine agent # 使用 Agent 批量引擎
154
131
  ```
155
132
 
156
133
  1. 扫描 `targetPath` 目录中的源文件
157
134
  2. 提取中文文案
158
- 3. 使用 MT 机器翻译逐条生成翻译 key(保留文件路径结构)
159
- 4. 翻译为配置的多语言
160
- 5. 替换代码中的中文为 `$i18n.get()` 调用
161
- 6. 询问是否生成 Medusa Excel 文件
135
+ 3. 使用选定引擎翻译生成 key 和多语言文件
136
+ 4. 替换代码中的中文为 `$i18n.get()` 调用
137
+ 5. 询问是否生成 Medusa Excel 文件
162
138
 
163
- ### init-agent初始化翻译(Agent 批量模式)
139
+ ### patch增量翻译
164
140
 
165
141
  ```bash
166
- npx i18n init-agent
167
- ```
168
-
169
- 使用百炼 Agent 工作流进行批量翻译,适合大量文案的初始化场景。
170
-
171
- ### patch / patch-mt — 增量翻译(MT 模式)
172
-
173
- ```bash
174
- npx i18n patch
142
+ npx i18n patch # 默认使用 MT 引擎
143
+ npx i18n patch --engine agent # 使用 Agent 批量引擎
175
144
  ```
176
145
 
177
146
  只扫描和翻译新增的中文文案,更新现有翻译文件。
178
147
 
179
- ### patch-agent — 增量翻译(Agent 批量模式)
180
-
181
- ```bash
182
- npx i18n patch-agent
183
- ```
184
-
185
- 使用百炼 Agent 工作流进行批量增量翻译。
186
-
187
148
  ### check — 检查翻译状态
188
149
 
189
150
  ```bash
190
151
  npx i18n check
191
152
  npx i18n check --auto-delete-unused
153
+ npx i18n check --summary-only
192
154
  ```
193
155
 
194
156
  1. 检查代码中使用的翻译 key
195
157
  2. 对比翻译文件的完整性
196
158
  3. 显示缺失和未使用的 key
197
159
  4. `--auto-delete-unused`:自动删除未使用的 key
160
+ 5. `--summary-only`:只输出各语言缺失统计
198
161
 
199
162
  ### reverse — 还原国际化代码
200
163
 
@@ -207,6 +170,15 @@ npx i18n reverse
207
170
  3. 替换为原始中文文本
208
171
  4. 删除 `$i18n` 的导入语句
209
172
 
173
+ ### translate / replace — 三步工作流
174
+
175
+ ```bash
176
+ npx i18n translate # Step 1: 只生成 locale 文件,不修改源码
177
+ npx i18n translate -f file # 单文件模式
178
+ # Step 2: 同步到 Medusa(外部操作)
179
+ npx i18n replace # Step 3: 替换源码中的中文
180
+ ```
181
+
210
182
  ### medusa — 从 Medusa 平台拉取翻译文件
211
183
 
212
184
  ```bash
@@ -216,19 +188,6 @@ npx i18n medusa --output ./src/locales # 自定义输出目录
216
188
  npx i18n medusa --no-merge # 不合并,直接覆盖
217
189
  ```
218
190
 
219
- 支持的选项:
220
-
221
- | 选项 | 说明 |
222
- |------|------|
223
- | `-c, --config <path>` | 配置文件路径 |
224
- | `-o, --output <dir>` | 输出目录 |
225
- | `-a, --app <name>` | Medusa 应用名称 |
226
- | `-t, --tag <name>` | Medusa 标签名称 |
227
- | `--type <type>` | 包类型(json/android/ios) |
228
- | `--no-merge` | 不合并现有文件,直接覆盖 |
229
-
230
- 配置优先级:命令行参数 > 配置文件 > 默认值。
231
-
232
191
  ### install-skill — 安装 IDE i18n Skill
233
192
 
234
193
  ```bash
@@ -237,37 +196,32 @@ npx i18n install-skill --agent cursor qoder
237
196
  npx i18n install-skill --app-id 12345
238
197
  ```
239
198
 
240
- 为 Cursor、Qoder、Claude Code、Codex、OpenCode 等 IDE 安装 i18n-helper skill。默认自动检测已有的 IDE 配置目录。
199
+ 为 Cursor、Qoder、Claude Code、Codex、OpenCode 等 IDE 安装 i18n-helper skill
241
200
 
242
- ## 翻译模式对比
201
+ ## 翻译引擎对比
243
202
 
244
- | | Agent 批量模式 (init-agent / patch-agent) | MT 机器翻译模式 (init / patch) |
203
+ | | MT 机器翻译(默认) | Agent 批量 (`--engine agent`) |
245
204
  |---|---|---|
246
- | 翻译方式 | 批量翻译 | 逐条翻译 |
247
- | 速度 | 快(批量处理) | 较慢(支持并发控制) |
248
- | 翻译质量 | 一般 | 高(无上下文干扰) |
249
- | Key 生成 | 批量生成 | 保留文件路径结构 |
250
- | 错误隔离 | 批次粒度 | 单条粒度 |
251
- | 适用场景 | 大量文案初始化 | 高质量翻译、增量更新 |
205
+ | 翻译方式 | 逐条翻译 | 批量翻译 |
206
+ | 速度 | 较慢(支持并发控制) | 快(批量处理) |
207
+ | 翻译质量 | 高(无上下文干扰) | 一般 |
208
+ | Key 生成 | 保留文件路径结构 | 批量生成 |
209
+ | 错误隔离 | 单条粒度 | 批次粒度 |
210
+ | 适用场景 | 高质量翻译、增量更新 | 大量文案初始化 |
252
211
 
253
212
  ### 性能调优
254
213
 
255
214
  ```javascript
256
215
  dashScope: {
257
- // Agent 批量模式:调整每批数量,建议 50-200
258
- batchSize: 50,
259
-
260
- // MT 模式:需满足 1000 / mtDelay * mtConcurrency <= API rps 限制
261
- mtConcurrency: 1, // 并发数,建议 1-5
262
- mtDelay: 1000, // 批次间延迟(ms)
263
- mtBatchSize: 25, // 每次 API 调用翻译条数
216
+ batchSize: 50, // Agent 批量模式:每批数量,建议 50-200
217
+ mtConcurrency: 1, // MT 模式并发数,建议 1-5
218
+ mtDelay: 1000, // MT 批次间延迟(ms)
219
+ mtBatchSize: 25, // MT 每次 API 调用翻译条数
264
220
  }
265
221
  ```
266
222
 
267
223
  ## 忽略功能
268
224
 
269
- 工具支持三种级别的忽略控制,优先级从高到低:
270
-
271
225
  ### 1. 文件级别忽略
272
226
 
273
227
  在文件**第一行**添加注释,跳过整个文件:
@@ -275,46 +229,24 @@ dashScope: {
275
229
  ```jsx
276
230
  // @i18n-ignore
277
231
  import React from 'react';
278
-
279
- const Component = () => {
280
- return <div>这个文件中的所有中文都不会被处理</div>;
281
- };
232
+ const Component = () => <div>这个文件中的所有中文都不会被处理</div>;
282
233
  ```
283
234
 
284
235
  ### 2. 块级别忽略
285
236
 
286
- 使用 `@i18n-ignore-block-start` / `@i18n-ignore-block-end` 包裹代码块:
287
-
288
237
  ```jsx
289
238
  {/* @i18n-ignore-block-start */}
290
239
  <div>这个块中的所有文本都不会被处理</div>
291
- <span>这个也不会被处理</span>
292
240
  {/* @i18n-ignore-block-end */}
293
241
  ```
294
242
 
295
243
  ### 3. 行级别忽略
296
244
 
297
- 在行尾添加 `@i18n-ignore-line` 注释:
298
-
299
245
  ```jsx
300
246
  <p>这行文本不会被处理 {/* @i18n-ignore-line */}</p>
301
247
  const message = '这个变量不会被处理'; // @i18n-ignore-line
302
248
  ```
303
249
 
304
- ## 多语言支持
305
-
306
- 在 `valueTranslateAppId` 中配置各语言对应的工作流 appId:
307
-
308
- ```javascript
309
- valueTranslateAppId: {
310
- 'en-us': 'english-app-id',
311
- 'ja-jp': 'japanese-app-id',
312
- 'ko-kr': 'korean-app-id',
313
- }
314
- ```
315
-
316
- 每种语言会生成对应的翻译文件(`zh-cn.json`、`en-us.json`、`ja-jp.json` 等)。
317
-
318
250
  ## 代码示例
319
251
 
320
252
  ### 翻译前
@@ -347,19 +279,16 @@ const Form = () => {
347
279
  };
348
280
  ```
349
281
 
350
- ### 还原后
282
+ ## 从旧版迁移
351
283
 
352
- ```tsx
353
- const Form = () => {
354
- return (
355
- <form>
356
- <label>用户名:</label>
357
- <input placeholder="请输入用户名" />
358
- <button>提交</button>
359
- </form>
360
- );
361
- };
362
- ```
284
+ 以下命令已弃用,但仍可使用(会提示迁移):
285
+
286
+ | 旧命令 | 新命令 |
287
+ |--------|--------|
288
+ | `npx i18n init-mt` | `npx i18n init` |
289
+ | `npx i18n init-agent` | `npx i18n init --engine agent` |
290
+ | `npx i18n patch-mt` | `npx i18n patch` |
291
+ | `npx i18n patch-agent` | `npx i18n patch --engine agent` |
363
292
 
364
293
  ## 注意事项
365
294
 
@@ -368,21 +297,14 @@ const Form = () => {
368
297
  3. 忽略标记必须出现在注释中,字符串中的标记不会被识别
369
298
  4. Medusa 拉取功能需要确保网络连接到 Medusa 平台
370
299
  5. MT 翻译的并发参数需满足 `1000 / mtDelay * mtConcurrency <= API rps 限制`
371
- 6. 错误日志自动记录到 `logs/` 目录下(`api-errors.log`、`translation-errors.log`、`mt-translation-errors.log`)
300
+ 6. 错误日志自动记录到 `logs/` 目录下
372
301
 
373
302
  ## 开发
374
303
 
375
304
  ```bash
376
- # 安装依赖
377
305
  pnpm install
378
-
379
- # 运行测试
380
306
  pnpm test
381
-
382
- # 代码格式化
383
307
  pnpm run format
384
-
385
- # 代码检查
386
308
  pnpm run lint
387
309
  ```
388
310
 
@@ -0,0 +1,106 @@
1
+ // 注意:这里是 i18n 脚本(node 环境)使用的配置文件,不会打包进前端产物。
2
+ // 为避免将敏感信息(如 API Key)提交到仓库,使用 dotenv 从本地 .env 读取。
3
+ require('dotenv').config();
4
+
5
+ module.exports = {
6
+ // 不需要翻译的文件和路径列表
7
+ doNotTranslateFiles: ['node_modules', 'dist', '.git', 'tests/locales'],
8
+
9
+ // 要处理的文件类型
10
+ fileType: '.tsx,.ts,.js,.jsx',
11
+
12
+ // 需要翻译的文件目录
13
+ targetPath: './tests',
14
+
15
+ // 翻译文件输出目录
16
+ localesFilePath: './tests/locales',
17
+
18
+ // i18n实例文件输出目录
19
+ i18nFilePath: './tests/i18n',
20
+
21
+ // 翻译key的前缀
22
+ keyPrefix: 'test',
23
+
24
+ // i18n函数导入路径
25
+ functionImportPath: '@/tests/i18n',
26
+
27
+ // 自定义 i18n 调用表达式配置
28
+ // 默认生成 $i18n.get({ id, dm }) 形式(成员调用)
29
+ //
30
+ // 方式一:成员调用 —— objectName.methodName({ id, dm })
31
+ // callExpression: {
32
+ // objectName: '$i18n', // 调用对象名,如 $i18n、intl
33
+ // methodName: 'get', // 方法名,如 get、t
34
+ // importName: '$i18n', // import 绑定名,如 import $i18n from '...'
35
+ // },
36
+ //
37
+ // 方式二:直接函数调用 —— functionName({ id, dm })
38
+ // callExpression: {
39
+ // functionName: 'translate', // 函数名,如 translate、t、$t
40
+ // importName: 'translate', // import 绑定名,如 import translate from '...'
41
+ // },
42
+
43
+ // DashScope API配置
44
+ dashScope: {
45
+ // 翻译key的大模型工作流agent appId,如果不使用 init-agent/patch-agent,则不需要配置
46
+ keyTranslateAppId: '',
47
+
48
+ // 翻译value的大模型工作流appId,如果不使用 init-mt/patch-mt,则不需要配置
49
+ valueTranslateAppId: {
50
+ 'en-us': '',
51
+ 'ja-jp': '',
52
+ },
53
+
54
+ // API密钥
55
+ apiKey: process.env.DASHSCOPE_APIKEY,
56
+
57
+ // 批量翻译配置(适用于init和patch)
58
+ batchSize: 50, // 每批处理50条文案,默认100
59
+
60
+ // MT翻译配置(适用于init-mt和patch-mt, 1000 / mtDelay * mtConcurrency 需要满足基模调用的 rps 限制)
61
+ mtConcurrency: 1, // MT翻译并发数为3,默认1
62
+ mtDelay: 1000, // MT翻译批次间延迟200ms,默认100ms
63
+ mtBatchSize: 25, // MT翻译批次大小,默认10
64
+ },
65
+
66
+ // check 命令配置
67
+ check: {
68
+ // 需要检查的语言列表(默认从 dashScope.valueTranslateAppId 推断)
69
+ // 配置后优先使用此处的语言列表
70
+ languages: ['zh-cn', 'en-us', 'ja-jp'],
71
+ },
72
+
73
+ // medusa 配置,用于生成 excel 文件和从平台拉取翻译文件
74
+ medusa: {
75
+ // 应用名称
76
+ appName: '',
77
+
78
+ // 应用ID
79
+ appId: '',
80
+
81
+ // 标签名称(用于拉取文件)
82
+ tag: '',
83
+
84
+ // 包类型(用于拉取文件)json/android/ios
85
+ type: 'json',
86
+
87
+ // 输出目录(用于拉取文件)
88
+ outputPath: './locales',
89
+
90
+ // 是否合并现有文件(用于拉取文件)
91
+ mergeExisting: true,
92
+
93
+ // medusa group id(用于生成 Excel 文件)
94
+ group: '',
95
+
96
+ // 语言映射配置(用于生成 Excel 文件)
97
+ keyMap: {
98
+ 'en-us': 'English',
99
+ 'ja-jp': 'Japanese',
100
+ 'zh-cn': 'Simplified Chinese',
101
+ },
102
+
103
+ // bucId(从 .env 中的 BUC_ID 读取)
104
+ bucId: process.env.BUC_ID,
105
+ },
106
+ };