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:
Frederic Jung 2026-10-26 06:04:28 +01:00
parent 0ecce20dec
commit 4e95b9b397
No known key found for this signature in database
3 changed files with 50 additions and 7 deletions

View file

@ -21,3 +21,15 @@ jobs:
- run: npm test
- name: Build (not deployed — only releases deploy)
run: npm run build
translations:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: 24
cache: npm
- run: npm ci
- name: Translation coverage
run: node scripts/i18n-check.js --summary "$GITHUB_STEP_SUMMARY"

View file

@ -58,7 +58,7 @@ A tour of the code is in [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md).
Every player-facing string goes through `tr()`, `trn()` or `N_()`. Dictionaries live
in `site/i18n/` (English text → translation). To add a language, copy `it.js`, translate
the values, register the language in the language list in `site/index.html`, and run
`node scripts/i18n-check.js <lang>` until it reports nothing missing, unused or broken.
`node scripts/i18n-check.js <lang>` until it reports nothing missing, unused or broken. The CI job "translations" runs the same check on every pull request, shows each language's coverage in the run summary, and also fails if a new language is not added to the language list or to the offline cache in `site/sw.js`.
Language pull requests must be reviewed by a human who speaks the language, and
checked in the game itself, not only in the dictionary file. Passing the checker is

View file

@ -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);
}