50 lines
2.6 KiB
JavaScript
Executable File
50 lines
2.6 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
||
// Language pack helper (langs/<locale>.json).
|
||
// node scripts/i18n-missing.js pl_PL – keys from en_US.json missing in langs/pl_PL.json
|
||
// node scripts/i18n-missing.js --sources – UI texts found in web/ that are not in en_US.json
|
||
// node scripts/i18n-missing.js --new de_DE "Deutsch" "German"
|
||
// – creates langs/de_DE.json with all keys (empty values)
|
||
// Empty values fall back to English at runtime, so a pack can be translated incrementally.
|
||
'use strict';
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
const root = path.join(__dirname, '..');
|
||
const langsDir = path.join(root, 'langs');
|
||
const load = (code) => JSON.parse(fs.readFileSync(path.join(langsDir, code + '.json'), 'utf8'));
|
||
const en = load('en_US');
|
||
const arg = process.argv[2] || '--sources';
|
||
|
||
if (arg === '--new') {
|
||
const [code, nativeName, name] = process.argv.slice(3);
|
||
if (!code || !/^[a-z]{2,3}_[A-Z]{2}$/.test(code)) { console.error('usage: --new xx_YY "Native name" "English name"'); process.exit(2); }
|
||
const file = path.join(langsDir, code + '.json');
|
||
if (fs.existsSync(file)) { console.error(`${file} already exists`); process.exit(1); }
|
||
const tr = {};
|
||
for (const k of Object.keys(en.translations)) tr[k] = '';
|
||
const pack = { meta: { locale: code, name: name || code, native_name: nativeName || name || code, direction: 'ltr' }, translations: tr, patterns: [] };
|
||
fs.writeFileSync(file, JSON.stringify(pack, null, 2) + '\n');
|
||
console.error(`created ${file} (${Object.keys(tr).length} keys); see langs/pl_PL.json for pattern examples`);
|
||
process.exit(0);
|
||
}
|
||
|
||
let missing;
|
||
if (arg === '--sources') {
|
||
// Heuristic: human-readable fragments of string literals / HTML text in the panel sources.
|
||
const files = fs.readdirSync(path.join(root, 'web', 'js')).filter((f) => /\.js$/.test(f) && !/^10-/.test(f))
|
||
.map((f) => path.join(root, 'web', 'js', f)).concat([path.join(root, 'web', 'index.html')]);
|
||
const src = files.map((f) => fs.readFileSync(f, 'utf8')).join('\n');
|
||
const found = new Set();
|
||
for (const m of src.matchAll(/'((?:[^'\\\n]|\\.)*)'|>([^<>{}\n]+)</g)) {
|
||
for (const part of (m[1] || m[2] || '').replace(/<[^>]*>/g, '\n').split('\n')) {
|
||
const t = part.replace(/\\'/g, "'").trim();
|
||
if (/[A-Za-z]{3,}.*\s.*[A-Za-z]/.test(t) && !/[{}=;$]|^[\w-]+(\s[\w-]+)*=|https?:|\/api\//.test(t)) found.add(t);
|
||
}
|
||
}
|
||
missing = [...found].filter((k) => !(k in en.translations));
|
||
} else {
|
||
const pack = load(arg);
|
||
missing = Object.keys(en.translations).filter((k) => !pack.translations[k]);
|
||
}
|
||
missing.forEach((k) => console.log(k));
|
||
console.error(`${arg}: missing entries: ${missing.length}`);
|