Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): check tokens in locale-only plural forms

Yudhi Armyndharisa6c13415700e
diff --git a/dashboard/scripts/check-i18n-parity.mjs b/dashboard/scripts/check-i18n-parity.mjs
index b1fb0aed8..4a2bdf8ec 100644
--- a/dashboard/scripts/check-i18n-parity.mjs
+++ b/dashboard/scripts/check-i18n-parity.mjs
@@ -6,7 +6,8 @@
* 1. KEY PARITY (hard fail): every nested key path in en.json exists in each locale.
* 2. PLACEHOLDER PARITY (hard fail): a translated string carries the SAME `{{token}}` interpolation
* placeholders as the reference — a localized/renamed token (e.g. `{{nombre}}` instead of
- * `{{name}}`) silently breaks interpolation, which a key-presence check can't see.
+ * `{{name}}`) silently breaks interpolation, which a key-presence check can't see. A plural form
+ * only the locale has (fr `_many`, ar `_few`) may use no token its base's `_other` lacks.
* 3. UNTRANSLATED PROSE (warning): a long leaf value byte-identical to en.json is very likely still
* English — surfaced as a non-fatal drift signal (short coincidental matches are ignored).
* 4. PLURAL FORMS (hard fail): for every plural key in en.json (one with an `_other` variant), a
@@ -21,7 +22,8 @@ import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
const __dirname = dirname(fileURLToPath(import.meta.url));
-const LOCALES_DIR = join(__dirname, '..', 'src', 'i18n', 'locales');
+// An optional directory argument points the check at a copy of the catalogues (used by its own test).
+const LOCALES_DIR = process.argv[2] ?? join(__dirname, '..', 'src', 'i18n', 'locales');
const REFERENCE = 'en.json';
// A leaf value identical to the reference is only flagged when at least this long — short UI words
// (e.g. "Media", "OK") legitimately coincide across languages, full sentences almost never do.
@@ -71,6 +73,8 @@ function load(file) {
const referenceKeys = flatten(load(REFERENCE));
const referenceEntries = flattenEntries(load(REFERENCE));
const pluralBases = [...referenceKeys].filter((k) => k.endsWith('_other')).map((k) => k.slice(0, -'_other'.length));
+const pluralBaseSet = new Set(pluralBases);
+const PLURAL_SUFFIX = /_(zero|one|two|few|many|other)$/;
const localeFiles = readdirSync(LOCALES_DIR)
.filter((f) => f.endsWith('.json') && f !== REFERENCE)
.sort();
@@ -88,15 +92,25 @@ for (const file of localeFiles) {
.sort();
const extra = [...keys].filter((k) => !referenceKeys.has(k) && !pluralForms.has(k)).sort();
- const placeholderMismatches = [];
+ const placeholderMismatches = new Map(); // path -> expected tokens
const untranslated = [];
for (const [path, refVal] of referenceEntries) {
if (typeof refVal !== 'string') continue;
const val = entries.get(path);
if (typeof val !== 'string') continue;
- if (!setsEqual(placeholders(refVal), placeholders(val))) placeholderMismatches.push(path);
+ if (!setsEqual(placeholders(refVal), placeholders(val))) placeholderMismatches.set(path, placeholders(refVal));
if (refVal === val && refVal.length >= UNTRANSLATED_MIN_LEN) untranslated.push(path);
}
+ // A plural form en.json has no counterpart for (fr `_many`, ar `_few`, he `_two`) is held to the
+ // tokens of the base's `_other`. A subset, not equality: a form for one exact number may spell the
+ // number out ("two filters") and drop `{{count}}`, but a token the reference lacks renders literally.
+ for (const [path, val] of entries) {
+ if (referenceEntries.has(path) || typeof val !== 'string') continue;
+ const base = path.replace(PLURAL_SUFFIX, '');
+ if (base === path || !pluralBaseSet.has(base)) continue;
+ const expected = placeholders(referenceEntries.get(`${base}_other`));
+ if ([...placeholders(val)].some((t) => !expected.has(t))) placeholderMismatches.set(path, expected);
+ }
if (missing.length > 0) {
hasErrors = true;
@@ -106,11 +120,11 @@ for (const file of localeFiles) {
console.log(`[OK] ${file}: all ${referenceKeys.size} keys present`);
}
- if (placeholderMismatches.length > 0) {
+ if (placeholderMismatches.size > 0) {
hasErrors = true;
- console.error(`[FAIL] ${file}: ${placeholderMismatches.length} key(s) with mismatched {{placeholders}}:`);
- for (const k of placeholderMismatches) {
- console.error(` ! ${k}: expected ${[...placeholders(referenceEntries.get(k))].join(', ') || '(none)'}`);
+ console.error(`[FAIL] ${file}: ${placeholderMismatches.size} key(s) with mismatched {{placeholders}}:`);
+ for (const [k, expected] of placeholderMismatches) {
+ console.error(` ! ${k}: expected ${[...expected].join(', ') || '(none)'}`);
}
}
diff --git a/dashboard/src/i18n/check-i18n-parity.test.ts b/dashboard/src/i18n/check-i18n-parity.test.ts
new file mode 100644
index 000000000..fab1ffafb
--- /dev/null
+++ b/dashboard/src/i18n/check-i18n-parity.test.ts
@@ -0,0 +1,39 @@
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import { cpSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
+import { spawnSync } from 'node:child_process';
+import { tmpdir } from 'node:os';
+import { fileURLToPath } from 'node:url';
+import { dirname, join } from 'node:path';
+
+// Runs the real parity script against a copy of the catalogues with one value broken, so the check
+// is proven to catch the mistake rather than only to pass on the shipped files.
+const HERE = dirname(fileURLToPath(import.meta.url));
+const LOCALES_DIR = join(HERE, 'locales');
+const SCRIPT = join(HERE, '..', '..', 'scripts', 'check-i18n-parity.mjs');
+
+function runWith(file: string, edit: (catalogue: { chats: { channels: Record<string, string> } }) => void) {
+ const dir = mkdtempSync(join(tmpdir(), 'i18n-parity-'));
+ try {
+ cpSync(LOCALES_DIR, dir, { recursive: true });
+ const catalogue = JSON.parse(readFileSync(join(dir, file), 'utf8'));
+ edit(catalogue);
+ writeFileSync(join(dir, file), JSON.stringify(catalogue, null, 2));
+ return spawnSync(process.execPath, [SCRIPT, dir], { encoding: 'utf8' });
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+}
+
+test('the shipped catalogues pass the parity check', () => {
+ const result = spawnSync(process.execPath, [SCRIPT], { encoding: 'utf8' });
+ assert.equal(result.status, 0, result.stderr);
+});
+
+test('a renamed token in a plural form en.json does not have fails the check', () => {
+ const result = runWith('fr.json', c => {
+ c.chats.channels.subscribers_many = '{{cnt}} abonnés';
+ });
+ assert.equal(result.status, 1, result.stdout);
+ assert.match(result.stderr, /chats\.channels\.subscribers_many: expected count/);
+});