ai-localize-validators 2.0.5 → 2.0.7

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 CHANGED
@@ -1,5 +1,31 @@
1
1
  # ai-localize-validators
2
2
 
3
+ ## 2.0.7
4
+
5
+ ### Minor Changes
6
+
7
+ - **Two-pass `MissingKeyValidator`** — now validates both the default language file (checks
8
+ all keys exist and have non-empty values) and each target language file (checks keys exist,
9
+ are non-empty, and don't equal the source placeholder). Catches missing translations in the
10
+ source language itself.
11
+ - **`UnusedKeyValidator` guards missing `sourceDir`** — no longer throws when `sourceDir`
12
+ does not exist; returns an empty unused-keys list instead.
13
+ - **Backtick + last-segment key matching** — translation call detection now handles
14
+ template-literal keys and matches by last dot-separated segment for namespaced keys.
15
+
16
+ ### Patch Changes
17
+
18
+ - Added `repository`, `homepage`, `bugs`, `author` fields to `package.json` for npm registry display.
19
+ - Added `sideEffects: false` to enable tree-shaking in bundlers.
20
+ - Added `prepublishOnly` script to ensure the package is built before publishing.
21
+
22
+ ## 2.0.6
23
+
24
+ ### Patch Changes
25
+
26
+ - Add per-package README.md files so each package displays documentation on npmjs.com
27
+ - Update README version badge to 2.0.6
28
+
3
29
  ## 2.0.5
4
30
 
5
31
  ### Patch Changes
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-2026 ai-localize-core contributors
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,83 @@
1
+ # ai-localize-validators
2
+
3
+ > Locale file validation — missing keys, duplicate values, placeholder mismatches, and unused keys — for the [ai-localize-core](https://github.com/ai-localize/ai-localize-core) platform.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/ai-localize-validators.svg)](https://www.npmjs.com/package/ai-localize-validators)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
7
+
8
+ ---
9
+
10
+ ## What it does
11
+
12
+ | Validator | Type | Description |
13
+ |---|---|---|
14
+ | `MissingKeyValidator` | Error | Finds keys absent in target languages **or** where the translated value still equals the English source placeholder |
15
+ | `DuplicateKeyValidator` | Error | Finds keys sharing the same translated value within a namespace |
16
+ | `PlaceholderValidator` | Error | Detects `{{var}}` / `{var}` / `%(var)s` / `%var` mismatches between source and target |
17
+ | `UnusedKeyValidator` | Warning | Finds locale keys not referenced anywhere in source code |
18
+
19
+ ## Installation
20
+
21
+ ```bash
22
+ npm install ai-localize-validators
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ```ts
28
+ import { LocaleValidator } from 'ai-localize-validators';
29
+
30
+ const validator = new LocaleValidator({
31
+ localesDir: './locales',
32
+ sourceDir: './src',
33
+ defaultLanguage: 'en',
34
+ targetLanguages: ['fr', 'de'],
35
+ checkUnused: true,
36
+ checkDuplicates: true,
37
+ checkPlaceholders: true,
38
+ });
39
+
40
+ const result = validator.validate();
41
+ // result.valid — boolean
42
+ // result.errors — ValidationError[]
43
+ // result.warnings — ValidationWarning[]
44
+ ```
45
+
46
+ ## Missing key detection (v2.0.5+)
47
+
48
+ A translation key is flagged as **missing** when ANY of these is true:
49
+
50
+ 1. The key is absent from the target language file
51
+ 2. The target value is an empty string `""`
52
+ 3. **The target value equals the source (English) value** — indicating it was auto-seeded by the extractor and not yet translated
53
+
54
+ This third condition correctly catches keys seeded by `ai-localize extract` which copies the English value into target files as a placeholder. Error message:
55
+
56
+ ```
57
+ [missing-key] Missing translation for "common.save" in "fr" (value equals source)
58
+ ```
59
+
60
+ > **Intentionally identical values:** If a key is supposed to be the same across all languages (brand names, units, etc.), use `staticKeys` in your config instead of hardcoding the value. Static keys bypass the validator entirely.
61
+
62
+ ## Individual validators
63
+
64
+ ```ts
65
+ import {
66
+ MissingKeyValidator,
67
+ DuplicateKeyValidator,
68
+ PlaceholderValidator,
69
+ UnusedKeyValidator,
70
+ } from 'ai-localize-validators';
71
+
72
+ const { errors, missingByLanguage } = new MissingKeyValidator(
73
+ './locales', 'en', ['fr', 'de']
74
+ ).validate();
75
+ ```
76
+
77
+ ---
78
+
79
+ ## Part of ai-localize-core
80
+
81
+ Install the CLI for the complete toolset: `npm install -g ai-localize-cli`
82
+
83
+ MIT © ai-localize-core contributors
package/dist/index.js CHANGED
@@ -57,6 +57,28 @@ var MissingKeyValidator = class {
57
57
  const defaultDir = path.join(this.localesDir, this.defaultLanguage);
58
58
  if (!fs.existsSync(defaultDir)) return { errors, missingByLanguage };
59
59
  const nsFiles = fs.readdirSync(defaultDir).filter((f) => f.endsWith(".json"));
60
+ for (const nsFile of nsFiles) {
61
+ const defaultFilePath = path.join(defaultDir, nsFile);
62
+ const defaultEntries = (0, import_ai_localize_shared.readJsonSafe)(defaultFilePath);
63
+ if (!defaultEntries) continue;
64
+ const namespace = nsFile.replace(".json", "");
65
+ for (const [key, value] of Object.entries(defaultEntries)) {
66
+ const isEmpty = value === null || value === void 0 || typeof value === "string" && value.trim() === "";
67
+ if (isEmpty) {
68
+ const fullKey = `${namespace}.${key}`;
69
+ errors.push({
70
+ type: "missing-key",
71
+ key: fullKey,
72
+ language: this.defaultLanguage,
73
+ message: `Default locale "${this.defaultLanguage}" has empty value for key "${fullKey}"`,
74
+ filePath: defaultFilePath
75
+ });
76
+ if (!missingByLanguage[this.defaultLanguage])
77
+ missingByLanguage[this.defaultLanguage] = [];
78
+ missingByLanguage[this.defaultLanguage].push(fullKey);
79
+ }
80
+ }
81
+ }
60
82
  const langs = this.targetLanguages.length > 0 ? this.targetLanguages : fs.readdirSync(this.localesDir, { withFileTypes: true }).filter((e) => e.isDirectory() && e.name !== this.defaultLanguage).map((e) => e.name);
61
83
  for (const nsFile of nsFiles) {
62
84
  const defaultEntries = (0, import_ai_localize_shared.readJsonSafe)(path.join(defaultDir, nsFile));
@@ -75,7 +97,7 @@ var MissingKeyValidator = class {
75
97
  type: "missing-key",
76
98
  key: fullKey,
77
99
  language: lang,
78
- message: `Missing translation for "${fullKey}" in "${lang}" (value equals source)`,
100
+ message: `Missing translation for "${fullKey}" in "${lang}"`,
79
101
  filePath: targetPath
80
102
  });
81
103
  if (!missingByLanguage[lang]) missingByLanguage[lang] = [];
@@ -194,11 +216,18 @@ var UnusedKeyValidator = class {
194
216
  validate() {
195
217
  const warnings = [];
196
218
  const unusedKeys = [];
219
+ if (!this.sourceDir || !fs4.existsSync(this.sourceDir)) {
220
+ return { warnings, unusedKeys };
221
+ }
197
222
  const allKeys = this.collectLocaleKeys();
223
+ if (allKeys.length === 0) return { warnings, unusedKeys };
198
224
  const sourceContent = this.collectSourceContent();
199
225
  for (const key of allKeys) {
200
- const shortKey = key.includes(".") ? key.split(".").slice(1).join(".") : key;
201
- const referenced = sourceContent.includes(`'${key}'`) || sourceContent.includes(`"${key}"`) || sourceContent.includes(`'${shortKey}'`) || sourceContent.includes(`"${shortKey}"`);
226
+ const parts = key.split(".");
227
+ const shortKey = parts.length > 1 ? parts.slice(1).join(".") : key;
228
+ const lastSegment = parts[parts.length - 1];
229
+ const referenced = sourceContent.includes(`'${key}'`) || sourceContent.includes(`"${key}"`) || sourceContent.includes("`" + key + "`") || sourceContent.includes(`'${shortKey}'`) || sourceContent.includes(`"${shortKey}"`) || sourceContent.includes("`" + shortKey + "`") || // Also match the last dot-segment alone (e.g. t('submitButton'))
230
+ lastSegment !== key && (sourceContent.includes(`'${lastSegment}'`) || sourceContent.includes(`"${lastSegment}"`) || sourceContent.includes("`" + lastSegment + "`"));
202
231
  if (!referenced) {
203
232
  unusedKeys.push(key);
204
233
  warnings.push({ type: "unused-key", key, message: `Key "${key}" not referenced in source` });
package/dist/index.mjs CHANGED
@@ -17,6 +17,28 @@ var MissingKeyValidator = class {
17
17
  const defaultDir = path.join(this.localesDir, this.defaultLanguage);
18
18
  if (!fs.existsSync(defaultDir)) return { errors, missingByLanguage };
19
19
  const nsFiles = fs.readdirSync(defaultDir).filter((f) => f.endsWith(".json"));
20
+ for (const nsFile of nsFiles) {
21
+ const defaultFilePath = path.join(defaultDir, nsFile);
22
+ const defaultEntries = readJsonSafe(defaultFilePath);
23
+ if (!defaultEntries) continue;
24
+ const namespace = nsFile.replace(".json", "");
25
+ for (const [key, value] of Object.entries(defaultEntries)) {
26
+ const isEmpty = value === null || value === void 0 || typeof value === "string" && value.trim() === "";
27
+ if (isEmpty) {
28
+ const fullKey = `${namespace}.${key}`;
29
+ errors.push({
30
+ type: "missing-key",
31
+ key: fullKey,
32
+ language: this.defaultLanguage,
33
+ message: `Default locale "${this.defaultLanguage}" has empty value for key "${fullKey}"`,
34
+ filePath: defaultFilePath
35
+ });
36
+ if (!missingByLanguage[this.defaultLanguage])
37
+ missingByLanguage[this.defaultLanguage] = [];
38
+ missingByLanguage[this.defaultLanguage].push(fullKey);
39
+ }
40
+ }
41
+ }
20
42
  const langs = this.targetLanguages.length > 0 ? this.targetLanguages : fs.readdirSync(this.localesDir, { withFileTypes: true }).filter((e) => e.isDirectory() && e.name !== this.defaultLanguage).map((e) => e.name);
21
43
  for (const nsFile of nsFiles) {
22
44
  const defaultEntries = readJsonSafe(path.join(defaultDir, nsFile));
@@ -35,7 +57,7 @@ var MissingKeyValidator = class {
35
57
  type: "missing-key",
36
58
  key: fullKey,
37
59
  language: lang,
38
- message: `Missing translation for "${fullKey}" in "${lang}" (value equals source)`,
60
+ message: `Missing translation for "${fullKey}" in "${lang}"`,
39
61
  filePath: targetPath
40
62
  });
41
63
  if (!missingByLanguage[lang]) missingByLanguage[lang] = [];
@@ -154,11 +176,18 @@ var UnusedKeyValidator = class {
154
176
  validate() {
155
177
  const warnings = [];
156
178
  const unusedKeys = [];
179
+ if (!this.sourceDir || !fs4.existsSync(this.sourceDir)) {
180
+ return { warnings, unusedKeys };
181
+ }
157
182
  const allKeys = this.collectLocaleKeys();
183
+ if (allKeys.length === 0) return { warnings, unusedKeys };
158
184
  const sourceContent = this.collectSourceContent();
159
185
  for (const key of allKeys) {
160
- const shortKey = key.includes(".") ? key.split(".").slice(1).join(".") : key;
161
- const referenced = sourceContent.includes(`'${key}'`) || sourceContent.includes(`"${key}"`) || sourceContent.includes(`'${shortKey}'`) || sourceContent.includes(`"${shortKey}"`);
186
+ const parts = key.split(".");
187
+ const shortKey = parts.length > 1 ? parts.slice(1).join(".") : key;
188
+ const lastSegment = parts[parts.length - 1];
189
+ const referenced = sourceContent.includes(`'${key}'`) || sourceContent.includes(`"${key}"`) || sourceContent.includes("`" + key + "`") || sourceContent.includes(`'${shortKey}'`) || sourceContent.includes(`"${shortKey}"`) || sourceContent.includes("`" + shortKey + "`") || // Also match the last dot-segment alone (e.g. t('submitButton'))
190
+ lastSegment !== key && (sourceContent.includes(`'${lastSegment}'`) || sourceContent.includes(`"${lastSegment}"`) || sourceContent.includes("`" + lastSegment + "`"));
162
191
  if (!referenced) {
163
192
  unusedKeys.push(key);
164
193
  warnings.push({ type: "unused-key", key, message: `Key "${key}" not referenced in source` });
package/package.json CHANGED
@@ -1,10 +1,22 @@
1
1
  {
2
2
  "name": "ai-localize-validators",
3
- "version": "2.0.5",
3
+ "version": "2.0.7",
4
4
  "description": "Locale file validation engine — missing, duplicate, placeholder and unused key validators",
5
+ "author": "ai-localize-core contributors",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/ai-localize/ai-localize-core.git",
10
+ "directory": "packages/validators"
11
+ },
12
+ "homepage": "https://github.com/ai-localize/ai-localize-core/tree/main/packages/validators#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/ai-localize/ai-localize-core/issues"
15
+ },
5
16
  "main": "./dist/index.js",
6
17
  "module": "./dist/index.mjs",
7
18
  "types": "./dist/index.d.ts",
19
+ "sideEffects": false,
8
20
  "files": [
9
21
  "dist",
10
22
  "README.md",
@@ -33,14 +45,13 @@
33
45
  "node": ">=18.0.0"
34
46
  },
35
47
  "dependencies": {
36
- "ai-localize-shared": "2.0.5"
48
+ "ai-localize-shared": "2.0.7"
37
49
  },
38
50
  "devDependencies": {
39
51
  "tsup": "^8.0.1",
40
52
  "typescript": "^5.3.3",
41
53
  "vitest": "^1.2.1"
42
54
  },
43
- "license": "MIT",
44
55
  "publishConfig": {
45
56
  "access": "public"
46
57
  },