diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 88089b9..47ba455 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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" diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8389cf6..0388682 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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 ` until it reports nothing missing, unused or broken. +`node scripts/i18n-check.js ` 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 diff --git a/scripts/i18n-check.js b/scripts/i18n-check.js index 17754f9..0b5ae8e 100644 --- a/scripts/i18n-check.js +++ b/scripts/i18n-check.js @@ -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
${title} (${items.length})\n\n${items.slice(0, 200).map((k) => '- `' + k.replace(/`/g, "'") + '`').join('\n')}\n
\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); }