locale-keysmith 0.3.1 → 0.4.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 +1 -1
- package/README.md +226 -102
- package/dist/checks.d.ts +59 -5
- package/dist/checks.js +191 -37
- package/dist/checks.js.map +1 -1
- package/dist/cli.js +236 -123
- package/dist/cli.js.map +1 -1
- package/dist/config.js +9 -2
- package/dist/config.js.map +1 -1
- package/dist/glob.d.ts +11 -1
- package/dist/glob.js +23 -2
- package/dist/glob.js.map +1 -1
- package/dist/init.js +3 -1
- package/dist/init.js.map +1 -1
- package/dist/keys.d.ts +12 -0
- package/dist/keys.js +48 -19
- package/dist/keys.js.map +1 -1
- package/dist/parser.d.ts +24 -1
- package/dist/parser.js +243 -10
- package/dist/parser.js.map +1 -1
- package/dist/placeholders.d.ts +10 -0
- package/dist/placeholders.js +80 -0
- package/dist/placeholders.js.map +1 -0
- package/dist/plan.d.ts +21 -0
- package/dist/plan.js +32 -0
- package/dist/plan.js.map +1 -0
- package/dist/scanner.js +4 -1
- package/dist/scanner.js.map +1 -1
- package/dist/translate.d.ts +25 -1
- package/dist/translate.js +79 -39
- package/dist/translate.js.map +1 -1
- package/dist/types.d.ts +4 -0
- package/dist/usage.d.ts +46 -5
- package/dist/usage.js +186 -29
- package/dist/usage.js.map +1 -1
- package/package.json +2 -2
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,76 +1,81 @@
|
|
|
1
1
|
# locale-keysmith
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/locale-keysmith)
|
|
4
|
+
[](https://github.com/faysalakash/locale-keysmith/actions/workflows/test.yml)
|
|
5
|
+
[](LICENSE)
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
pnpm add -D locale-keysmith
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
Requires Node.js 22.12 or newer.
|
|
7
|
+
Keep your i18n locale files complete and tidy, from the command line or in CI.
|
|
12
8
|
|
|
13
|
-
|
|
9
|
+
locale-keysmith finds your locale files (JSON or YAML), compares every translation with the
|
|
10
|
+
source language, and checks which keys your code still uses. It can then sort the files,
|
|
11
|
+
delete unused keys, and fill in missing translations with Google Translate.
|
|
14
12
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
- **Audit:** one `health` command reports missing, extra, unused, duplicate and empty keys,
|
|
14
|
+
type mismatches, broken placeholders, missing files and unsorted files.
|
|
15
|
+
- **Clean up:** `sort` and `remove-unused` rewrite files while keeping their indentation, line
|
|
16
|
+
endings and YAML comments.
|
|
17
|
+
- **Translate:** fill missing keys via Google Translate, with `{{placeholders}}`, ICU messages
|
|
18
|
+
and `<0>tags</0>` kept intact.
|
|
19
|
+
- **CI-ready:** `--json` output, and exit codes that tell "issues found" apart from "the check
|
|
20
|
+
itself failed".
|
|
21
|
+
- **Zero config:** works out of the box for a `src` folder; `init` writes a config for
|
|
22
|
+
anything else.
|
|
18
23
|
|
|
19
|
-
|
|
20
|
-
pnpm exec locale-keysmith scan
|
|
24
|
+
## Works with
|
|
21
25
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
26
|
+
locale-keysmith is built around **i18next** conventions, as used by react-i18next,
|
|
27
|
+
next-i18next and i18next-http-backend: plural suffixes (`_one`, `_other`, ...), context,
|
|
28
|
+
namespaces and `keyPrefix` are all understood.
|
|
25
29
|
|
|
26
|
-
|
|
27
|
-
|
|
30
|
+
Other libraries that keep translations in JSON/YAML key files and read them with `t("key")`
|
|
31
|
+
or `$t("key")` (vue-i18n, for example) work for the key checks, but plural forms are checked
|
|
32
|
+
by i18next's rules.
|
|
28
33
|
|
|
29
|
-
##
|
|
34
|
+
## Install
|
|
30
35
|
|
|
31
|
-
|
|
36
|
+
```bash
|
|
37
|
+
npm install --save-dev locale-keysmith
|
|
38
|
+
# or: pnpm add -D locale-keysmith
|
|
39
|
+
# or: yarn add -D locale-keysmith
|
|
40
|
+
```
|
|
32
41
|
|
|
33
|
-
|
|
34
|
-
| -------------------------- | ------------------------------------------------------------------------ |
|
|
35
|
-
| `init` | Write a starter `locale-keysmith.config.json` for this project. |
|
|
36
|
-
| `scan` | List the locale files that were found, with their inferred locale. |
|
|
37
|
-
| `health` | Run every check below plus extra keys, empty values and type mismatches. |
|
|
38
|
-
| `missing` | List keys present in the source locale but missing from the others. |
|
|
39
|
-
| `unused` | List source-locale keys that no source file references. |
|
|
40
|
-
| `sort` | Sort every locale file's keys alphabetically (nested too). |
|
|
41
|
-
| `remove-unused` | Delete unused keys from every locale file. |
|
|
42
|
-
| `translate --to <locales>` | Fill missing keys in target locales via Google Translate. |
|
|
42
|
+
Or run it without installing anything: `npx locale-keysmith health`.
|
|
43
43
|
|
|
44
|
-
|
|
44
|
+
Requires Node.js 22.12 or newer.
|
|
45
45
|
|
|
46
|
-
|
|
47
|
-
result on stdout (warnings go to stderr, so stdout always parses):
|
|
46
|
+
## Quick start
|
|
48
47
|
|
|
49
48
|
```bash
|
|
50
|
-
|
|
49
|
+
npx locale-keysmith init # optional: write a config based on the locale files found
|
|
50
|
+
npx locale-keysmith scan # see which locale files are detected
|
|
51
|
+
npx locale-keysmith health # report everything that needs attention
|
|
51
52
|
```
|
|
52
53
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
"healthy": false,
|
|
56
|
-
"issueCount": 1,
|
|
57
|
-
"issues": [{ "type": "missing", "file": "src/locales/de.json", "locale": "de", "key": "status.offline" }]
|
|
58
|
-
}
|
|
59
|
-
```
|
|
54
|
+
By default everything is scanned under the `src` folder. To look elsewhere, run `init` or add
|
|
55
|
+
a config file (see [Configuration](#configuration)).
|
|
60
56
|
|
|
61
|
-
|
|
57
|
+
## Commands
|
|
62
58
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
|
67
|
-
|
|
|
59
|
+
The examples use `npx locale-keysmith <command>`; with pnpm or yarn, `pnpm exec` or `yarn`
|
|
60
|
+
work the same way.
|
|
61
|
+
|
|
62
|
+
| Command | What it does |
|
|
63
|
+
| --------------- | -------------------------------------------------------------------------------------- |
|
|
64
|
+
| `init` | Write a starter `locale-keysmith.config.json` for this project. |
|
|
65
|
+
| `scan` | List the locale files that were found, with their inferred locale. |
|
|
66
|
+
| `health` | Run every check below plus extra keys, empty values, type mismatches and placeholders. |
|
|
67
|
+
| `missing` | List keys present in the source locale but missing from the others. |
|
|
68
|
+
| `unused` | List source-locale keys that no source file references. |
|
|
69
|
+
| `why <key>` | Show where and how a key is used. |
|
|
70
|
+
| `sort` | Sort every locale file's keys alphabetically (nested too). |
|
|
71
|
+
| `remove-unused` | Delete unused keys from every locale file. |
|
|
72
|
+
| `translate` | Fill missing keys in target locales via Google Translate. |
|
|
68
73
|
|
|
69
74
|
### `init`
|
|
70
75
|
|
|
71
76
|
```bash
|
|
72
|
-
|
|
73
|
-
|
|
77
|
+
npx locale-keysmith init # refuses to overwrite an existing config
|
|
78
|
+
npx locale-keysmith init --force # overwrite it
|
|
74
79
|
```
|
|
75
80
|
|
|
76
81
|
Looks for locale files in the project (skipping `node_modules`, build output, and test or
|
|
@@ -84,14 +89,14 @@ fixture folders) and writes a config with:
|
|
|
84
89
|
### `scan`
|
|
85
90
|
|
|
86
91
|
```bash
|
|
87
|
-
|
|
92
|
+
npx locale-keysmith scan
|
|
88
93
|
```
|
|
89
94
|
|
|
90
|
-
Prints one line per detected locale file
|
|
91
|
-
|
|
92
|
-
|
|
95
|
+
Prints one line per detected locale file. Matched JSON/YAML files that are not locale files
|
|
96
|
+
(see [How locale files are recognised](#how-locale-files-are-recognised)) are listed
|
|
97
|
+
underneath as skipped, so you can see why a file you expected is missing:
|
|
93
98
|
|
|
94
|
-
```
|
|
99
|
+
```text
|
|
95
100
|
en src/locales/en.json
|
|
96
101
|
de src/locales/de.json
|
|
97
102
|
|
|
@@ -104,12 +109,12 @@ With `--json`, the skipped files are in the `skipped` array.
|
|
|
104
109
|
### `health`
|
|
105
110
|
|
|
106
111
|
```bash
|
|
107
|
-
|
|
112
|
+
npx locale-keysmith health
|
|
108
113
|
```
|
|
109
114
|
|
|
110
115
|
A one-shot audit. It prints each problem it finds:
|
|
111
116
|
|
|
112
|
-
```
|
|
117
|
+
```text
|
|
113
118
|
unsorted src/locales/de.json
|
|
114
119
|
duplicate en nav.home
|
|
115
120
|
missing de status.offline
|
|
@@ -121,15 +126,21 @@ unused draft.placeholder
|
|
|
121
126
|
7 issues found.
|
|
122
127
|
```
|
|
123
128
|
|
|
124
|
-
| Issue | Meaning
|
|
125
|
-
| --------------- |
|
|
126
|
-
| `unsorted` | The file's keys are not in alphabetical order (fix with `sort --write`).
|
|
127
|
-
| `duplicate` |
|
|
128
|
-
| `missing`
|
|
129
|
-
| `
|
|
130
|
-
| `
|
|
131
|
-
| `
|
|
132
|
-
| `
|
|
129
|
+
| Issue | Meaning |
|
|
130
|
+
| --------------- | ---------------------------------------------------------------------------------------------------------------------------- |
|
|
131
|
+
| `unsorted` | The file's keys are not in alphabetical order (fix with `sort --write`). |
|
|
132
|
+
| `duplicate` | A key is written twice: `"nav.home"` and `"nav": { "home" }`, or twice in one object. |
|
|
133
|
+
| `missing-file` | A locale listed in `locales` has no file for this set of translations. |
|
|
134
|
+
| `missing` | A key from the source locale is absent in this locale. |
|
|
135
|
+
| `extra` | This locale has a key the source locale does not (often a leftover). |
|
|
136
|
+
| `empty` | The value is an empty or whitespace-only string, or `null`. |
|
|
137
|
+
| `type-mismatch` | The key holds a different kind of value than in the source (text vs. object, text vs. number), however either file nests it. |
|
|
138
|
+
| `placeholder` | The translation lost, misspelled or added a placeholder such as `{{name}}`, `{count}`, `<0>` or `$t(key)`. |
|
|
139
|
+
| `unused` | No source file references the key. |
|
|
140
|
+
|
|
141
|
+
Placeholders are compared regardless of order and spacing. The text inside an ICU message is
|
|
142
|
+
not compared, since it is translated, and `{{count}}` may differ between plural forms ("One
|
|
143
|
+
item" / "{{count}} items").
|
|
133
144
|
|
|
134
145
|
It exits with a non-zero status when any issue is found, so it can gate CI. When everything
|
|
135
146
|
is fine it prints `All locale files are healthy.` and exits `0`. Keys matching `ignoreKeys`
|
|
@@ -138,11 +149,15 @@ are skipped by every check.
|
|
|
138
149
|
### `missing`
|
|
139
150
|
|
|
140
151
|
```bash
|
|
141
|
-
|
|
152
|
+
npx locale-keysmith missing
|
|
142
153
|
```
|
|
143
154
|
|
|
144
155
|
Lists, per non-source locale, the keys that exist in the source locale but are absent.
|
|
145
156
|
|
|
157
|
+
With `locales` in the config, it also lists translation files that do not exist at all, such
|
|
158
|
+
as a `de/checkout.json` that was never created or was deleted. Without it, only files that
|
|
159
|
+
exist are compared.
|
|
160
|
+
|
|
146
161
|
Plural keys follow each language's plural rules, as i18next does: English `item_one` /
|
|
147
162
|
`item_other` means Polish needs `item_one`, `item_few`, `item_many` and `item_other`, while
|
|
148
163
|
Japanese only needs `item_other`. Neither is reported as missing or extra for doing so, and
|
|
@@ -151,17 +166,38 @@ Japanese only needs `item_other`. Neither is reported as missing or extra for do
|
|
|
151
166
|
### `unused`
|
|
152
167
|
|
|
153
168
|
```bash
|
|
154
|
-
|
|
169
|
+
npx locale-keysmith unused
|
|
155
170
|
```
|
|
156
171
|
|
|
157
172
|
Lists source-locale keys that are never referenced in your source files. See
|
|
158
173
|
[Key usage detection](#key-usage-detection) for exactly what counts as "referenced".
|
|
159
174
|
|
|
175
|
+
### `why`
|
|
176
|
+
|
|
177
|
+
```bash
|
|
178
|
+
npx locale-keysmith why cart.item_one
|
|
179
|
+
npx locale-keysmith why account:title # only the account namespace
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
Shows what keeps a key alive, so you can check what `unused` and `remove-unused` will do:
|
|
183
|
+
|
|
184
|
+
```text
|
|
185
|
+
cart.item_one (src/locales/en.json)
|
|
186
|
+
a plural or context form of "cart.item":
|
|
187
|
+
src/components/Cart.tsx:12
|
|
188
|
+
read as part of "cart":
|
|
189
|
+
src/pages/Checkout.tsx:40
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Each reason lists the source lines, or the translations that nest the key with `$t()`. A
|
|
193
|
+
key nothing uses is reported as `not used` and the command exits 1. `--json` prints the same
|
|
194
|
+
as data.
|
|
195
|
+
|
|
160
196
|
### `sort`
|
|
161
197
|
|
|
162
198
|
```bash
|
|
163
|
-
|
|
164
|
-
|
|
199
|
+
npx locale-keysmith sort # preview (no changes written)
|
|
200
|
+
npx locale-keysmith sort --write # write sorted files to disk
|
|
165
201
|
```
|
|
166
202
|
|
|
167
203
|
Sorts keys alphabetically at every nesting level. Without `--write` it only reports which
|
|
@@ -170,29 +206,38 @@ files would change.
|
|
|
170
206
|
### `remove-unused`
|
|
171
207
|
|
|
172
208
|
```bash
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
209
|
+
npx locale-keysmith remove-unused # preview
|
|
210
|
+
npx locale-keysmith remove-unused --dry-run # preview (explicit)
|
|
211
|
+
npx locale-keysmith remove-unused --write # delete unused keys and re-sort
|
|
176
212
|
```
|
|
177
213
|
|
|
178
214
|
Removes unused keys from **every** locale file (not just the source), then re-sorts them.
|
|
179
|
-
|
|
215
|
+
When a whole plural family is unused, the forms only other languages have (Polish
|
|
216
|
+
`item_few`, `item_many`) are removed with it.
|
|
217
|
+
It lists every key it removes and how many keys each file loses; without `--write` it lists
|
|
218
|
+
exactly what `--write` would do, and nothing is written until every change is worked out.
|
|
219
|
+
`--dry-run` and `--write` cannot be combined.
|
|
220
|
+
|
|
221
|
+
> **Warning:** `--write` deletes translations. Commit your work first and read the list it prints: a key
|
|
222
|
+
> that your code builds entirely at runtime cannot be detected (see
|
|
223
|
+
> [Key usage detection](#key-usage-detection)). Protect such keys with `ignoreKeys`.
|
|
180
224
|
|
|
181
225
|
### `translate`
|
|
182
226
|
|
|
183
227
|
```bash
|
|
184
228
|
export GOOGLE_TRANSLATE_API_KEY="your-api-key"
|
|
185
|
-
|
|
186
|
-
|
|
229
|
+
npx locale-keysmith translate --to de,fr # preview counts
|
|
230
|
+
npx locale-keysmith translate --from en --to de,fr --write
|
|
187
231
|
```
|
|
188
232
|
|
|
189
233
|
Translates the source locale's missing keys into each target locale using the Google
|
|
190
234
|
Translate API. A target locale that has no file yet is created next to the source file. Keys
|
|
191
|
-
matching `ignoreKeys` are not translated
|
|
235
|
+
matching `ignoreKeys` are not translated, including plural forms such as `*_few`. Without
|
|
236
|
+
`--to`, it translates into every locale listed in `locales` in the config.
|
|
192
237
|
|
|
193
238
|
| Option | Default | Description |
|
|
194
239
|
| ----------------- | ----------------------- | ----------------------------------------------- |
|
|
195
|
-
| `--to <locales>` |
|
|
240
|
+
| `--to <locales>` | `locales` (config) | Comma-separated target locales. |
|
|
196
241
|
| `--from <locale>` | `sourceLocale` (config) | Source locale to translate from. |
|
|
197
242
|
| `--write` | off | Write results to disk (otherwise preview only). |
|
|
198
243
|
|
|
@@ -205,8 +250,72 @@ never in the URL.
|
|
|
205
250
|
with a warning rather than written broken. (The text inside an ICU message is kept as is.)
|
|
206
251
|
- **Progress is saved.** If a request fails midway, the keys translated so far are written
|
|
207
252
|
before the error is reported, so rerunning only translates what is left.
|
|
253
|
+
- **Existing translations are never replaced.** A key is written with the same layout as in
|
|
254
|
+
the source (a dotted `"cart.title"` key stays dotted). When the target already holds
|
|
255
|
+
something else in its place, such as text where the source has nested keys, the key is
|
|
256
|
+
skipped with a warning. If a target file exists but is not among the scanned locale
|
|
257
|
+
files, or cannot be written, the command stops before sending anything.
|
|
208
258
|
- Keys are sent in batches of up to 100 strings per request.
|
|
209
259
|
|
|
260
|
+
#### Getting an API key
|
|
261
|
+
|
|
262
|
+
1. In the [Google Cloud console](https://console.cloud.google.com/), create or select a
|
|
263
|
+
project. It needs a billing account.
|
|
264
|
+
2. Enable the **Cloud Translation API** (APIs & Services → Library).
|
|
265
|
+
3. Create a key under APIs & Services → Credentials → **Create credentials → API key**.
|
|
266
|
+
4. Restrict the key to the Cloud Translation API, then export it as `GOOGLE_TRANSLATE_API_KEY`.
|
|
267
|
+
|
|
268
|
+
Google charges per translated character, with a monthly free allowance; see
|
|
269
|
+
[Cloud Translation pricing](https://cloud.google.com/translate/pricing). Run without
|
|
270
|
+
`--write` first to see how many keys would be sent.
|
|
271
|
+
|
|
272
|
+
## Using it in CI
|
|
273
|
+
|
|
274
|
+
`health` fails the job when it finds anything, which makes it a one-line CI check:
|
|
275
|
+
|
|
276
|
+
```yaml
|
|
277
|
+
# .github/workflows/i18n.yml
|
|
278
|
+
name: i18n
|
|
279
|
+
on: pull_request
|
|
280
|
+
|
|
281
|
+
jobs:
|
|
282
|
+
locales:
|
|
283
|
+
runs-on: ubuntu-latest
|
|
284
|
+
steps:
|
|
285
|
+
- uses: actions/checkout@v7
|
|
286
|
+
- uses: actions/setup-node@v7
|
|
287
|
+
with:
|
|
288
|
+
node-version: 22
|
|
289
|
+
- run: npx --yes locale-keysmith@0 health
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
If locale-keysmith is a dev dependency, run your install step (`npm ci`) and then
|
|
293
|
+
`npx locale-keysmith health`, so CI uses the version in your lockfile. To check only for
|
|
294
|
+
missing translations, use `missing` instead of `health`.
|
|
295
|
+
|
|
296
|
+
Exit codes:
|
|
297
|
+
|
|
298
|
+
| Code | Meaning |
|
|
299
|
+
| ---- | --------------------------------------------------------------------------------- |
|
|
300
|
+
| `0` | Nothing to report. |
|
|
301
|
+
| `1` | `missing`, `unused` or `health` found something, or `why` found no use. |
|
|
302
|
+
| `2` | The command itself failed (invalid option or config, unreadable file, API error). |
|
|
303
|
+
|
|
304
|
+
`scan`, `missing`, `unused` and `health` accept `--json` and print a machine-readable
|
|
305
|
+
result on stdout (warnings go to stderr, so stdout always parses):
|
|
306
|
+
|
|
307
|
+
```bash
|
|
308
|
+
npx locale-keysmith health --json
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
```json
|
|
312
|
+
{
|
|
313
|
+
"healthy": false,
|
|
314
|
+
"issueCount": 1,
|
|
315
|
+
"issues": [{ "type": "missing", "file": "src/locales/de.json", "locale": "de", "key": "status.offline" }]
|
|
316
|
+
}
|
|
317
|
+
```
|
|
318
|
+
|
|
210
319
|
## Configuration
|
|
211
320
|
|
|
212
321
|
Configuration is **optional**. With no config file, locale-keysmith scans **only the `src`
|
|
@@ -234,25 +343,32 @@ set either explicitly, which overrides the derived globs:
|
|
|
234
343
|
"roots": ["src"],
|
|
235
344
|
"translationFiles": ["src/locales/*.{json,yml,yaml}", "public/locales/**/*.{json,yml,yaml}"],
|
|
236
345
|
"src": ["src/**/*.{ts,tsx,js,jsx,vue}"],
|
|
237
|
-
"ignoreKeys": ["debug.*"]
|
|
346
|
+
"ignoreKeys": ["debug.*"],
|
|
347
|
+
"locales": ["en", "de", "fr"]
|
|
238
348
|
}
|
|
239
349
|
```
|
|
240
350
|
|
|
241
351
|
### Fields and defaults
|
|
242
352
|
|
|
243
|
-
| Field | Default | Purpose
|
|
244
|
-
| ------------------ | --------------------------------------------------------- |
|
|
245
|
-
| `sourceLocale` | `"en"` | Locale treated as the source of truth.
|
|
246
|
-
| `roots` | `["src"]` | Folders to scan for locale files and source usage.
|
|
247
|
-
| `translationFiles` | `<root>/**/*.{json,yml,yaml}` per root | Globs for locale files (overrides the derived globs).
|
|
248
|
-
| `src` | `<root>/**/*.{ts,tsx,js,jsx,mjs,cjs,vue,svelte}` per root | Globs for source files (overrides the derived globs).
|
|
249
|
-
| `ignoreKeys` | `[]` | Key patterns excluded from audits (`*` wildcard).
|
|
353
|
+
| Field | Default | Purpose |
|
|
354
|
+
| ------------------ | --------------------------------------------------------- | ------------------------------------------------------- |
|
|
355
|
+
| `sourceLocale` | `"en"` | Locale treated as the source of truth. |
|
|
356
|
+
| `roots` | `["src"]` | Folders to scan for locale files and source usage. |
|
|
357
|
+
| `translationFiles` | `<root>/**/*.{json,yml,yaml}` per root | Globs for locale files (overrides the derived globs). |
|
|
358
|
+
| `src` | `<root>/**/*.{ts,tsx,js,jsx,mjs,cjs,vue,svelte}` per root | Globs for source files (overrides the derived globs). |
|
|
359
|
+
| `ignoreKeys` | `[]` | Key patterns excluded from audits (`*` wildcard). |
|
|
360
|
+
| `locales` | `[]` | Locales every set of translations must have a file for. |
|
|
250
361
|
|
|
251
362
|
The config is checked before any command runs. Wrong types (`"roots": "src"` instead of
|
|
252
363
|
`["src"]`), a `sourceLocale` that is not a locale code, and unknown fields (with a "did you
|
|
253
364
|
mean" hint for typos) are all reported together.
|
|
254
365
|
|
|
255
|
-
`node_modules`, `dist`, and `.next` are always ignored.
|
|
366
|
+
`node_modules`, `dist`, and `.next` are always ignored. A linked locale file is used only
|
|
367
|
+
when it points inside the project, linked folders are not entered, and no command writes
|
|
368
|
+
outside the project directory.
|
|
369
|
+
|
|
370
|
+
Writes replace a file in one step and keep its permissions. A file that changed on disk
|
|
371
|
+
since the command read it is left alone and the command fails, so rerun it.
|
|
256
372
|
|
|
257
373
|
### How locale files are recognised
|
|
258
374
|
|
|
@@ -282,6 +398,8 @@ referenced indirectly. All of these are detected:
|
|
|
282
398
|
t("app.title"); // direct call (also i18n.t / $t / translate)
|
|
283
399
|
<Trans i18nKey="banner.welcome" />; // JSX i18nKey prop
|
|
284
400
|
t(online ? "status.online" : "status.offline"); // ternary branches
|
|
401
|
+
t(["error.specific", "error.generic"]); // fallback keys
|
|
402
|
+
t(($) => $.menu.title); // selector function
|
|
285
403
|
|
|
286
404
|
const priorityKey = { low: "priority.low", high: "priority.high" };
|
|
287
405
|
t(priorityKey[level]); // value from a lookup object
|
|
@@ -297,12 +415,21 @@ runtime:
|
|
|
297
415
|
```ts
|
|
298
416
|
t("cart.item", { count }); // cart.item_one, cart.item_other, cart.item_zero, ... are used
|
|
299
417
|
t("friend.greeting", { context }); // friend.greeting_male, friend.greeting_female, ... are used
|
|
300
|
-
t("common:common.title"); // namespaced: common.title is used
|
|
418
|
+
t("common:common.title"); // namespaced: common.title in the common namespace is used
|
|
419
|
+
t("menu", { returnObjects: true }); // every key under menu is used
|
|
420
|
+
t("steps.0"); // the steps array is used
|
|
301
421
|
|
|
302
422
|
const { t } = useTranslation("translation", { keyPrefix: "settings" });
|
|
303
423
|
t("title"); // keyPrefix: settings.title is used (also getFixedT(lng, ns, "settings"))
|
|
304
424
|
```
|
|
305
425
|
|
|
426
|
+
Translations can use other keys through i18next nesting, as in `"title": "$t(brand) app"`.
|
|
427
|
+
A key referenced that way from any locale file is used too.
|
|
428
|
+
|
|
429
|
+
A namespaced reference only counts for its own namespace: with `public/locales/en/account.json`
|
|
430
|
+
and `public/locales/en/home.json`, `t("account:title")` keeps `title` in `account.json` but
|
|
431
|
+
not in `home.json`. A reference without a namespace counts for every namespace.
|
|
432
|
+
|
|
306
433
|
In a file that sets a `keyPrefix`, every key is counted both with and without the prefix,
|
|
307
434
|
since another `t` in the same file may not use it. A `keyPrefix` built at runtime cannot be
|
|
308
435
|
resolved, so keep its keys out of `remove-unused` with `ignoreKeys`.
|
|
@@ -315,20 +442,17 @@ t(`theme.${mode}`); // every key under theme. (theme.dark, theme.light, ...) is
|
|
|
315
442
|
t("errors." + code); // every key under errors. is used
|
|
316
443
|
```
|
|
317
444
|
|
|
318
|
-
|
|
445
|
+
A key computed entirely at runtime (`t(keyFromServer)`) leaves no trace in the source, so it
|
|
446
|
+
is reported as unused. Add such keys to `ignoreKeys`.
|
|
319
447
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
pnpm run format # Prettier --write
|
|
323
|
-
pnpm run test # Vitest
|
|
324
|
-
pnpm run coverage # Vitest with v8 coverage
|
|
325
|
-
pnpm run check # lint + format:check + build + test
|
|
326
|
-
```
|
|
448
|
+
If the `src` globs match no file at all, every key would look unused. `unused` and
|
|
449
|
+
`health` then print a warning, and `remove-unused` stops without removing anything.
|
|
327
450
|
|
|
328
|
-
|
|
329
|
-
(placeholders, HTML entities, batching, error messages), run the live tests with a key. A
|
|
330
|
-
full run sends about 2,000 characters:
|
|
451
|
+
## Contributing
|
|
331
452
|
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
453
|
+
Bug reports and pull requests are welcome. See [CONTRIBUTING.md](CONTRIBUTING.md) for the
|
|
454
|
+
development setup.
|
|
455
|
+
|
|
456
|
+
## License
|
|
457
|
+
|
|
458
|
+
[MIT](LICENSE) © Faysal Ahmed Akash
|
package/dist/checks.d.ts
CHANGED
|
@@ -1,28 +1,82 @@
|
|
|
1
|
-
import type { LocaleFile, LocaleObject } from "./types.js";
|
|
1
|
+
import type { LocaleFile, LocaleLintConfig, LocaleObject } from "./types.js";
|
|
2
|
+
import type { KeyUsage } from "./usage.js";
|
|
2
3
|
export interface TypeMismatch {
|
|
3
4
|
key: string;
|
|
4
5
|
/** Value kind in the source file: string, number, boolean, null, array or object. */
|
|
5
6
|
source: string;
|
|
6
7
|
target: string;
|
|
7
8
|
}
|
|
9
|
+
export type HealthIssueType = "unsorted" | "duplicate" | "missing-file" | "missing" | "extra" | "empty" | "type-mismatch" | "placeholder" | "unused";
|
|
10
|
+
export interface HealthIssue {
|
|
11
|
+
type: HealthIssueType;
|
|
12
|
+
file: LocaleFile;
|
|
13
|
+
key?: string;
|
|
14
|
+
/** For type mismatches: what the key holds in the source and in this file. */
|
|
15
|
+
kinds?: {
|
|
16
|
+
source: string;
|
|
17
|
+
target: string;
|
|
18
|
+
};
|
|
19
|
+
/** For placeholder issues: placeholders of the source text this file lacks, and extra ones. */
|
|
20
|
+
placeholders?: {
|
|
21
|
+
missing: string[];
|
|
22
|
+
unexpected: string[];
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export interface PlaceholderMismatch {
|
|
26
|
+
key: string;
|
|
27
|
+
missing: string[];
|
|
28
|
+
unexpected: string[];
|
|
29
|
+
}
|
|
30
|
+
/** Every issue `health` reports for groups of a source file and its translations. */
|
|
31
|
+
export declare function healthIssues(groups: Array<{
|
|
32
|
+
source: LocaleFile;
|
|
33
|
+
targets: LocaleFile[];
|
|
34
|
+
}>, { ignoreKeys, locales }: Pick<LocaleLintConfig, "ignoreKeys" | "locales">, usage: KeyUsage): HealthIssue[];
|
|
35
|
+
/**
|
|
36
|
+
* The files a group lacks for the required `locales`, as empty locale files at the path each
|
|
37
|
+
* would have (`locales/de/checkout.json` next to `locales/en/checkout.json`).
|
|
38
|
+
*/
|
|
39
|
+
export declare function absentLocaleFiles({ source, targets }: {
|
|
40
|
+
source: LocaleFile;
|
|
41
|
+
targets: LocaleFile[];
|
|
42
|
+
}, locales: string[]): LocaleFile[];
|
|
43
|
+
/** Source keys no code or translation reads, judged within the file's own namespace. */
|
|
44
|
+
export declare function unusedKeys(source: LocaleFile, ignoreKeys: string[], usage: KeyUsage): string[];
|
|
8
45
|
/**
|
|
9
46
|
* The keys a target locale should contain: the source keys, with plural forms adjusted to
|
|
10
47
|
* the target language. English `item_one`/`item_other` becomes `item_one`/`item_few`/
|
|
11
48
|
* `item_many`/`item_other` for Polish and just `item_other` for Japanese.
|
|
12
49
|
*/
|
|
13
50
|
export declare function expectedKeys(sourceKeys: string[], targetLocale: string): string[];
|
|
14
|
-
|
|
51
|
+
/**
|
|
52
|
+
* `keys` plus every plural form of the complete plural families among them, so removing
|
|
53
|
+
* English `item_one`/`item_other` also removes Polish `item_few` and `item_many`.
|
|
54
|
+
*/
|
|
55
|
+
export declare function withPluralForms(keys: string[]): string[];
|
|
56
|
+
/** Expected keys the target lacks. Ignore patterns apply to the target's own plural forms too. */
|
|
57
|
+
export declare function missingKeys(sourceKeys: string[], target: LocaleFile, ignoreKeys?: string[]): string[];
|
|
15
58
|
/** Keys in the target that the source does not have (allowing for the target's plural forms). */
|
|
16
59
|
export declare function extraKeys(sourceKeys: string[], target: LocaleFile): string[];
|
|
17
60
|
/**
|
|
18
61
|
* Keys written more than once in a file. `{ "a.b": "x", "a": { "b": "y" } }` defines `a.b`
|
|
19
|
-
* twice, and i18next only ever reads one of the two values.
|
|
62
|
+
* twice, and i18next only ever reads one of the two values. A key repeated in one JSON object
|
|
63
|
+
* never reaches `data` (the parser keeps the last), so it comes from `duplicateMembers`.
|
|
20
64
|
*/
|
|
21
65
|
export declare function duplicateKeys(file: LocaleFile): string[];
|
|
22
66
|
/** Keys whose value is an empty or whitespace-only string, or null. */
|
|
23
67
|
export declare function emptyValueKeys(file: LocaleFile): string[];
|
|
24
|
-
/**
|
|
25
|
-
|
|
68
|
+
/**
|
|
69
|
+
* Keys that hold a different kind of value in the target than in the source. Keys are compared
|
|
70
|
+
* as i18next reads them, so `{ "a.b": "x" }` and `{ "a": { "b": 1 } }` are the same key.
|
|
71
|
+
*/
|
|
72
|
+
export declare function typeMismatches(source: LocaleObject, target: LocaleObject): TypeMismatch[];
|
|
73
|
+
/**
|
|
74
|
+
* Translations whose placeholders differ from the source text's: `{{name}}` dropped or
|
|
75
|
+
* misspelled, a `<0>` tag lost, a `$t()` added. A plural form is compared with the same form
|
|
76
|
+
* in the source, or with `_other` when the source language lacks it; `{{count}}` may come and
|
|
77
|
+
* go between plural forms ("One item" / "{{count}} items").
|
|
78
|
+
*/
|
|
79
|
+
export declare function placeholderMismatches(source: LocaleObject, target: LocaleObject): PlaceholderMismatch[];
|
|
26
80
|
/**
|
|
27
81
|
* For a plural form the source language does not have (Polish `_few` from English), the
|
|
28
82
|
* source text to translate from: the `_other` form, which i18next treats as the general one.
|