ai-localize-codemods 2.0.5 → 2.0.6
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/CHANGELOG.md +7 -0
- package/README.md +78 -0
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
package/README.md
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# ai-localize-codemods
|
|
2
|
+
|
|
3
|
+
> Automatic i18n codemod injection for React, Vue, and Angular — part of the [ai-localize-core](https://github.com/ai-localize/ai-localize-core) platform.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/ai-localize-codemods)
|
|
6
|
+
[](LICENSE)
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## What it does
|
|
11
|
+
|
|
12
|
+
Transforms source files using Babel AST + Recast to:
|
|
13
|
+
|
|
14
|
+
1. Add the appropriate import (`useTranslation`, `useI18n`, `TranslateService`, …)
|
|
15
|
+
2. Initialise the hook in each component
|
|
16
|
+
3. Wrap every hardcoded string with the translation call: `t('key')`
|
|
17
|
+
4. Preserve original code formatting (Recast format-preserving transforms)
|
|
18
|
+
|
|
19
|
+
Supports **CDN URL replacement** in source files using an asset manifest.
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm install ai-localize-codemods
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { CodemodRunner } from 'ai-localize-codemods';
|
|
31
|
+
|
|
32
|
+
const runner = new CodemodRunner(config, process.cwd());
|
|
33
|
+
const result = await runner.run(detectedTexts, { dryRun: false });
|
|
34
|
+
// result.totalReplacements — number of string replacements
|
|
35
|
+
// result.changedFiles — number of files modified
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Codemod configuration
|
|
39
|
+
|
|
40
|
+
Control what code is injected via the `codemods` block in `ai-localize.config.json`:
|
|
41
|
+
|
|
42
|
+
```json
|
|
43
|
+
{
|
|
44
|
+
"codemods": {
|
|
45
|
+
"importPackage": "react-i18next",
|
|
46
|
+
"hookName": "useTranslation",
|
|
47
|
+
"translationFunction": "t",
|
|
48
|
+
"namespace": "common",
|
|
49
|
+
"accessorStyle": "function"
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
| Field | Default | Description |
|
|
55
|
+
|---|---|---|
|
|
56
|
+
| `importPackage` | `"react-i18next"` | npm package **or** project-relative path (e.g. `"src/hooks/useTranslation"`) |
|
|
57
|
+
| `hookName` | `"useTranslation"` | Hook/function to import |
|
|
58
|
+
| `translationFunction` | `"t"` | Accessor variable name |
|
|
59
|
+
| `namespace` | — | Namespace arg: `useTranslation("common")` |
|
|
60
|
+
| `accessorStyle` | `"function"` | `"function"` → `t('key')` · `"bracket"` → `t['key']` |
|
|
61
|
+
|
|
62
|
+
### Local hook path resolution
|
|
63
|
+
|
|
64
|
+
When `importPackage` is a project-relative path, the codemod computes the correct
|
|
65
|
+
relative import for every transformed file:
|
|
66
|
+
|
|
67
|
+
```
|
|
68
|
+
src/components/Button.tsx → import { useTranslation } from '../../hooks/useTranslation'
|
|
69
|
+
src/pages/Home.tsx → import { useTranslation } from '../hooks/useTranslation'
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## Part of ai-localize-core
|
|
75
|
+
|
|
76
|
+
Install the CLI for the complete toolset: `npm install -g ai-localize-cli`
|
|
77
|
+
|
|
78
|
+
MIT © ai-localize-core contributors
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai-localize-codemods",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.6",
|
|
4
4
|
"description": "AST-based codemods to inject i18n wrappers into frontend source code",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"@babel/generator": "^7.23.9",
|
|
42
42
|
"recast": "^0.23.4",
|
|
43
43
|
"jscodeshift": "^0.15.2",
|
|
44
|
-
"ai-localize-shared": "2.0.
|
|
44
|
+
"ai-localize-shared": "2.0.6"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@types/babel__generator": "^7.6.8",
|