Skip to content

LordOS — a free, fast and secure GNU/Linux distribution based on Debian, with its own desktop environment: display server and Wayland compositor in C, a React UI engine on QuickJS, and private native apps.

3.7 KB
Raw Blame
// i18n-check.mjs — checks the LordOS language catalogs.
//
//   node tools/i18n-check.mjs            every language in assets/locales
//   node tools/i18n-check.mjs pt         just one (or a pack: path to lordos-lang-xx.json)
//
// Reports: keys used in the code (t('namespace.key')) that are not in the reference
// catalog (en), keys each language is missing or has in excess compared to en, and
// {x} variables that do not match. Exits with code 1 if something is missing.
// With every language it also runs tools/err-check.mjs: error/status codes used by the server
// without an English string, and prose (or Spanish) in ERR lines and JS exceptions, and
// tools/brand-check.mjs: third-party brand names in the catalogs, Help, README and CHANGELOG.
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { checkErrors } from './err-check.mjs';
import { checkBrands } from './brand-check.mjs';

const root = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
const dir = path.join(root, 'assets/locales');
const REF = 'en';

function loadLang(id) {
  if (id.endsWith('.json') && fs.existsSync(id)) {
    const pack = JSON.parse(fs.readFileSync(id, 'utf8'));
    const { format, ...cats } = pack.catalogs;      // format.json is not texts
    return { id: pack.meta.id, cats };
  }
  const cats = {};
  for (const f of fs.readdirSync(path.join(dir, id))) {
    if (f.endsWith('.json') && f !== 'meta.json' && f !== 'format.json') cats[f.slice(0, -5)] = JSON.parse(fs.readFileSync(path.join(dir, id, f), 'utf8'));
  }
  return { id, cats };
}

const flat = (cats) => Object.fromEntries(Object.entries(cats).flatMap(([ns, o]) => Object.entries(o).map(([k, v]) => [`${ns}.${k}`, v])));
const vars = (v) => [...new Set((typeof v === 'object' ? Object.values(v).join(' ') : String(v)).match(/\{\w+\}/g) || [])].sort().join(',');

// keys used in the code
function usedKeys() {
  const out = new Map();
  const walk = (d) => {
    for (const e of fs.readdirSync(d, { withFileTypes: true })) {
      const p = path.join(d, e.name);
      if (e.isDirectory()) { if (!['node_modules', 'assets'].includes(e.name)) walk(p); continue; }
      if (!/\.(jsx?|mjs)$/.test(e.name) || e.name.endsWith('.bundle.js')) continue;
      const src = fs.readFileSync(p, 'utf8').replace(/^\s*\/\/.*$/gm, '');
      for (const m of src.matchAll(/\b(?:t|T|tr)\(\s*'([a-z][\w-]*\.[\w.-]+)'/g)) out.set(m[1], path.relative(root, p));
    }
  };
  for (const d of ['apps', 'themes', 'sdk']) walk(path.join(root, d));
  return out;
}

const ref = flat(loadLang(REF).cats);
let bad = 0;
for (const [k, file] of usedKeys()) if (!(k in ref)) { console.log(`✗ ${k} (used in ${file}) is not in ${REF}`); bad++; }

const langs = process.argv[2] ? [process.argv[2]] : fs.readdirSync(dir).filter((d) => d !== REF && fs.existsSync(path.join(dir, d, 'meta.json')));
for (const id of langs) {
  const l = loadLang(id);
  const cur = flat(l.cats);
  const missing = Object.keys(ref).filter((k) => !(k in cur));
  const extra = Object.keys(cur).filter((k) => !(k in ref));
  const varDiff = Object.keys(ref).filter((k) => k in cur && vars(ref[k]) !== vars(cur[k]));
  console.log(`${l.id}: ${Object.keys(cur).length}/${Object.keys(ref).length} keys` +
    `${missing.length ? `, ${missing.length} missing` : ''}${extra.length ? `, ${extra.length} extra` : ''}${varDiff.length ? `, different variables in ${varDiff.length}` : ''}`);
  for (const k of missing) console.log(`  missing ${k}`);
  for (const k of extra) console.log(`  extra   ${k}`);
  for (const k of varDiff) console.log(`  vars    ${k}: ${vars(ref[k])} ≠ ${vars(cur[k])}`);
  bad += missing.length + varDiff.length;
}
if (!process.argv[2]) bad += checkErrors().problems.length + checkBrands().problems.length;
process.exit(bad ? 1 : 0);