locale-keysmith 0.3.1 → 0.3.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.
Files changed (3) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +131 -70
  3. package/package.json +1 -1
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2026
3
+ Copyright (c) 2026 Faysal Ahmed Akash
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,34 +1,63 @@
1
1
  # locale-keysmith
2
2
 
3
- Sort, audit, clean, and translate i18n locale files from the command line.
3
+ [![npm](https://img.shields.io/npm/v/locale-keysmith)](https://www.npmjs.com/package/locale-keysmith)
4
+ [![Test](https://github.com/faysalakash/locale-keysmith/actions/workflows/test.yml/badge.svg)](https://github.com/faysalakash/locale-keysmith/actions/workflows/test.yml)
5
+ [![License: MIT](https://img.shields.io/npm/l/locale-keysmith)](LICENSE)
6
+
7
+ Keep your i18n locale files complete and tidy, from the command line or in CI.
8
+
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.
12
+
13
+ - **Audit:** one `health` command reports missing, extra, unused, duplicate and empty keys,
14
+ type mismatches 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.
23
+
24
+ ## Works with
25
+
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.
29
+
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.
4
33
 
5
34
  ## Install
6
35
 
7
36
  ```bash
8
- pnpm add -D locale-keysmith
37
+ npm install --save-dev locale-keysmith
38
+ # or: pnpm add -D locale-keysmith
39
+ # or: yarn add -D locale-keysmith
9
40
  ```
10
41
 
42
+ Or run it without installing anything: `npx locale-keysmith health`.
43
+
11
44
  Requires Node.js 22.12 or newer.
12
45
 
13
46
  ## Quick start
14
47
 
15
48
  ```bash
16
- # Write a starter config based on the locale files in your project (optional)
17
- pnpm exec locale-keysmith init
18
-
19
- # See which locale files are detected
20
- pnpm exec locale-keysmith scan
21
-
22
- # Get a full report: missing keys, unused keys, and unsorted files
23
- pnpm exec locale-keysmith health
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
24
52
  ```
25
53
 
26
- By default everything is scanned under the `src` folder. To look elsewhere, add a config
27
- file (see [Configuration](#configuration)).
54
+ By default everything is scanned under the `src` folder. To look elsewhere, run `init` or add
55
+ a config file (see [Configuration](#configuration)).
28
56
 
29
57
  ## Commands
30
58
 
31
- Run a command with `pnpm exec locale-keysmith <command>` (or `npx locale-keysmith <command>`).
59
+ The examples use `npx locale-keysmith <command>`; with pnpm or yarn, `pnpm exec` or `yarn`
60
+ work the same way.
32
61
 
33
62
  | Command | What it does |
34
63
  | -------------------------- | ------------------------------------------------------------------------ |
@@ -41,36 +70,11 @@ Run a command with `pnpm exec locale-keysmith <command>` (or `npx locale-keysmit
41
70
  | `remove-unused` | Delete unused keys from every locale file. |
42
71
  | `translate --to <locales>` | Fill missing keys in target locales via Google Translate. |
43
72
 
44
- ### Using it in CI
45
-
46
- `scan`, `missing`, `unused` and `health` accept `--json` and print a machine-readable
47
- result on stdout (warnings go to stderr, so stdout always parses):
48
-
49
- ```bash
50
- pnpm exec locale-keysmith health --json
51
- ```
52
-
53
- ```json
54
- {
55
- "healthy": false,
56
- "issueCount": 1,
57
- "issues": [{ "type": "missing", "file": "src/locales/de.json", "locale": "de", "key": "status.offline" }]
58
- }
59
- ```
60
-
61
- Exit codes:
62
-
63
- | Code | Meaning |
64
- | ---- | --------------------------------------------------------------------------------- |
65
- | `0` | Nothing to report. |
66
- | `1` | `missing`, `unused` or `health` found something. |
67
- | `2` | The command itself failed (invalid config, unreadable locale file, API error...). |
68
-
69
73
  ### `init`
70
74
 
71
75
  ```bash
72
- pnpm exec locale-keysmith init # refuses to overwrite an existing config
73
- pnpm exec locale-keysmith init --force # overwrite it
76
+ npx locale-keysmith init # refuses to overwrite an existing config
77
+ npx locale-keysmith init --force # overwrite it
74
78
  ```
75
79
 
76
80
  Looks for locale files in the project (skipping `node_modules`, build output, and test or
@@ -84,14 +88,14 @@ fixture folders) and writes a config with:
84
88
  ### `scan`
85
89
 
86
90
  ```bash
87
- pnpm exec locale-keysmith scan
91
+ npx locale-keysmith scan
88
92
  ```
89
93
 
90
- Prints one line per detected locale file, e.g. `en src/locales/en.json`. Matched JSON/YAML
91
- files that are not locale files (see [How locale files are recognised](#how-locale-files-are-recognised))
92
- are listed underneath as skipped, so you can see why a file you expected is missing:
94
+ Prints one line per detected locale file. Matched JSON/YAML files that are not locale files
95
+ (see [How locale files are recognised](#how-locale-files-are-recognised)) are listed
96
+ underneath as skipped, so you can see why a file you expected is missing:
93
97
 
94
- ```
98
+ ```text
95
99
  en src/locales/en.json
96
100
  de src/locales/de.json
97
101
 
@@ -104,12 +108,12 @@ With `--json`, the skipped files are in the `skipped` array.
104
108
  ### `health`
105
109
 
106
110
  ```bash
107
- pnpm exec locale-keysmith health
111
+ npx locale-keysmith health
108
112
  ```
109
113
 
110
114
  A one-shot audit. It prints each problem it finds:
111
115
 
112
- ```
116
+ ```text
113
117
  unsorted src/locales/de.json
114
118
  duplicate en nav.home
115
119
  missing de status.offline
@@ -138,7 +142,7 @@ are skipped by every check.
138
142
  ### `missing`
139
143
 
140
144
  ```bash
141
- pnpm exec locale-keysmith missing
145
+ npx locale-keysmith missing
142
146
  ```
143
147
 
144
148
  Lists, per non-source locale, the keys that exist in the source locale but are absent.
@@ -151,7 +155,7 @@ Japanese only needs `item_other`. Neither is reported as missing or extra for do
151
155
  ### `unused`
152
156
 
153
157
  ```bash
154
- pnpm exec locale-keysmith unused
158
+ npx locale-keysmith unused
155
159
  ```
156
160
 
157
161
  Lists source-locale keys that are never referenced in your source files. See
@@ -160,8 +164,8 @@ Lists source-locale keys that are never referenced in your source files. See
160
164
  ### `sort`
161
165
 
162
166
  ```bash
163
- pnpm exec locale-keysmith sort # preview (no changes written)
164
- pnpm exec locale-keysmith sort --write # write sorted files to disk
167
+ npx locale-keysmith sort # preview (no changes written)
168
+ npx locale-keysmith sort --write # write sorted files to disk
165
169
  ```
166
170
 
167
171
  Sorts keys alphabetically at every nesting level. Without `--write` it only reports which
@@ -170,20 +174,24 @@ files would change.
170
174
  ### `remove-unused`
171
175
 
172
176
  ```bash
173
- pnpm exec locale-keysmith remove-unused # preview
174
- pnpm exec locale-keysmith remove-unused --dry-run # preview (explicit)
175
- pnpm exec locale-keysmith remove-unused --write # delete unused keys and re-sort
177
+ npx locale-keysmith remove-unused # preview
178
+ npx locale-keysmith remove-unused --dry-run # preview (explicit)
179
+ npx locale-keysmith remove-unused --write # delete unused keys and re-sort
176
180
  ```
177
181
 
178
182
  Removes unused keys from **every** locale file (not just the source), then re-sorts them.
179
183
  It lists every key it removes; without `--write` it only lists the keys that would be removed.
180
184
 
185
+ > **Warning:** `--write` deletes translations. Commit your work first and read the list it prints: a key
186
+ > that your code builds entirely at runtime cannot be detected (see
187
+ > [Key usage detection](#key-usage-detection)). Protect such keys with `ignoreKeys`.
188
+
181
189
  ### `translate`
182
190
 
183
191
  ```bash
184
192
  export GOOGLE_TRANSLATE_API_KEY="your-api-key"
185
- pnpm exec locale-keysmith translate --to de,fr # preview counts
186
- pnpm exec locale-keysmith translate --from en --to de,fr --write
193
+ npx locale-keysmith translate --to de,fr # preview counts
194
+ npx locale-keysmith translate --from en --to de,fr --write
187
195
  ```
188
196
 
189
197
  Translates the source locale's missing keys into each target locale using the Google
@@ -207,6 +215,65 @@ never in the URL.
207
215
  before the error is reported, so rerunning only translates what is left.
208
216
  - Keys are sent in batches of up to 100 strings per request.
209
217
 
218
+ #### Getting an API key
219
+
220
+ 1. In the [Google Cloud console](https://console.cloud.google.com/), create or select a
221
+ project. It needs a billing account.
222
+ 2. Enable the **Cloud Translation API** (APIs & Services → Library).
223
+ 3. Create a key under APIs & Services → Credentials → **Create credentials → API key**.
224
+ 4. Restrict the key to the Cloud Translation API, then export it as `GOOGLE_TRANSLATE_API_KEY`.
225
+
226
+ Google charges per translated character, with a monthly free allowance; see
227
+ [Cloud Translation pricing](https://cloud.google.com/translate/pricing). Run without
228
+ `--write` first to see how many keys would be sent.
229
+
230
+ ## Using it in CI
231
+
232
+ `health` fails the job when it finds anything, which makes it a one-line CI check:
233
+
234
+ ```yaml
235
+ # .github/workflows/i18n.yml
236
+ name: i18n
237
+ on: pull_request
238
+
239
+ jobs:
240
+ locales:
241
+ runs-on: ubuntu-latest
242
+ steps:
243
+ - uses: actions/checkout@v7
244
+ - uses: actions/setup-node@v7
245
+ with:
246
+ node-version: 22
247
+ - run: npx --yes locale-keysmith@0 health
248
+ ```
249
+
250
+ If locale-keysmith is a dev dependency, run your install step (`npm ci`) and then
251
+ `npx locale-keysmith health`, so CI uses the version in your lockfile. To check only for
252
+ missing translations, use `missing` instead of `health`.
253
+
254
+ Exit codes:
255
+
256
+ | Code | Meaning |
257
+ | ---- | --------------------------------------------------------------------------------- |
258
+ | `0` | Nothing to report. |
259
+ | `1` | `missing`, `unused` or `health` found something. |
260
+ | `2` | The command itself failed (invalid config, unreadable locale file, API error...). |
261
+
262
+ `scan`, `missing`, `unused` and `health` accept `--json` and print a machine-readable
263
+ result on stdout (warnings go to stderr, so stdout always parses):
264
+
265
+ ```bash
266
+ npx locale-keysmith health --json
267
+ ```
268
+
269
+ ```json
270
+ {
271
+ "healthy": false,
272
+ "issueCount": 1,
273
+ "issues": [{ "type": "missing", "file": "src/locales/de.json", "locale": "de", "key": "status.offline" }]
274
+ }
275
+ ```
276
+
210
277
  ## Configuration
211
278
 
212
279
  Configuration is **optional**. With no config file, locale-keysmith scans **only the `src`
@@ -315,20 +382,14 @@ t(`theme.${mode}`); // every key under theme. (theme.dark, theme.light, ...) is
315
382
  t("errors." + code); // every key under errors. is used
316
383
  ```
317
384
 
318
- ## Development
385
+ A key computed entirely at runtime (`t(keyFromServer)`) leaves no trace in the source, so it
386
+ is reported as unused. Add such keys to `ignoreKeys`.
319
387
 
320
- ```bash
321
- pnpm run lint # ESLint (typescript-eslint, flat config)
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
- ```
388
+ ## Contributing
327
389
 
328
- `pnpm run test` never calls Google Translate. To check `translate` against the real API
329
- (placeholders, HTML entities, batching, error messages), run the live tests with a key. A
330
- full run sends about 2,000 characters:
390
+ Bug reports and pull requests are welcome. See [CONTRIBUTING.md](CONTRIBUTING.md) for the
391
+ development setup.
331
392
 
332
- ```bash
333
- GOOGLE_TRANSLATE_API_KEY="your-api-key" pnpm run test:live
334
- ```
393
+ ## License
394
+
395
+ [MIT](LICENSE) © Faysal Ahmed Akash
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "locale-keysmith",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "CLI helper for sorting, auditing, cleaning, and translating i18n locale keys.",
5
5
  "type": "module",
6
6
  "packageManager": "pnpm@12.16.1",