@ansstory/hias 1.0.6 → 1.0.8

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,14 +1,14 @@
1
1
  # hias-cli
2
2
 
3
- > Personal scaffolding CLI with built-in i18n translation toolkit.
3
+ > 个人脚手架 CLI,内置国际化翻译工具链。
4
4
 
5
- ## Install
5
+ ## 安装
6
6
 
7
7
  ```sh
8
8
  npm i @ansstory/hias -g
9
9
  ```
10
10
 
11
- ## Usage
11
+ ## 使用
12
12
 
13
13
  ```
14
14
  hias <command> [options]
@@ -16,17 +16,17 @@ hias <command> [options]
16
16
 
17
17
  ---
18
18
 
19
- ## Quick Start (i18n Translation)
19
+ ## 快速开始(国际化翻译)
20
20
 
21
- ### 1. Set up API credentials
21
+ ### 1. 配置 API 密钥
22
22
 
23
- Register a translation provider and create a config file in your project:
23
+ 注册一个翻译服务商,然后在项目中生成配置文件:
24
24
 
25
25
  ```sh
26
26
  hias setting
27
27
  ```
28
28
 
29
- Edit `.hias/setting.json` with your `appId` and `secretKey`:
29
+ 编辑 `.hias/setting.json`,填入 `appId` `secretKey`:
30
30
 
31
31
  ```json
32
32
  {
@@ -38,56 +38,56 @@ Edit `.hias/setting.json` with your `appId` and `secretKey`:
38
38
  }
39
39
  ```
40
40
 
41
- > **No API key?** Leave both empty for fallback mode — Chinese text is used as keys directly. The i18n migration still completes.
41
+ > **没有 API 密钥?** 留空即可使用回退模式——中文原文直接作为 key,国际化迁移仍然可以完成。
42
42
 
43
- ### 2. Preview changes (recommended)
43
+ ### 2. 预览变更(推荐)
44
44
 
45
45
  ```sh
46
46
  hias tfo src/views myNamespace --dry-run
47
47
  ```
48
48
 
49
- Shows a line-level diff and prompts `Apply changes? (y/N)`.
49
+ 显示行级 diff 并提示 `Apply changes? (y/N)`。
50
50
 
51
- ### 3. Apply translation
51
+ ### 3. 执行翻译
52
52
 
53
53
  ```sh
54
54
  hias tfo src/views myNamespace
55
55
  ```
56
56
 
57
- Locale files are written to `.hias/lang/myNamespace/`. If `replaceOriginalFile` is `true`, source files are overwritten in place.
57
+ 语言包文件生成到 `.hias/lang/myNamespace/`。若 `replaceOriginalFile` `true`,则直接覆盖源文件。
58
58
 
59
59
  ---
60
60
 
61
- ## Commands
61
+ ## 命令
62
62
 
63
- ### `create <project>` (alias: `crt`)
63
+ ### `create <project>`(别名: `crt`)
64
64
 
65
- Create a project from a framework template.
65
+ 从框架模板创建项目。
66
66
 
67
- An interactive prompt will list all available frameworks. After selection, the template is downloaded from Gitee and placed into `<project>/`.
67
+ 交互式提示列出所有可用框架。选择后模板将从 Gitee 下载到 `<project>/` 目录。
68
68
 
69
69
  ```sh
70
70
  hias create my-app
71
71
  ```
72
72
 
73
- **Available frameworks:**
73
+ **可用框架:**
74
74
 
75
- | Name | Description |
76
- | ------------ | --------------------------- |
77
- | `vue2` | Vue 2 template |
78
- | `vue3` | Vue 3 template |
79
- | `vue-ts` | Vue 3 + TypeScript template |
80
- | `uni-vite` | uni-app + Vite template |
81
- | `nuxt-web` | Nuxt.js web template |
82
- | `vue-screen` | Vue data screen template |
83
- | `react` | React JSX template |
84
- | `react-ts` | React TypeScript template |
75
+ | 名称 | 说明 |
76
+ | ------------ | ----------------------- |
77
+ | `vue2` | Vue 2 模板 |
78
+ | `vue3` | Vue 3 模板 |
79
+ | `vue-ts` | Vue 3 + TypeScript 模板 |
80
+ | `uni-vite` | uni-app + Vite 模板 |
81
+ | `nuxt-web` | Nuxt.js 网页模板 |
82
+ | `vue-screen` | Vue 数据大屏模板 |
83
+ | `react` | React JSX 模板 |
84
+ | `react-ts` | React TypeScript 模板 |
85
85
 
86
86
  ---
87
87
 
88
88
  ### `adv <name> [options]`
89
89
 
90
- Add a Vue component file.
90
+ 添加 Vue 组件文件。
91
91
 
92
92
  ```sh
93
93
  hias adv Demo -d src/components
@@ -96,7 +96,7 @@ hias adv index -d src/views/demo
96
96
 
97
97
  ### `adr <name> [options]`
98
98
 
99
- Add a React JSX component file.
99
+ 添加 React JSX 组件文件。
100
100
 
101
101
  ```sh
102
102
  hias adr Demo -d src/components
@@ -104,7 +104,7 @@ hias adr Demo -d src/components
104
104
 
105
105
  ### `adrt <name> [options]`
106
106
 
107
- Add a React TSX component file.
107
+ 添加 React TSX 组件文件。
108
108
 
109
109
  ```sh
110
110
  hias adrt Demo -d src/components
@@ -112,7 +112,7 @@ hias adrt Demo -d src/components
112
112
 
113
113
  ### `adrd <name> [options]`
114
114
 
115
- Add a Redux JSX store file.
115
+ 添加 Redux JSX Store 文件。
116
116
 
117
117
  ```sh
118
118
  hias adrd useDemoStore -d src/store/modules
@@ -120,25 +120,25 @@ hias adrd useDemoStore -d src/store/modules
120
120
 
121
121
  ### `adrdt <name> [options]`
122
122
 
123
- Add a Redux TSX store file.
123
+ 添加 Redux TSX Store 文件。
124
124
 
125
125
  ```sh
126
126
  hias adrdt useDemoStore -d src/store/modules
127
127
  ```
128
128
 
129
- #### Common options for component commands
129
+ #### 组件命令通用选项
130
130
 
131
- | Option | Description |
132
- | ------------------- | ----------------------------------------------------------------------------- |
133
- | `-d, --dest <path>` | Target folder. Defaults to `src/components`. Accepts Windows backslash paths. |
131
+ | 选项 | 说明 |
132
+ | ------------------- | ---------------------------------------------------------- |
133
+ | `-d, --dest <path>` | 目标文件夹,默认 `src/components`,支持 Windows 反斜杠路径 |
134
134
 
135
- If `name` is `index`, the component file is named after the parent folder instead.
135
+ 如果 `name` `index`,则组件文件以父文件夹名命名。
136
136
 
137
137
  ---
138
138
 
139
139
  ### `close-port <ports...>`
140
140
 
141
- Kill processes occupying one or more ports.
141
+ 关闭占用一个或多个端口的进程。
142
142
 
143
143
  ```sh
144
144
  hias close-port 8076 8077
@@ -146,141 +146,155 @@ hias close-port 8076,8077
146
146
  hias close-port 3000
147
147
  ```
148
148
 
149
- Supports both space-separated and comma-separated port lists. Works on Windows (`netstat` + `taskkill`), macOS, and Linux (`lsof` + `kill`).
149
+ 支持空格分隔和逗号分隔的端口列表。兼容 Windows(`netstat` + `taskkill`)、macOS Linux(`lsof` + `kill`)。
150
150
 
151
151
  ---
152
152
 
153
153
  ### `lang [language]`
154
154
 
155
- Set or view the CLI language.
155
+ 设置或查看 CLI 语言。
156
156
 
157
157
  ```sh
158
158
  hias lang zh-CN
159
159
  hias lang en
160
- hias lang -l # show current language
160
+ hias lang -l # 查看当前语言
161
161
  ```
162
162
 
163
- | Option | Description |
164
- | ------------ | ------------------------------------- |
165
- | `-l, --list` | Display the currently active language |
163
+ | 选项 | 说明 |
164
+ | ------------ | ------------ |
165
+ | `-l, --list` | 显示当前语言 |
166
166
 
167
- Supported languages: `en`, `zh-CN`.
167
+ 支持的语言:`en`、`zh-CN`。
168
168
 
169
- Language preference is stored in `~/.hias-cli/config.json`.
169
+ 语言偏好存储在 `~/.hias-cli/config.json` 中。
170
170
 
171
171
  ---
172
172
 
173
- ### Translation Commands
173
+ ### 翻译命令
174
174
 
175
- The translation system extracts Chinese text from source files, replaces it with `$t()` i18n calls, and generates locale JSON files. Supports Baidu Translate API and Tencent Cloud TMT API.
175
+ 翻译系统从源文件中提取中文,替换为 `$t()` 国际化调用,并生成语言包 JSON 文件。支持百度翻译 API 和腾讯云 TMT API
176
176
 
177
177
  #### `tf [file] [name]`
178
178
 
179
- Translate a single file.
179
+ 翻译单个文件。
180
180
 
181
181
  ```sh
182
182
  hias tf src/views/index.vue vue
183
183
  hias tf src/views/index.vue vue --verbose
184
184
  ```
185
185
 
186
- | Argument / Option | Description |
187
- | -------------------- | -------------------------------------------------------------------------------------------------------------------------- |
188
- | `file` (positional) | Path to the file to translate |
189
- | `name` (positional) | i18n namespace key (defaults to parent directory name) |
190
- | `-n, --name <name>` | Alternative way to specify the namespace |
191
- | `--dry-run` | Preview changes without modifying files (shows line-level diff, prompts to apply) |
192
- | `-v, --verbose` | Show detailed per-file logs instead of a progress spinner |
193
- | `--show-extractions` | Only display all extracted Chinese texts with file paths and positions, without performing translation or making API calls |
186
+ | 参数 / 选项 | 说明 |
187
+ | -------------------- | --------------------------------------------------------------------- |
188
+ | `file`(位置参数) | 要翻译的文件路径 |
189
+ | `name`(位置参数) | 国际化命名空间 key(默认使用父目录名) |
190
+ | `-n, --name <name>` | 指定命名空间的另一种方式 |
191
+ | `--dry-run` | 预览变更但不实际修改文件(显示行级 diff,确认后执行) |
192
+ | `-v, --verbose` | 显示详细逐文件日志,替代进度 spinner |
193
+ | `--show-extractions` | 仅展示所有提取到的中文文本及其文件和位置,不执行翻译也不发送 API 请求 |
194
194
 
195
- **Supported file types:** `.vue`, `.js`, `.ts`, `.jsx`, `.tsx`, `.json`
195
+ **支持的文件类型:** `.vue`、`.js`、`.ts`、`.jsx`、`.tsx`、`.json`
196
196
 
197
197
  #### `tfo [folder] [name]`
198
198
 
199
- Recursively translate all supported files in a folder.
199
+ 递归翻译文件夹内所有支持的文件。
200
200
 
201
201
  ```sh
202
202
  hias tfo src/views views
203
203
  hias tfo src/views views --verbose
204
204
  ```
205
205
 
206
- | Argument / Option | Description |
207
- | --------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
208
- | `folder` (positional) | Path to the folder |
209
- | `name` (positional) | i18n namespace key (defaults to folder name) |
210
- | `-n, --name <name>` | Alternative way to specify the namespace |
211
- | `--dry-run` | Preview changes without modifying files (shows line-level diff, prompts to apply) |
212
- | `-v, --verbose` | Show detailed per-file logs instead of a progress spinner |
213
- | `--exclude <pattern>` | Exclude files matching a glob pattern (e.g. `**/*.test.js`, `node_modules/**`). Uses minimatch. Can be specified multiple times. |
214
- | `--show-extractions` | Only display all extracted Chinese texts with file paths and positions, without performing translation or making API calls |
206
+ | 参数 / 选项 | 说明 |
207
+ | --------------------- | --------------------------------------------------------------------------------------------------- |
208
+ | `folder`(位置参数) | 文件夹路径 |
209
+ | `name`(位置参数) | 国际化命名空间 key(默认使用文件夹名) |
210
+ | `-n, --name <name>` | 指定命名空间的另一种方式 |
211
+ | `--dry-run` | 预览变更但不实际修改文件(显示行级 diff,确认后执行) |
212
+ | `-v, --verbose` | 显示详细逐文件日志,替代进度 spinner |
213
+ | `--exclude <pattern>` | 排除匹配 glob 模式的文件(如 `**/*.test.js`、`node_modules/**`)。使用 minimatch 匹配。可多次指定。 |
214
+ | `--show-extractions` | 仅展示所有提取到的中文文本及其文件和位置,不执行翻译也不发送 API 请求 |
215
215
 
216
216
  #### `setting`
217
217
 
218
- Generate a project-level translation config file.
218
+ 生成项目级翻译配置文件。
219
219
 
220
220
  ```sh
221
221
  hias setting
222
222
  ```
223
223
 
224
- Creates `.hias/setting.json` in the current project directory.
224
+ 在当前项目目录下创建 `.hias/setting.json`。
225
225
 
226
- #### `globalsetting`
226
+ #### `global-setting`
227
227
 
228
- Generate a global translation config file.
228
+ 生成全局翻译配置文件。
229
229
 
230
230
  ```sh
231
- hias globalsetting
231
+ hias global-setting
232
232
  ```
233
233
 
234
- Creates or updates `~/.hias-cli/config.json`. Preserves any existing `language` setting.
234
+ 创建或更新 `~/.hias-cli/config.json`,保留已有的 `language` 设置。
235
+
236
+ #### `clear-config`
237
+
238
+ 删除全局配置目录 `~/.hias-cli`。
239
+
240
+ ```sh
241
+ hias clear-config
242
+ ```
243
+
244
+ 如果需要完全卸载 CLI 并清理用户配置,请执行:
245
+
246
+ ```sh
247
+ hias uninstall -g
248
+ ```
249
+
250
+ 该命令会先执行 `hias clear-config`,再执行 `npm uninstall @ansstory/hias -g`。npm v7+ 不会在卸载包时运行旧的 `preuninstall` / `postuninstall` 生命周期脚本,因此单独运行 `npm uninstall` 不会删除 home 目录下的配置文件。
235
251
 
236
252
  #### `gitignore`
237
253
 
238
- Add `.hias` to the project's `.gitignore` file. Creates `.gitignore` if it doesn't exist, or appends the entry if it already exists.
254
+ `.hias` 添加到项目的 `.gitignore`。若文件不存在则创建,若已存在则追加条目。
239
255
 
240
256
  ```sh
241
257
  hias gitignore
242
258
  ```
243
259
 
244
- After running, the `.gitignore` file will contain an entry for `.hias`.
245
-
246
260
  ---
247
261
 
248
262
  #### `rollback`
249
263
 
250
- Restore files from the last translation operation and clean up generated output.
264
+ 恢复上一次翻译操作的原文件并清理生成的输出。
251
265
 
252
- Each translation run creates a snapshot in `.hias/.langbackup/<timestamp>_<name>/`. Running `rollback` restores the most recent snapshot and removes it, preserving older snapshots for further step-by-step rollback.
266
+ 每次翻译运行会在 `.hias/.langbackup/<timestamp>_<name>/` 下创建一个快照。执行 `rollback` 会恢复最近一次快照并删除该快照,保留更早的快照供多次单步回滚。
253
267
 
254
268
  ```sh
255
269
  hias rollback
256
- hias rollback --list # list available backups
257
- hias rollback --name 20250101_120000_vue # rollback a specific backup
258
- hias rollback --keep 3 # keep only the 3 most recent backups, remove older ones
270
+ hias rollback --list # 列出所有可用备份
271
+ hias rollback --name 20250101_120000_vue # 回滚指定备份
272
+ hias rollback --keep 3 # 仅保留最近 3 个备份,删除更旧的
259
273
  ```
260
274
 
261
- **Behavior:** When `replaceOriginalFile` was `true`, original source files are restored; when it was `false`, only the output directory `.hias/lang/<name>/` is removed.
275
+ **行为:** 若当时 `replaceOriginalFile` `true`,则还原原始源文件;若为 `false`,则仅删除输出目录 `.hias/lang/<name>/`。
262
276
 
263
- **`--keep` behavior:** Backups are sorted by directory name (timestamp), the oldest ones are removed until only N remain. No action if total backups ≤ N.
277
+ **`--keep` 行为:** 按备份目录名称(时间戳)排序,删除最旧的备份,只保留最近的 N 个。若备份总数 ≤ N 则不操作。
264
278
 
265
- | Option | Description |
266
- | ---------------- | ------------------------------------------------------ |
267
- | `-l, --list` | List all available backups |
268
- | `--name <name>` | Rollback a specific backup by name |
269
- | `--keep <count>` | Keep only the N most recent backups, remove older ones |
279
+ | 选项 | 说明 |
280
+ | ---------------- | ----------------------------------- |
281
+ | `-l, --list` | 列出所有可用备份 |
282
+ | `--name <name>` | 按名称回滚指定备份 |
283
+ | `--keep <count>` | 仅保留最近 N 个备份,删除更旧的备份 |
270
284
 
271
285
  ---
272
286
 
273
- ## Configuration
287
+ ## 配置
274
288
 
275
- ### Config Cascade
289
+ ### 配置级联
276
290
 
277
- Translation settings are resolved in the following priority order:
291
+ 翻译设置按以下优先级解析:
278
292
 
279
- 1. **Project-level** — `.hias/setting.json` in the current working directory
280
- 2. **Global** — `~/.hias-cli/config.json` (created by `hias globalsetting`)
281
- 3. **Defaults**hardcoded defaults (shown below)
293
+ 1. **项目级**当前工作目录下的 `.hias/setting.json`
294
+ 2. **全局** — `~/.hias-cli/config.json`(通过 `hias global-setting` 创建)
295
+ 3. **默认值**硬编码的默认值(如下所示)
282
296
 
283
- ### Setting File Format
297
+ ### 设置文件格式
284
298
 
285
299
  ```json
286
300
  {
@@ -299,194 +313,194 @@ Translation settings are resolved in the following priority order:
299
313
  }
300
314
  ```
301
315
 
302
- ### Configuration Fields
316
+ ### 配置字段说明
303
317
 
304
- | Field | Type | Default | Description |
305
- | --------------------- | ---------- | -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
306
- | `locales` | `string[]` | `["zh-CN", "en-US"]` | Locale code list. **First entry is the source language**, the rest are target languages. E.g. `["zh-CN", "en-US", "ja-JP", "ko-KR"]` generates English, Japanese, and Korean locale files in one pass |
307
- | `outDir` | `string` | `".hias/lang"` | Output directory for generated locale files and translated copies |
308
- | `fallbackToKey` | `boolean` | `true` | Whether to generate keys when translation API is unavailable |
309
- | `replaceOriginalFile` | `boolean` | `false` | When `true`, overwrites source files in place (no copy to `outDir`) |
310
- | `provider` | `string` | `"tencent"` | Translation provider: `"baidu"` or `"tencent"` |
311
- | `i18nCallTemplate` | `string` | `"$t"` | i18n function name. When containing `{{key}}`, it's replaced with `namespace.subKey`; otherwise the function wraps it as `$t('key')` |
312
- | `extensions` | `string[]` | — | Override supported file extensions (e.g. `[".vue", ".ts"]`). Defaults to all supported types |
313
- | `appId` | `string` | `""` | Baidu AppId or Tencent SecretId |
314
- | `secretKey` | `string` | `""` | Baidu SecretKey or Tencent SecretKey |
318
+ | 字段 | 类型 | 默认值 | 说明 |
319
+ | --------------------- | ---------- | -------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
320
+ | `locales` | `string[]` | `["zh-CN", "en-US"]` | 语言代码列表。**第一个为源语言**,其余为目标语言。例如 `["zh-CN", "en-US", "ja-JP", "ko-KR"]` 会同时生成英文、日文、韩文三套语言包 |
321
+ | `outDir` | `string` | `".hias/lang"` | 生成的语言包和翻译副本的输出目录 |
322
+ | `fallbackToKey` | `boolean` | `true` | 翻译 API 不可用时是否生成 key |
323
+ | `replaceOriginalFile` | `boolean` | `false` | `true` 时直接覆盖源文件(不复制到 `outDir`) |
324
+ | `provider` | `string` | `"tencent"` | 翻译服务商:`"baidu"` `"tencent"` |
325
+ | `i18nCallTemplate` | `string` | `"$t"` | i18n 函数名。当包含 `{{key}}` 时直接替换;否则包装为 `$t('key')` |
326
+ | `extensions` | `string[]` | — | 覆盖支持的文件扩展名(如 `[".vue", ".ts"]`),默认支持所有类型 |
327
+ | `appId` | `string` | `""` | 百度 AppId 或腾讯 SecretId |
328
+ | `secretKey` | `string` | `""` | 百度 SecretKey 或腾讯 SecretKey |
315
329
 
316
- ### Translation Providers
330
+ ### 翻译服务商
317
331
 
318
- | Provider | `provider` value | `appId` | `secretKey` |
319
- | ----------------- | ---------------- | ---------------- | ----------------- |
320
- | Baidu Translate | `"baidu"` | Baidu AppId | Baidu SecretKey |
321
- | Tencent Cloud TMT | `"tencent"` | Tencent SecretId | Tencent SecretKey |
332
+ | 服务商 | `provider` | `appId` | `secretKey` |
333
+ | ---------- | ------------- | ------------- | -------------- |
334
+ | 百度翻译 | `"baidu"` | 百度 AppId | 百度 SecretKey |
335
+ | 腾讯云 TMT | `"tencent"` | 腾讯 SecretId | 腾讯 SecretKey |
322
336
 
323
- Leave both `appId` and `secretKey` empty to use fallback mode (Chinese text is used as-is for both key and value in locale files).
337
+ `appId` `secretKey` 都留空则使用回退模式(中文原文同时作为 key value)。
324
338
 
325
- > **Config validation:** Settings are automatically validated when loaded (e.g., `locales` must be an array, `provider` must be a supported enum value, `appId`/`secretKey` must be strings). Invalid values trigger a warning and are reset to defaults without interrupting the workflow.
339
+ > **配置校验:** 加载配置时会自动校验字段格式(如 `locales` 是否为数组、`provider` 是否为支持的枚举值、`appId`/`secretKey` 是否为字符串等)。格式不符合时将发出警告并回退至默认值,不会中断流程。
326
340
 
327
- ### API Provider Setup
341
+ ### 翻译服务商配置
328
342
 
329
- #### Baidu Translate
343
+ #### 百度翻译
330
344
 
331
- 1. Visit [Baidu Translate Open Platform](https://fanyi-api.baidu.com/) and register
332
- 2. Create an application to get **AppId** and **SecretKey**
333
- 3. Set `"provider": "baidu"` in config
345
+ 1. 访问 [百度翻译开放平台](https://fanyi-api.baidu.com/) 注册账号
346
+ 2. 创建应用获取 **AppId** **SecretKey**
347
+ 3. 配置文件中设置 `"provider": "baidu"`
334
348
 
335
- #### Tencent Cloud TMT
349
+ #### 腾讯云 TMT
336
350
 
337
- 1. Visit [Tencent Cloud TMT](https://console.cloud.tencent.com/tmt) and enable the service
338
- 2. Create an API key to get **SecretId** (→ `appId`) and **SecretKey**
339
- 3. Set `"provider": "tencent"` in config
351
+ 1. 访问 [腾讯云 TMT](https://console.cloud.tencent.com/tmt) 开通服务
352
+ 2. 创建 API 密钥获取 **SecretId**(填入 `appId`)和 **SecretKey**
353
+ 3. 配置文件中设置 `"provider": "tencent"`
340
354
 
341
355
  ---
342
356
 
343
- ## Output Structure
357
+ ## 输出结构
344
358
 
345
- After translation, the following directory structure is created (assuming `locales: ["zh-CN", "en-US", "ja-JP"]`):
359
+ 翻译完成后会创建以下目录结构(假设 `locales: ["zh-CN", "en-US", "ja-JP"]`):
346
360
 
347
361
  ```
348
362
  .hias/
349
- ├── setting.json # project-level config
363
+ ├── setting.json # 项目级配置文件
350
364
  ├── lang/
351
- │ └── <namespace>/
352
- │ ├── zh-CN.json # source locale
353
- │ ├── en-US.json # target locale
354
- │ ├── ja-JP.json # target locale
355
- │ └── <file>.vue # translated copy (only if replaceOriginalFile=false)
356
- ├── .translation-cache.json # translation cache (auto-generated)
365
+ │ └── <命名空间>/
366
+ │ ├── zh-CN.json # 源语言包
367
+ │ ├── en-US.json # 目标语言包
368
+ │ ├── ja-JP.json # 目标语言包
369
+ │ └── <file>.vue # 翻译副本(仅 replaceOriginalFile=false 时)
370
+ ├── .translation-cache.json # 翻译缓存(自动生成)
357
371
  └── .langbackup/
358
- └── <timestamp>_<namespace>/
359
- └── <file>.vue.bak # original file backup
372
+ └── <时间戳>_<命名空间>/
373
+ └── <file>.vue.bak # 原始文件备份
360
374
  ```
361
375
 
362
- Locale files use a nested `{ namespace: { key: value } }` format, making them ready for use with `vue-i18n` or similar libraries via module registration.
376
+ 语言包采用 `{ namespace: { key: value } }` 嵌套格式,可直接用于 `vue-i18n` 等库的模块注册。
363
377
 
364
378
  ---
365
379
 
366
- ## Translation Behavior
380
+ ## 翻译行为
367
381
 
368
- ### Source Code Extraction
382
+ ### 源码提取
369
383
 
370
- The tool scans source files and extracts Chinese text from:
384
+ 工具扫描源文件并从以下位置提取中文:
371
385
 
372
- - **Quoted strings**: `'中文'`, `"中文"`, `` `中文{expr}文` `` (template literals with expressions are split, only static Chinese segments extracted)
373
- - **Vue templates**: `{{ '中文' }}`, `<div>中文</div>`, `:title="'中文'"`
374
- - **Vue complex expressions**: `{{ isHidden || '中文' }}`, `{{ isHidden ?? '中文' }}` (extracts inline quoted strings from expressions)
375
- - **Vue directives**: `v-if="text1 == '中文'"`, `v-for="..."` (extracts inline strings from directive expressions)
376
- - **JSX/TSX text nodes**: `<span>中文 {name}</span>` (mixed text with expressions supported)
377
- - **JSX/TSX attributes**: `<div title="中文">` or `<div title={'中文'}>`
378
- - **JSON values**: `"key": "中文值"`
379
- - **Plain HTML attributes**: `<div title="兴趣">` in Vue templates
386
+ - **引号字符串**:`'中文'`、`"中文"`、`` `中文{expr}文` ``(模板字面量含表达式时拆分,仅提取静态中文段)
387
+ - **Vue 模板**:`{{ '中文' }}`、`<div>中文</div>`
388
+ - **Vue 复杂表达式**:`{{ isHidden || '中文' }}`、`{{ isHidden ?? '中文' }}`(提取表达式中的引号字符串)
389
+ - **Vue 指令**:`v-if="text1 == '中文'"`(提取指令表达式中的内联字符串)
390
+ - **JSX/TSX 文本节点**:`<span>中文 {name}</span>`(支持含表达式的混合文本)
391
+ - **JSX/TSX 属性**:`<div title="中文">` `<div title={'中文'}>`
392
+ - **JSON 值**:`"key": "中文值"`
393
+ - **HTML 普通属性**:`<div title="兴趣">`(Vue 模板中)
380
394
 
381
- Comments (`//`, `/* */`, `<!-- -->`) are correctly skipped. `data-*` and `aria-*` attributes are excluded.
395
+ 注释(`//`、`/* */`、`<!-- -->`)会被正确跳过。`data-*` `aria-*` 属性会被排除。
382
396
 
383
- ### Replacement Pattern
397
+ ### 替换模式
384
398
 
385
- | Context | Before | After |
386
- | -------------------------- | ------------------------- | -------------------------------------------------- |
387
- | Vue template text node | `<div>兴趣</div>` | `<div>{{ $t('demo.interest') }}</div>` |
388
- | Vue template interpolation | `{{ '兴趣' }}` | `{{ $t('demo.interest') }}` |
389
- | Vue complex expression | <code>{{ isHidden &#124;&#124; '兴趣' }}</code> | <code>{{ isHidden &#124;&#124; $t('demo.interest') }}</code> |
390
- | Vue directive expression | `v-if="text1 == '文本'"` | `v-if="text1 == $t('demo.text')"` |
391
- | Vue script / JS / TS | `'兴趣'` | `$t('demo.interest')` |
392
- | JSX text node | `<div>兴趣</div>` | `<div>{$t('demo.interest')}</div>` |
393
- | JSX attribute | `<div title="兴趣">` | `<div title={$t('demo.interest')}>` |
394
- | Vue v-bind attribute | `<div :title="'兴趣'">` | `<div :title="$t('demo.interest')">` |
395
- | Template literal | `` `中文 ${name} 测试` `` | `` `$t('demo.chinese') ${name} $t('demo.test')` `` |
396
- | JSON value | `"兴趣"` | `"interest"` (replaced with translation directly) |
399
+ | 上下文 | 替换前 | 替换后 |
400
+ | -------------------- | ------------------------- | -------------------------------------------------- |
401
+ | Vue 模板文本节点 | `<div>兴趣</div>` | `<div>{{ $t('demo.interest') }}</div>` |
402
+ | Vue 模板插值 | `{{ '兴趣' }}` | `{{ $t('demo.interest') }}` |
403
+ | Vue 复杂表达式 | <code>{{ isHidden &#124;&#124; '兴趣' }}</code> | <code>{{ isHidden &#124;&#124; $t('demo.interest') }}</code> |
404
+ | Vue 指令表达式 | `v-if="text1 == '文本'"` | `v-if="text1 == $t('demo.text')"` |
405
+ | Vue script / JS / TS | `'兴趣'` | `$t('demo.interest')` |
406
+ | JSX 文本节点 | `<div>兴趣</div>` | `<div>{$t('demo.interest')}</div>` |
407
+ | JSX 属性 | `<div title="兴趣">` | `<div title={$t('demo.interest')}>` |
408
+ | Vue v-bind 属性 | `<div :title="'兴趣'">` | `<div :title="$t('demo.interest')">` |
409
+ | 模板字面量 | `` `中文 ${name} 测试` `` | `` `$t('demo.chinese') ${name} $t('demo.test')` `` |
410
+ | JSON | `"兴趣"` | `"interest"`(直接替换为翻译结果) |
397
411
 
398
- ### Locale File Format
412
+ ### 语言包文件格式
399
413
 
400
- **When translation succeeds** (English-based keys, truncated to 40 characters):
414
+ **翻译成功时**(基于英文的 key):
401
415
 
402
416
  ```json
403
417
  // zh-CN.json
404
418
  { "demo": { "interest": "兴趣", "test": "测试" } }
405
419
  // en-US.json
406
420
  { "demo": { "interest": "interest", "test": "test" } }
407
- // ja-JP.json (if locales includes ja-JP)
421
+ // ja-JP.json(如果 locales 包含 ja-JP
408
422
  { "demo": { "interest": "興味", "test": "テスト" } }
409
423
  ```
410
424
 
411
- **When translation fails** (auto-generated short keys from Chinese, truncated to 40 characters):
425
+ **翻译失败时**(根据中文自动生成简短 key):
412
426
 
413
427
  ```json
414
- // zh-CN.json and en-US.json
428
+ // zh-CN.json en-US.json
415
429
  { "demo": { "提起妈妈的手_一股酸痛便涌上心头": "提起妈妈的手,一股酸痛便涌上心头...", "test": "测试" } }
416
430
  ```
417
431
 
418
- ### Key Generation Rules
432
+ ### Key 生成规则
419
433
 
420
- | Scenario | Source | Key Generation | Max Length |
421
- | ------------ | ------------------ | --------------------------------------- | ---------- |
422
- | API success | Translated English | snake_case of translation | 40 chars |
423
- | API fallback | Original Chinese | first-line punctuation → `_`, truncated | 40 chars |
434
+ | 场景 | 来源 | Key 生成方式 | 最大长度 |
435
+ | ------------ | -------- | ------------------------ | -------- |
436
+ | API 翻译成功 | 英文译文 | snake_case | 40 字符 |
437
+ | API 回退 | 原始中文 | 去标点、`。` → `_`,截断 | 40 字符 |
424
438
 
425
- Keys are deduplicated: if two texts produce the same truncated key, `_1`, `_2`, etc. are appended.
439
+ Key 会去重:若两段文本生成相同的截断 key,则自动追加 `_1`、`_2`……
426
440
 
427
- ### Translation API Fallback Warning
441
+ ### 翻译 API 回退警告
428
442
 
429
- When the translation API call fails (network error, invalid credentials, or rate limit), the tool logs a warning message to the console, e.g.:
443
+ 当翻译 API 调用失败(网络错误、凭据无效或频率限制)时,工具会在控制台输出警告,例如:
430
444
 
431
445
  ```
432
446
  ⚠ Translation API failed, falling back to Chinese keys for this batch
433
447
  ```
434
448
 
435
- It then falls back to Chinese-based auto-generated keys (see [Key Generation Rules](#key-generation-rules) above). This ensures the i18n migration can proceed even without API connectivity.
449
+ 然后回退到基于中文的自动生成 key(参见上方 [Key 生成规则](#key-生成规则)),确保即使没有 API 连接也能继续进行国际化迁移。
436
450
 
437
- > **Per-text fallback:** If some texts in a batch succeed while others fail, only the failed texts use fallback mode — successful texts still use English keys. This prevents a single failure from degrading the entire batch to Chinese keys.
451
+ > **逐文本回退:** 若批次内部分文本翻译成功、部分失败,仅失败文本走回退模式,成功文本仍使用英文 key。这避免了因单个文本翻译失败导致整批内容降级使用中文 key。
438
452
 
439
- ### Translation Cache
453
+ ### 翻译缓存
440
454
 
441
- Successful API translation results are cached to `.hias/.translation-cache.json`. The next time the same text is encountered (regardless of namespace), it's served from cache, avoiding redundant network requests and API costs.
455
+ 每次成功的 API 翻译结果会缓存到 `.hias/.translation-cache.json`。下次翻译相同文本时(无论哪个命名空间),直接从缓存读取,避免重复的网络请求和 API 费用。
442
456
 
443
- - Cache is keyed by original text with target language as sub-key: `{ "text": { "en": "translated" } }`
444
- - Corrupted or malformed cache files are silently ignored without affecting the normal flow
445
- - Writes are deferred (`setImmediate`) to avoid blocking the translation pipeline
457
+ - 缓存以原文为键、目标语言为二级键:`{ "text": { "en": "translated" } }`
458
+ - 缓存文件破损或格式错误时自动忽略,不影响正常翻译流程
459
+ - 缓存自动在"空闲时"落盘(`setImmediate` 延迟写入),不阻塞翻译管线
446
460
 
447
- ### Unused Translation Cleanup
461
+ ### 未使用翻译清理
448
462
 
449
- After replacing Chinese text with `$t()` calls, the tool checks existing locale JSON files and **removes keys that are no longer referenced anywhere in the source code**. This prevents locale files from accumulating stale entries over time.
463
+ 替换中文为 `$t()` 调用后,工具会检查现有的语言包 JSON 文件,**移除源文件中不再引用的 key**,防止语言包随时间积累过期条目。
450
464
 
451
- Cleanup runs against the same namespace used for the current translation operation, and only affects keys that have zero references across all scanned source files.
465
+ 清理操作仅针对本次翻译操作使用的命名空间,且仅移除在所有扫描源文件中引用数为零的 key。
452
466
 
453
- ### Namespace Normalization
467
+ ### 命名空间规范化
454
468
 
455
- Existing `$t('oldNamespace.key')` calls in source files are automatically updated to the current namespace name during translation.
469
+ 源文件中已有的 `$t('旧命名空间.key')` 调用会在翻译时自动更新为当前命名空间。
456
470
 
457
- ### Dry-Run Mode
471
+ ### 预览模式(--dry-run)
458
472
 
459
- When `--dry-run` is specified, the tool shows a line-level diff of all changes without actually modifying files. After the preview, it prompts `Apply changes? (y/N)` — entering `y` or `yes` executes the changes, anything else cancels.
473
+ 指定 `--dry-run` 时,工具会显示所有变更的行级 diff,但不实际修改文件。预览结束后提示 `Apply changes? (y/N)` — 输入 `y` `yes` 执行变更,其他任何输入取消。
460
474
 
461
- ### `i18nCallTemplate` Customization
475
+ ### `i18nCallTemplate` 自定义
462
476
 
463
- The `i18nCallTemplate` field controls the i18n function name and call format:
477
+ `i18nCallTemplate` 字段控制国际化函数名和调用格式:
464
478
 
465
- | Template Value | Generated Output |
466
- | ---------------------------- | ------------------------------------------- |
467
- | `"$t"` (default) | `$t('demo.interest')` |
468
- | `"$te"` | `$te('demo.interest')` |
469
- | `"{{key}}"` | `demo.interest` (raw key, no function call) |
470
- | `"t('{{key}}')"` | `t('demo.interest')` |
471
- | `"i18n.global.t('{{key}}')"` | `i18n.global.t('demo.interest')` |
479
+ | 模板值 | 生成结果 |
480
+ | ---------------------------- | ------------------------------------- |
481
+ | `"$t"`(默认) | `$t('demo.interest')` |
482
+ | `"$te"` | `$te('demo.interest')` |
483
+ | `"{{key}}"` | `demo.interest`(裸 key,无函数调用) |
484
+ | `"t('{{key}}')"` | `t('demo.interest')` |
485
+ | `"i18n.global.t('{{key}}')"` | `i18n.global.t('demo.interest')` |
472
486
 
473
- When the template **contains** `{{key}}`, it's a template string where `{{key}}` is replaced with `namespace.subKey`. When it does **not** contain `{{key}}`, it wraps as `template('namespace.subKey')`.
487
+ 当模板 **包含** `{{key}}` 时,`{{key}}` 会被替换为 `namespace.subKey`;当 **不包含** `{{key}}` 时,包装为 `template('namespace.subKey')`。
474
488
 
475
- ### Workflow Tips
489
+ ### 工作流建议
476
490
 
477
- - **Always run `--dry-run` first** when trying translation on a new codebase to verify the extraction results
478
- - **Use `tfo` with a single file's parent folder** for targeted translation: `hias tfo src/views/MyComponent myComp`
479
- - **Use `--exclude` to skip config or generated files**: `hias tfo src/views views --exclude "**/*.test.js" --exclude node_modules`
480
- - **Use separate namespaces per module** to keep locale files small and focused. For example, `src/views/user/` → namespace `user`, `src/views/admin/` → namespace `admin`
481
- - **Run rollback after verifying** if `replaceOriginalFile` was on: `hias rollback`
482
- - **Multiple rollbacks**: each `rollback` undoes one snapshot. Run repeatedly to go back further
483
- - **Re-running translation**: existing `$t()` calls are stripped before re-extraction, so running twice is safe and will only add new translations for any remaining Chinese text
491
+ - **首次翻译前务必使用 `--dry-run`** 预览提取结果,确认无误后再执行
492
+ - **针对单文件翻译**:`hias tfo src/views/MyComponent myComp`
493
+ - **排除配置或生成文件**:`hias tfo src/views views --exclude "**/*.test.js" --exclude node_modules`
494
+ - **按模块划分命名空间**:例如 `src/views/user/` → 命名空间 `user`,`src/views/admin/` → 命名空间 `admin`,保持语言包小巧聚焦
495
+ - **启用 `replaceOriginalFile` 后及时回滚确认**:`hias rollback`
496
+ - **多次回滚**:每次 `rollback` 撤销一个快照,可多次执行逐步回退
497
+ - **重复翻译安全**:已有 `$t()` 调用会在重新提取前被剥离,重复运行只会添加剩余中文的翻译
484
498
 
485
499
  ---
486
500
 
487
- ## Global Config
501
+ ## 全局配置
488
502
 
489
- The global config file is located at `~/.hias-cli/config.json` and is shared by both the language setting and translation settings:
503
+ 全局配置文件位于 `~/.hias-cli/config.json`,语言设置和翻译设置共用此文件:
490
504
 
491
505
  ```json
492
506
  {
@@ -506,28 +520,30 @@ The global config file is located at `~/.hias-cli/config.json` and is shared by
506
520
  }
507
521
  ```
508
522
 
509
- Managed by:
523
+ 由以下命令管理:
510
524
 
511
- - **`hias lang <language>`** — sets `language`
512
- - **`hias globalsetting`** — creates/updates `translationSetting`
525
+ - **`hias lang <language>`** — 设置 `language`
526
+ - **`hias global-setting`** — 创建/更新 `translationSetting`
527
+ - **`hias clear-config`** — 删除 `~/.hias-cli`
528
+ - **`hias uninstall -g`** — 先执行 `hias clear-config`,再卸载全局包
513
529
 
514
530
  ---
515
531
 
516
- ## All Commands Reference
517
-
518
- | Command | Alias | Description |
519
- | ----------------------- | ----- | ----------------------------------------- |
520
- | `create <project>` | `crt` | Create project from framework template |
521
- | `adv <name>` | – | Add Vue component |
522
- | `adr <name>` | – | Add React JSX component |
523
- | `adrt <name>` | – | Add React TSX component |
524
- | `adrd <name>` | – | Add Redux JSX store |
525
- | `adrdt <name>` | – | Add Redux TSX store |
526
- | `close-port <ports...>` | – | Kill processes on ports |
527
- | `lang [language]` | – | Set or view CLI language |
528
- | `tf [file] [name]` | – | Translate a single file |
529
- | `tfo [folder] [name]` | – | Translate all files in a folder |
530
- | `setting` | – | Generate project-level translation config |
531
- | `globalsetting` | – | Generate global translation config |
532
- | `gitignore` | – | Add `.hias` to `.gitignore` |
533
- | `rollback` | – | Roll back the last translation operation |
532
+ ## 全部命令一览
533
+
534
+ | 命令 | 别名 | 说明 |
535
+ | ----------------------- | ----- | ---------------------------- |
536
+ | `create <project>` | `crt` | 从框架模板创建项目 |
537
+ | `adv <name>` | – | 添加 Vue 组件 |
538
+ | `adr <name>` | – | 添加 React JSX 组件 |
539
+ | `adrt <name>` | – | 添加 React TSX 组件 |
540
+ | `adrd <name>` | – | 添加 Redux JSX Store |
541
+ | `adrdt <name>` | – | 添加 Redux TSX Store |
542
+ | `close-port <ports...>` | – | 关闭端口的进程 |
543
+ | `lang [language]` | – | 设置或查看 CLI 语言 |
544
+ | `tf [file] [name]` | – | 单个文件翻译 |
545
+ | `tfo [folder] [name]` | – | 递归翻译文件夹 |
546
+ | `setting` | – | 生成项目级翻译配置 |
547
+ | `global-setting` | – | 生成全局翻译配置 |
548
+ | `gitignore` | – | 添加 `.hias` `.gitignore` |
549
+ | `rollback` | – | 回滚上一次翻译操作 |