CI: translation coverage job
A separate 'translations' job runs the i18n checker on every push and PR and writes a coverage table per language to the run summary, with the missing, broken and unused keys listed. The checker now also fails when a language file is not offered in SOT_LANGS or cached in sw.js.
This commit is contained in:
parent
0ecce20dec
commit
4e95b9b397
3 changed files with 50 additions and 7 deletions
|
|
@ -2,7 +2,9 @@
|
|||
// Checks a translation against the English text in site/index.html: every tr()/trn()/N_() literal and
|
||||
// every data-i18n text, aria-label and title in the static markup should have an entry, no stale entry
|
||||
// should linger, and each translation must keep the {placeholders} and HTML tags of its English key.
|
||||
// Usage: node scripts/i18n-check.js [lang ...] (default: every site/i18n/*.js)
|
||||
// Usage: node scripts/i18n-check.js [lang ...] [--summary FILE] (default: every site/i18n/*.js)
|
||||
// It also checks that each language is offered in the game and cached for offline play, and with --summary
|
||||
// appends a Markdown coverage table (the CI job writes it to the run's summary page).
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const vm = require('node:vm');
|
||||
|
|
@ -54,19 +56,48 @@ function check(keys, dict) {
|
|||
|
||||
module.exports = { loadDict, brokenEntries };
|
||||
|
||||
// A language file only works if the game offers it and the offline cache keeps it.
|
||||
function registration(lang) {
|
||||
const html = fs.readFileSync(path.join(SITE, 'index.html'), 'utf8');
|
||||
const sw = fs.readFileSync(path.join(SITE, 'sw.js'), 'utf8');
|
||||
const out = [];
|
||||
const m = html.match(/SOT_LANGS=\{([^}]*)\}/);
|
||||
if (!m || !new RegExp(String.raw`(^|[,{\s])${lang}\s*:`).test(m[1])) out.push('not in the language list (SOT_LANGS in site/index.html)');
|
||||
if (!sw.includes(`/i18n/${lang}.js`)) out.push('not cached for offline play (CORE in site/sw.js)');
|
||||
return out;
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
const args = process.argv.slice(2);
|
||||
const si = args.indexOf('--summary');
|
||||
const summary = si >= 0 ? args.splice(si, 2)[1] : null; // append a Markdown table here (CI: $GITHUB_STEP_SUMMARY)
|
||||
const keys = sourceKeys(fs.readFileSync(path.join(SITE, 'index.html'), 'utf8'));
|
||||
const langs = process.argv.slice(2);
|
||||
const files = (langs.length ? langs.map((l) => `${l}.js`) : fs.readdirSync(path.join(SITE, 'i18n')))
|
||||
const files = (args.length ? args.map((l) => `${l}.js`) : fs.readdirSync(path.join(SITE, 'i18n')))
|
||||
.map((f) => path.join(SITE, 'i18n', f));
|
||||
let bad = false;
|
||||
const rows = [];
|
||||
for (const file of files) {
|
||||
const lang = path.basename(file, '.js');
|
||||
const r = check(keys, loadDict(file));
|
||||
console.log(`${path.basename(file)}: ${r.missing.length} missing, ${r.unused.length} unused, ${r.broken.length} broken`);
|
||||
for (const [label, list] of [['missing', r.missing], ['unused', r.unused], ['broken', r.broken]]) {
|
||||
const reg = registration(lang);
|
||||
const cov = keys.size ? (keys.size - r.missing.length) / keys.size : 1;
|
||||
const pctTxt = `${(Math.floor(cov * 1000) / 10).toFixed(1)}%`;
|
||||
console.log(`${path.basename(file)}: ${r.missing.length} missing, ${r.unused.length} unused, ${r.broken.length} broken (${pctTxt} of ${keys.size} strings)`);
|
||||
for (const [label, list] of [['missing', r.missing], ['unused', r.unused], ['broken', r.broken], ['registration', reg]]) {
|
||||
for (const k of list) console.log(` ${label}: ${JSON.stringify(k)}`);
|
||||
}
|
||||
bad ||= r.missing.length + r.unused.length + r.broken.length > 0;
|
||||
const ok = r.missing.length + r.unused.length + r.broken.length + reg.length === 0;
|
||||
bad ||= !ok;
|
||||
rows.push(`| ${lang} | ${pctTxt} | ${r.missing.length} | ${r.unused.length} | ${r.broken.length} | ${reg.length ? reg.join('; ') : 'ok'} | ${ok ? '✅' : '❌'} |`);
|
||||
}
|
||||
if (summary) {
|
||||
const list = (title, items) => items.length ? `\n<details><summary>${title} (${items.length})</summary>\n\n${items.slice(0, 200).map((k) => '- `' + k.replace(/`/g, "'") + '`').join('\n')}\n</details>\n` : '';
|
||||
let md = `## Translations\n\n${keys.size} English strings in the game.\n\n| Language | Coverage | Missing | Unused | Broken | Registered | |\n|---|---|---|---|---|---|---|\n${rows.join('\n')}\n`;
|
||||
for (const file of files) {
|
||||
const r = check(keys, loadDict(file)), lang = path.basename(file, '.js');
|
||||
md += list(`${lang}: missing`, r.missing) + list(`${lang}: broken`, r.broken) + list(`${lang}: unused`, r.unused);
|
||||
}
|
||||
fs.appendFileSync(summary, md + '\n');
|
||||
}
|
||||
process.exit(bad ? 1 : 0);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue