Files
faumaray 3632f2cd43 update
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-25 23:44:48 +03:00

104 lines
3.3 KiB
JavaScript

#!/usr/bin/env node
// Diffs the Russian catalog (src/i18n/ru.ts) against every t('…') / tf('…')
// literal in src/**/*.ts{,x}, both ways:
// - missing: used in code but absent from ru.ts (falls back to English)
// - orphaned: present in ru.ts but never used as a literal key
//
// Dynamic keys can't be found by this scan and must be checked by hand:
// - SHORTCUTS labels (lib/shortcuts.ts) rendered via t(sc.label)
// - HEADS view titles/subtitles (App.tsx) rendered via t(title)/t(sub)
// Orphans reported here may be one of those dynamic keys — the KNOWN_DYNAMIC
// list below suppresses the ones we know about.
//
// Usage: npm run i18n:check (exits 1 when keys are missing)
import { readFileSync, readdirSync, statSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
const root = join(dirname(fileURLToPath(import.meta.url)), '..');
const srcDir = join(root, 'src');
const ruFile = join(srcDir, 'i18n', 'ru.ts');
// Keys reached through variables (see header); keep in sync by hand.
const KNOWN_DYNAMIC = new Set([
// connection state rendered via t(s.connection) in Sidebar.tsx
'connected',
'disconnected',
'error',
// SHORTCUTS labels (lib/shortcuts.ts)
'Play / pause',
'Seek 5s',
'Volume',
'Skip / vote skip',
'Focus search',
'This help',
// HEADS view titles/subtitles (App.tsx)
'Dashboard',
'Live playback and TeamSpeak server activity',
'Queue',
'Reorder and manage upcoming tracks',
'Search',
'Find tracks across YouTube and Yandex Music',
'Library',
'Your favorites and saved playlists',
'History & Stats',
'Playback history and server insights',
'Server & Admin',
'Manage subscribers and broadcast',
'Updates',
'Runtime, Core, and plugin release control',
'RPC / API',
'Connect to Echoion programmatically',
// Sidebar NAV labels rendered via t(item.label) — same strings as HEADS
// titles, listed above.
]);
const unescape = (s) => s.replace(/\\(.)/g, '$1');
function walk(dir, out = []) {
for (const name of readdirSync(dir)) {
const p = join(dir, name);
if (statSync(p).isDirectory()) walk(p, out);
else if (/\.tsx?$/.test(name) && !/\.test\.tsx?$/.test(name)) {
out.push(p);
}
}
return out;
}
// Catalog keys: top-level `'key': '…'` entries of the ru object literal.
const ruKeys = new Set();
for (const m of readFileSync(ruFile, 'utf8').matchAll(
/^\s*'((?:[^'\\]|\\.)*)':/gm
)) {
ruKeys.add(unescape(m[1]));
}
// Used keys: every t('…') / tf('…', …) string literal.
const used = new Set();
for (const file of walk(srcDir)) {
if (file === ruFile) continue;
const text = readFileSync(file, 'utf8');
for (const m of text.matchAll(/\btf?\(\s*'((?:[^'\\]|\\.)*)'/g)) {
used.add(unescape(m[1]));
}
}
const missing = [...used].filter((k) => !ruKeys.has(k)).sort();
const orphaned = [...ruKeys]
.filter((k) => !used.has(k) && !KNOWN_DYNAMIC.has(k))
.sort();
if (missing.length) {
console.log(`Missing from ru.ts (${missing.length}):`);
for (const k of missing) console.log(`\t'${k.replace(/'/g, "\\'")}':`);
}
if (orphaned.length) {
console.log(`\nIn ru.ts but never used as a literal (${orphaned.length}):`);
for (const k of orphaned) console.log(`\t'${k.replace(/'/g, "\\'")}'`);
console.log('(may be dynamic keys — verify before deleting)');
}
if (!missing.length && !orphaned.length) console.log('i18n: OK');
process.exit(missing.length ? 1 : 0);