@posiwise/i18n 1.0.69 → 1.0.71
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/check-parity.js +130 -0
- package/check-parity.test.js +140 -0
- package/de.json +1809 -103
- package/en.json +2 -2
- package/es.json +1819 -114
- package/flows/de.json +24 -3
- package/flows/en.json +24 -3
- package/flows/es.json +24 -3
- package/flows/pt.json +24 -3
- package/package.json +1 -1
- package/pt.json +1865 -103
- package/smart-crm/de.json +384 -1
- package/smart-crm/es.json +384 -1
- package/smart-crm/pt.json +384 -1
package/check-parity.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/*
|
|
3
|
+
* i18n locale parity check (#4204).
|
|
4
|
+
*
|
|
5
|
+
* Compares every non-English locale file in each given directory against
|
|
6
|
+
* that directory's en.json and fails (exit 1) on:
|
|
7
|
+
* - missing keys (in en, absent or empty in the locale)
|
|
8
|
+
* - extra keys (in the locale, absent from en)
|
|
9
|
+
* - untranslated copy (value byte-identical to en, not allowlisted)
|
|
10
|
+
* - broken placeholders ({{ x }} tokens or HTML tags that differ from en)
|
|
11
|
+
*
|
|
12
|
+
* Ships inside @posiwise/i18n so consumer repos run the same rules:
|
|
13
|
+
* node node_modules/@posiwise/i18n/check-parity.js \
|
|
14
|
+
* --allowlist i18n-parity-allowlist.json src/assets/i18n/cloudolive
|
|
15
|
+
*
|
|
16
|
+
* Allowlist JSON:
|
|
17
|
+
* { "values": ["PosiWise"], "localeValues": { "de": ["Status"] }, "keys": { "de": ["Some.Key"] } }
|
|
18
|
+
* values -> identical value accepted in every locale (brands, acronyms)
|
|
19
|
+
* localeValues -> identical value accepted in that locale (genuine loanwords)
|
|
20
|
+
* keys -> identical value accepted for that key in that locale
|
|
21
|
+
*/
|
|
22
|
+
const fs = require('fs');
|
|
23
|
+
const path = require('path');
|
|
24
|
+
|
|
25
|
+
const LOCALES = ['de', 'es', 'pt'];
|
|
26
|
+
const PLACEHOLDER = /\{\{\s*[^}]+?\s*\}\}|\{[a-zA-Z_][\w.]*\}|<\/?[a-zA-Z][^>]*>/g;
|
|
27
|
+
// A single whitespace-free token carrying a technical symbol (URL, email, path, code) is language-neutral.
|
|
28
|
+
const TECHNICAL = /^[^\s]*[@/:_#][^\s]+$|^[\w-]+(\.[\w-]+)+$/;
|
|
29
|
+
// Once placeholders are stripped, a value with at most one letter ("s", "v{{n}}") needs no translation.
|
|
30
|
+
const LETTER = /\p{L}/gu;
|
|
31
|
+
|
|
32
|
+
function parseArgs(argv) {
|
|
33
|
+
const args = { allowlist: null, dirs: [] };
|
|
34
|
+
for (let i = 0; i < argv.length; i++) {
|
|
35
|
+
if (argv[i] === '--allowlist') args.allowlist = argv[++i];
|
|
36
|
+
else args.dirs.push(argv[i]);
|
|
37
|
+
}
|
|
38
|
+
return args;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function flatten(obj, prefix = '', out = {}) {
|
|
42
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
43
|
+
const full = prefix ? `${prefix}.${key}` : key;
|
|
44
|
+
if (value && typeof value === 'object') flatten(value, full, out);
|
|
45
|
+
else out[full] = value;
|
|
46
|
+
}
|
|
47
|
+
return out;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function readJson(file) {
|
|
51
|
+
return JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function tokens(value) {
|
|
55
|
+
return (String(value).match(PLACEHOLDER) || [])
|
|
56
|
+
.map(t => t.replace(/\s+/g, ''))
|
|
57
|
+
.sort()
|
|
58
|
+
.join('|');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function loadAllowlist(file) {
|
|
62
|
+
const raw = file ? readJson(file) : {};
|
|
63
|
+
const bySet = obj =>
|
|
64
|
+
Object.fromEntries(Object.entries(obj || {}).map(([l, list]) => [l, new Set(list)]));
|
|
65
|
+
return {
|
|
66
|
+
values: new Set(raw.values || []),
|
|
67
|
+
localeValues: bySet(raw.localeValues),
|
|
68
|
+
keys: bySet(raw.keys)
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function isAllowedIdentical(allow, locale, key, value) {
|
|
73
|
+
const words = String(value).replace(PLACEHOLDER, '').match(LETTER) || [];
|
|
74
|
+
if (words.length <= 1 || TECHNICAL.test(String(value)) || allow.values.has(value)) return true;
|
|
75
|
+
if (allow.localeValues[locale] && allow.localeValues[locale].has(value)) return true;
|
|
76
|
+
return Boolean(allow.keys[locale] && allow.keys[locale].has(key));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function checkLocale(dir, locale, en, allow) {
|
|
80
|
+
const file = path.join(dir, `${locale}.json`);
|
|
81
|
+
if (!fs.existsSync(file)) return [`${file}: file missing`];
|
|
82
|
+
const target = flatten(readJson(file));
|
|
83
|
+
const problems = [];
|
|
84
|
+
for (const [key, enValue] of Object.entries(en)) {
|
|
85
|
+
const value = target[key];
|
|
86
|
+
if (enValue === '' || enValue === null) continue;
|
|
87
|
+
if (value === undefined || value === '' || value === null) {
|
|
88
|
+
problems.push(`missing ${key}`);
|
|
89
|
+
} else if (value === enValue && !isAllowedIdentical(allow, locale, key, value)) {
|
|
90
|
+
problems.push(`untranslated ${key} = ${JSON.stringify(value)}`);
|
|
91
|
+
} else if (tokens(value) !== tokens(enValue)) {
|
|
92
|
+
problems.push(
|
|
93
|
+
`placeholder ${key}: en ${tokens(enValue) || '-'} vs ${tokens(value) || '-'}`
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
for (const key of Object.keys(target)) {
|
|
98
|
+
if (!(key in en)) problems.push(`extra ${key}`);
|
|
99
|
+
}
|
|
100
|
+
return problems.map(p => `${file}: ${p}`);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function checkDir(dir, allow) {
|
|
104
|
+
const enFile = path.join(dir, 'en.json');
|
|
105
|
+
if (!fs.existsSync(enFile)) return [`${enFile}: file missing`];
|
|
106
|
+
const en = flatten(readJson(enFile));
|
|
107
|
+
return LOCALES.flatMap(locale => checkLocale(dir, locale, en, allow));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function main() {
|
|
111
|
+
const args = parseArgs(process.argv.slice(2));
|
|
112
|
+
if (!args.dirs.length) {
|
|
113
|
+
console.error('usage: check-parity.js [--allowlist file.json] <i18n dir>...');
|
|
114
|
+
process.exit(2);
|
|
115
|
+
}
|
|
116
|
+
const allow = loadAllowlist(args.allowlist);
|
|
117
|
+
const problems = args.dirs.flatMap(dir => checkDir(dir, allow));
|
|
118
|
+
if (problems.length) {
|
|
119
|
+
problems.forEach(p => console.error(p));
|
|
120
|
+
console.error(
|
|
121
|
+
`\n❌ i18n parity: ${problems.length} problem(s) in ${args.dirs.length} dir(s)`
|
|
122
|
+
);
|
|
123
|
+
process.exit(1);
|
|
124
|
+
}
|
|
125
|
+
console.log(`✅ i18n parity: ${args.dirs.length} dir(s) — ${LOCALES.join(', ')} match en`);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (require.main === module) main();
|
|
129
|
+
|
|
130
|
+
module.exports = { flatten, tokens, checkDir, loadAllowlist };
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// Run: node --test libs/i18n/check-parity.test.js
|
|
2
|
+
const test = require('node:test');
|
|
3
|
+
const assert = require('node:assert');
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const os = require('os');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const { flatten, tokens, checkDir, loadAllowlist } = require('./check-parity.js');
|
|
8
|
+
|
|
9
|
+
function fixture(files) {
|
|
10
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'i18n-parity-'));
|
|
11
|
+
for (const [name, data] of Object.entries(files))
|
|
12
|
+
fs.writeFileSync(path.join(dir, name), JSON.stringify(data));
|
|
13
|
+
return dir;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const EN = {
|
|
17
|
+
'Button.Save': 'Save',
|
|
18
|
+
'Label.Status': 'Status',
|
|
19
|
+
'Msg.Count': '{{ count }} items in <b>{slug}</b>'
|
|
20
|
+
};
|
|
21
|
+
const good = {
|
|
22
|
+
de: {
|
|
23
|
+
'Button.Save': 'Speichern',
|
|
24
|
+
'Label.Status': 'Status',
|
|
25
|
+
'Msg.Count': '{{count}} Einträge in <b>{slug}</b>'
|
|
26
|
+
},
|
|
27
|
+
es: {
|
|
28
|
+
'Button.Save': 'Guardar',
|
|
29
|
+
'Label.Status': 'Estado',
|
|
30
|
+
'Msg.Count': '{{ count }} elementos en <b>{slug}</b>'
|
|
31
|
+
},
|
|
32
|
+
pt: {
|
|
33
|
+
'Button.Save': 'Guardar',
|
|
34
|
+
'Label.Status': 'Estado',
|
|
35
|
+
'Msg.Count': '{{ count }} itens em <b>{slug}</b>'
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
const allow = () => ({ values: new Set(), localeValues: { de: new Set(['Status']) }, keys: {} });
|
|
39
|
+
|
|
40
|
+
test('flatten handles nested and dotted keys', () => {
|
|
41
|
+
assert.deepStrictEqual(flatten({ a: { b: 'x' }, 'c.d': 'y', e: {} }), {
|
|
42
|
+
'a.b': 'x',
|
|
43
|
+
'c.d': 'y'
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('tokens ignore whitespace inside interpolation', () => {
|
|
48
|
+
assert.strictEqual(tokens('{{ n }} <b>x</b> {slug}'), tokens('<b>y</b>{slug} {{n}}'));
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test('clean locales pass, with a locale loanword allowlisted', () => {
|
|
52
|
+
const dir = fixture({
|
|
53
|
+
'en.json': EN,
|
|
54
|
+
'de.json': good.de,
|
|
55
|
+
'es.json': good.es,
|
|
56
|
+
'pt.json': good.pt
|
|
57
|
+
});
|
|
58
|
+
assert.deepStrictEqual(checkDir(dir, allow()), []);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('reports missing, extra, untranslated and placeholder problems', () => {
|
|
62
|
+
const de = { 'Label.Status': 'Status', 'Msg.Count': '{{ total }} Einträge', 'Old.Key': 'Alt' };
|
|
63
|
+
const es = { ...good.es, 'Button.Save': 'Save' };
|
|
64
|
+
const dir = fixture({
|
|
65
|
+
'en.json': EN,
|
|
66
|
+
'de.json': de,
|
|
67
|
+
'es.json': es,
|
|
68
|
+
'pt.json': { ...good.pt, 'Button.Save': '' }
|
|
69
|
+
});
|
|
70
|
+
const out = checkDir(dir, allow()).join('\n');
|
|
71
|
+
assert.match(out, /de\.json: missing +Button\.Save/);
|
|
72
|
+
assert.match(out, /de\.json: placeholder +Msg\.Count/);
|
|
73
|
+
assert.match(out, /de\.json: extra +Old\.Key/);
|
|
74
|
+
assert.match(out, /es\.json: untranslated Button\.Save/);
|
|
75
|
+
assert.match(out, /pt\.json: missing +Button\.Save/);
|
|
76
|
+
assert.doesNotMatch(out, /de\.json: untranslated Label\.Status/);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test('language-neutral values are accepted without an allowlist', () => {
|
|
80
|
+
const en = {
|
|
81
|
+
a: 'https://example.com',
|
|
82
|
+
b: 'name@example.com',
|
|
83
|
+
c: 'blog.example.com',
|
|
84
|
+
d: 'v{{n}}',
|
|
85
|
+
e: 's'
|
|
86
|
+
};
|
|
87
|
+
const dir = fixture({ 'en.json': en, 'de.json': en, 'es.json': en, 'pt.json': en });
|
|
88
|
+
assert.deepStrictEqual(checkDir(dir, loadAllowlist(null)), []);
|
|
89
|
+
// A word that only ends in a symbol is still a word: "Error:" must be translated.
|
|
90
|
+
const words = fixture({
|
|
91
|
+
'en.json': { a: 'Error:' },
|
|
92
|
+
'de.json': { a: 'Error:' },
|
|
93
|
+
'es.json': { a: 'Error:' },
|
|
94
|
+
'pt.json': { a: 'Erro:' }
|
|
95
|
+
});
|
|
96
|
+
assert.strictEqual(checkDir(words, loadAllowlist(null)).length, 2);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test('missing locale file and per-key allowlist', () => {
|
|
100
|
+
const file = path.join(fixture({}), 'allow.json');
|
|
101
|
+
fs.writeFileSync(file, JSON.stringify({ values: ['PosiWise'], keys: { es: ['Button.Save'] } }));
|
|
102
|
+
const a = loadAllowlist(file);
|
|
103
|
+
assert.ok(a.values.has('PosiWise') && a.keys.es.has('Button.Save'));
|
|
104
|
+
const dir = fixture({
|
|
105
|
+
'en.json': EN,
|
|
106
|
+
'de.json': good.de,
|
|
107
|
+
'es.json': { ...good.es, 'Button.Save': 'Save' }
|
|
108
|
+
});
|
|
109
|
+
assert.deepStrictEqual(checkDir(dir, { ...a, localeValues: { de: new Set(['Status']) } }), [
|
|
110
|
+
`${dir}/pt.json: file missing`
|
|
111
|
+
]);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test('CLI exits 0 on clean dirs, 1 on problems, 2 without arguments', () => {
|
|
115
|
+
const { spawnSync } = require('node:child_process');
|
|
116
|
+
const run = (...args) =>
|
|
117
|
+
spawnSync(process.execPath, [path.join(__dirname, 'check-parity.js'), ...args], {
|
|
118
|
+
encoding: 'utf8'
|
|
119
|
+
});
|
|
120
|
+
const allowFile = path.join(fixture({}), 'allow.json');
|
|
121
|
+
fs.writeFileSync(allowFile, JSON.stringify({ localeValues: { de: ['Status'] } }));
|
|
122
|
+
const clean = fixture({
|
|
123
|
+
'en.json': EN,
|
|
124
|
+
'de.json': good.de,
|
|
125
|
+
'es.json': good.es,
|
|
126
|
+
'pt.json': good.pt
|
|
127
|
+
});
|
|
128
|
+
const broken = fixture({
|
|
129
|
+
'en.json': EN,
|
|
130
|
+
'de.json': {},
|
|
131
|
+
'es.json': good.es,
|
|
132
|
+
'pt.json': good.pt
|
|
133
|
+
});
|
|
134
|
+
assert.strictEqual(run('--allowlist', allowFile, clean).status, 0);
|
|
135
|
+
const failed = run('--allowlist', allowFile, broken);
|
|
136
|
+
assert.strictEqual(failed.status, 1);
|
|
137
|
+
assert.match(failed.stderr, /3 problem\(s\)/);
|
|
138
|
+
assert.strictEqual(run().status, 2);
|
|
139
|
+
assert.strictEqual(run(fixture({})).status, 1);
|
|
140
|
+
});
|