@syedamirali/i18n-toolkit 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Syed Amir Ali
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,191 @@
1
+ # i18n-toolkit
2
+
3
+ AST-based CLI to extract translation keys from a React / Next.js project, diff them against translation resource files, and translate the missing ones via a local Ollama instance.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ # global
9
+ npm install -g @syedamirali/i18n-toolkit
10
+
11
+ # or one-shot via npx (no install)
12
+ npx @syedamirali/i18n-toolkit extract --src ./src
13
+ ```
14
+
15
+ Requires **Node ≥ 20**. For `translate` you also need a running Ollama instance.
16
+
17
+ ## Commands
18
+
19
+ | Command | Purpose |
20
+ |---|---|
21
+ | `i18n-toolkit extract` | Scan source for translation keys |
22
+ | `i18n-toolkit diff` | Compare extracted keys against a translation file |
23
+ | `i18n-toolkit translate` | Translate missing keys via Ollama |
24
+ | `i18n-toolkit help` | Show top-level help |
25
+
26
+ ---
27
+
28
+ ## `i18n-toolkit extract`
29
+
30
+ ```bash
31
+ i18n-toolkit extract --src /path/to/your/app/src
32
+ ```
33
+
34
+ Output: `output/{timestamp}_translation_extractor.json` — sorted, deterministic.
35
+
36
+ **What it matches** (all AST-based, no regex):
37
+
38
+ | Source | → |
39
+ |---|---|
40
+ | `t("k", { __default__: "v" })` | `{ "k": "v" }` |
41
+ | `t("k") \|\| "fallback"` | `{ "k": "fallback" }` |
42
+ | `t("k")` | `{ "k": "__missing_default__" }` |
43
+ | `<Trans localKey="k">Default</Trans>` | `{ "k": "Default" }` |
44
+ | `<X localKey={\`a.${slug}.b\`}>{name}</X>` | `` { "a.${slug}.b": "__dynamic__" } `` |
45
+
46
+ **Key options:**
47
+
48
+ ```
49
+ --src <path> root to scan (default ".")
50
+ --out <path> explicit output file (overrides --output-dir)
51
+ --output-dir <path> default ./output
52
+ --log-dir <path> default ./logs
53
+ --lang <code> top-level language key (default "en")
54
+ --fail-on-conflict exit 1 if any key has conflicting defaults
55
+ --help full options list
56
+ ```
57
+
58
+ ---
59
+
60
+ ## `i18n-toolkit diff`
61
+
62
+ ```bash
63
+ # Scriptable
64
+ i18n-toolkit diff --src /path/to/app/src --resource ./translations.json
65
+
66
+ # Interactive
67
+ i18n-toolkit diff
68
+ ```
69
+
70
+ `--src` accepts either a directory (runs extraction in-process) or a previously-extracted JSON file.
71
+ `--resource` auto-detects flat `{ key: value }` vs language-wrapped `{ "en": { … } }` and picks the `en` slice.
72
+
73
+ **Output:** `diff-output/{timestamp}/`
74
+
75
+ | File | What's in it |
76
+ |---|---|
77
+ | `missing_in_resource.json` | Keys present in code, missing from resource — **translate these** |
78
+ | `missing_in_extractor.json` | Keys present in resource, no longer in code — orphans |
79
+ | `{timestamp}_translation_diff.log` | Run log with absolute paths |
80
+
81
+ `"__dynamic__"` keys are skipped from comparison. Empty `""` values in either side are treated as missing.
82
+
83
+ ---
84
+
85
+ ## `i18n-toolkit translate`
86
+
87
+ ```bash
88
+ i18n-toolkit translate
89
+ ```
90
+
91
+ Interactive. Three steps:
92
+
93
+ 1. **Input** — pick a recent `missing_in_resource.json`, paste a path, or paste content inline.
94
+ 2. **Target languages** — searchable multi-select from `data/languages.json`. The source (`en`) is shown at the top, pinned and disabled.
95
+ 3. **Model** — pick from your local Ollama's installed models.
96
+
97
+ The Ollama call streams with live thinking + response phases and a sticky status footer (elapsed / tokens / KB).
98
+
99
+ **Output:** `translate-output/{timestamp}/translation.json`
100
+
101
+ ```json
102
+ {
103
+ "en": { "common.back": "Back", "common.cart": "Cart" },
104
+ "fr": { "common.back": "Retour", "common.cart": "Panier" },
105
+ "ar": { "common.back": "رجوع", "common.cart": "عربة" }
106
+ }
107
+ ```
108
+
109
+ **Env vars:**
110
+
111
+ ```
112
+ OLLAMA_URL default http://172.25.80.1:11434
113
+ OLLAMA_TIMEOUT_MS default 1800000 (30 min)
114
+ NO_COLOR disable ANSI colors
115
+ ```
116
+
117
+ ---
118
+
119
+ ## Pipeline
120
+
121
+ The three commands compose:
122
+
123
+ ```
124
+ your-app/src/ ──extract──▶ output/…json
125
+
126
+
127
+ resource.json ──diff──▶ diff-output/…/missing_in_resource.json
128
+
129
+
130
+ translate ──▶ translate-output/…/translation.json
131
+ ```
132
+
133
+ Drop the final `translation.json` back into your app's translation resource and the cycle closes.
134
+
135
+ ---
136
+
137
+ ## Local development
138
+
139
+ Clone the repo, install dependencies, and use the yarn aliases:
140
+
141
+ ```bash
142
+ git clone https://github.com/SyedAmirAli/i18n-toolkit.git
143
+ cd i18n-toolkit
144
+ yarn install
145
+
146
+ yarn extract -- --src /path/to/your/app/src
147
+ yarn diff -- --src /path/to/app/src --resource ./translations.json
148
+ yarn translate
149
+
150
+ yarn build # bundle to dist/ (used by npm publish)
151
+ yarn typecheck # tsc --noEmit
152
+ ```
153
+
154
+ ---
155
+
156
+ ## Project structure
157
+
158
+ ```
159
+ src/
160
+ ├── cli.ts subcommand router (the published bin entry)
161
+ ├── index.ts extract command + runExtraction() programmatic API
162
+ ├── diff.ts diff command
163
+ ├── translate.ts translate command
164
+ └── lib/
165
+ └── ui.ts Spinner, StatusFooter, JsonPretty, multilineInput, color helpers
166
+ prompt/ prompt template used by translate
167
+ data/ language list used by translate
168
+ docs/plans/ design docs (one per command)
169
+ dist/ bundled output (generated; not in git)
170
+ ```
171
+
172
+ Generated output dirs (gitignored): `output/`, `logs/`, `diff-output/`, `translate-output/`.
173
+
174
+ ---
175
+
176
+ ## Design docs
177
+
178
+ See [`docs/plans/`](./docs/plans/) for the full design behind each command.
179
+
180
+ | Plan | Command |
181
+ |---|---|
182
+ | [01-translation-extractor.plan.md](./docs/plans/01-translation-extractor.plan.md) | `extract` |
183
+ | [02-translation-diff.plan.md](./docs/plans/02-translation-diff.plan.md) | `diff` |
184
+ | [03-translate.plan.md](./docs/plans/03-translate.plan.md) | `translate` |
185
+ | [04-translate-quality.plan.md](./docs/plans/04-translate-quality.plan.md) | `translate` quality detection (planned) |
186
+
187
+ ---
188
+
189
+ ## License
190
+
191
+ [MIT](./LICENSE) © Syed Amir Ali