Translations keyed by labels instead of English text
tr('pnis.delivery_2_to_6_weeks') instead of tr('Delivery: 2 to 6 weeks'): the
English lives once, in src/i18n/en.js (inlined into the page at build, so no
extra request), and it/pt/fr map the same labels. Changing English wording no
longer renames a key in every dictionary.
- 2,899 labels, made from where each text is used and its first words; short
shared words are common.*
- plurals are one label with {one, other}: trn('label', n)
- static markup: data-i18n="label", data-i18n-aria-label, data-i18n-title
- saves from 1.33 and older stored English text in a few places; tr() still
resolves it
- i18n-check: undefined/unused labels, markup drift, plural shape; i18n-add.js
works on labels and --rename updates src/ too
- the old-branch port tool gives old English-keyed code and dictionaries the
same labels
This commit is contained in:
parent
d0ba85ef8f
commit
e817beed1a
94 changed files with 13681 additions and 10633 deletions
|
|
@ -1,39 +1,69 @@
|
|||
#!/usr/bin/env node
|
||||
// Adds, changes, renames or removes dictionary entries, keeping every site/i18n/*.js sorted (scripts/lib/dict.js).
|
||||
// Adds, changes, renames or removes labels, keeping every dictionary sorted (scripts/lib/dict.js).
|
||||
// English is src/i18n/en.js; translations are site/i18n/<lang>.js.
|
||||
//
|
||||
// node scripts/i18n-add.js changes.json {"it": {"English": "Italiano", "Old text": null}, "pt": {…}}
|
||||
// a string adds or replaces the entry, null removes it
|
||||
// node scripts/i18n-add.js - the same JSON from stdin
|
||||
// node scripts/i18n-add.js --rename "Old English" "New English" move a key in every dictionary
|
||||
// node scripts/i18n-add.js changes.json {"en": {"pnis.ready": "Ready in {n} days"},
|
||||
// "it": {"pnis.ready": "Pronto tra {n} giorni"},
|
||||
// "fr": {"pnis.old_label": null}}
|
||||
// a string sets the text, {"one":…,"other":…} sets a plural,
|
||||
// null removes it (null under "en" removes it from every language)
|
||||
// node scripts/i18n-add.js - the same JSON from stdin
|
||||
// node scripts/i18n-add.js --rename old.label new.label in every dictionary and everywhere in src/
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const D = require('./lib/dict');
|
||||
|
||||
const LABEL = /^[a-z0-9_]+(\.[a-z0-9_]+)+$/;
|
||||
const all = () => ['en', ...D.langs()];
|
||||
const die = (m) => { console.error(m); process.exit(1); };
|
||||
const args = process.argv.slice(2);
|
||||
|
||||
if (args[0] === '--rename') {
|
||||
const [, from, to] = args;
|
||||
if (!from || !to) { console.error('usage: i18n-add.js --rename "Old English" "New English"'); process.exit(2); }
|
||||
for (const lang of D.langs()) {
|
||||
if (!LABEL.test(from || '') || !LABEL.test(to || '')) die('usage: i18n-add.js --rename old.label new.label');
|
||||
for (const lang of all()) {
|
||||
const { header, dict } = D.read(lang);
|
||||
if (!(from in dict)) { console.log(`${lang}: no entry for ${JSON.stringify(from)}`); continue; }
|
||||
if (to in dict && to !== from) { console.error(`${lang}: already has ${JSON.stringify(to)}`); process.exit(1); }
|
||||
if (!(from in dict)) continue;
|
||||
if (to in dict) die(`${lang}: already has ${to}`);
|
||||
dict[to] = dict[from];
|
||||
if (to !== from) delete dict[from];
|
||||
delete dict[from];
|
||||
D.write(lang, dict, header);
|
||||
console.log(`${lang}: renamed`);
|
||||
}
|
||||
// The code: quoted labels in tr()/trn()/N_() calls and tables, and data-i18n attributes.
|
||||
const { listFiles } = require('./lib/page');
|
||||
const src = path.join(__dirname, '..', 'src');
|
||||
let n = 0;
|
||||
for (const f of listFiles(src)) {
|
||||
if (f === 'i18n/en.js') continue;
|
||||
const p = path.join(src, f), t = fs.readFileSync(p, 'utf8');
|
||||
const u = t.replace(new RegExp(String.raw`(['"\x60])${from.replace(/\./g, '\\.')}\1`, 'g'), (m, q) => { n++; return q + to + q; });
|
||||
if (u !== t) fs.writeFileSync(p, u);
|
||||
}
|
||||
console.log(`renamed ${from} → ${to} in every dictionary and ${n} place(s) in src/`);
|
||||
process.exit(0);
|
||||
}
|
||||
if (args.length !== 1) { console.error('usage: i18n-add.js changes.json | - | --rename OLD NEW'); process.exit(2); }
|
||||
|
||||
if (args.length !== 1) die('usage: i18n-add.js changes.json | - | --rename old.label new.label');
|
||||
const changes = JSON.parse(fs.readFileSync(args[0] === '-' ? 0 : args[0], 'utf8'));
|
||||
for (const [lang, entries] of Object.entries(changes)) {
|
||||
if (!D.langs().includes(lang)) { console.error(`no dictionary site/i18n/${lang}.js`); process.exit(1); }
|
||||
const { header, dict } = D.read(lang);
|
||||
let add = 0, del = 0;
|
||||
for (const [en, xx] of Object.entries(entries)) {
|
||||
if (xx === null) { if (en in dict) { delete dict[en]; del++; } continue; }
|
||||
if (typeof xx !== 'string' || !xx.trim()) { console.error(`${lang}: empty translation for ${JSON.stringify(en)}`); process.exit(1); }
|
||||
dict[en] = xx; add++;
|
||||
const en = D.read('en').dict;
|
||||
const order = Object.keys(changes).sort((a, b) => (a === 'en' ? -1 : b === 'en' ? 1 : 0)); // English first
|
||||
for (const lang of order) {
|
||||
if (!all().includes(lang)) die(`no dictionary for ${lang} (English is src/i18n/en.js, translations site/i18n/<lang>.js)`);
|
||||
const { header, dict } = lang === 'en' ? { header: D.read('en').header, dict: en } : D.read(lang);
|
||||
let set = 0, del = 0;
|
||||
for (const [label, v] of Object.entries(changes[lang])) {
|
||||
if (!LABEL.test(label)) die(`${lang}: ${JSON.stringify(label)} is not a label like 'area.some_words'`);
|
||||
if (v === null) {
|
||||
if (label in dict) { delete dict[label]; del++; }
|
||||
if (lang === 'en') for (const l of D.langs()) { const d = D.read(l); if (label in d.dict) { delete d.dict[label]; D.write(l, d.dict, d.header); } }
|
||||
continue;
|
||||
}
|
||||
const ok = typeof v === 'string' ? !!v.trim() : v && typeof v.one === 'string' && typeof v.other === 'string' && v.one.trim() && v.other.trim();
|
||||
if (!ok) die(`${lang}: ${label}: give a non-empty string, or {"one": …, "other": …}`);
|
||||
if (lang !== 'en' && !(label in en)) die(`${lang}: ${label} has no English yet; add it under "en" first`);
|
||||
if (lang !== 'en' && (typeof v === 'string') !== (typeof en[label] === 'string')) die(`${lang}: ${label} must be ${typeof en[label] === 'string' ? 'a string' : 'a plural {one, other}'} like its English`);
|
||||
dict[label] = v; set++;
|
||||
}
|
||||
D.write(lang, dict, header);
|
||||
console.log(`${lang}: ${add} added or changed, ${del} removed`);
|
||||
console.log(`${lang}: ${set} set, ${del} removed`);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,72 +1,102 @@
|
|||
#!/usr/bin/env node
|
||||
// Checks a translation against the English text in the game page (assembled from src/): 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 ...] [--summary FILE] [--fix] (default: every site/i18n/*.js)
|
||||
// Dictionaries must also be sorted, one entry per line (scripts/lib/dict.js); --fix sorts them.
|
||||
// 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).
|
||||
// Checks the translations. Text is keyed by label: tr('area.some_label') in src/ and data-i18n="…"
|
||||
// (data-i18n-aria-label, data-i18n-title) in src/index.html, with the English in src/i18n/en.js.
|
||||
//
|
||||
// node scripts/i18n-check.js [lang ...] [--summary FILE] [--fix] (default: every site/i18n/*.js)
|
||||
//
|
||||
// English: every label the code uses is defined, every defined label is used, labels look like
|
||||
// `area.words`, and the English left in the static markup matches en.js.
|
||||
// Each translation: nothing missing, nothing stale, and every entry keeps the {placeholders}, HTML tags and
|
||||
// plural forms of its English. Every dictionary is sorted, one entry per line (--fix sorts them), each
|
||||
// language is offered in the game and cached for offline play. --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');
|
||||
const D = require('./lib/dict');
|
||||
const { CALL } = require('./legacy/labels');
|
||||
|
||||
const SITE = path.join(__dirname, '..', 'site');
|
||||
const page = () => require('./lib/page').assemble().html;
|
||||
const LIT = String.raw`'(?:[^'\\\n]|\\.)*'|"(?:[^"\\\n]|\\.)*"|\x60(?:[^\x60\\$]|\\.|\$(?!\{))*\x60`;
|
||||
const CALL = new RegExp(String.raw`(?<![\w$.])(tr|trn|N_)\(\s*(${LIT})(?:\s*,\s*(${LIT}))?`, 'g');
|
||||
const CTX = vm.createContext({});
|
||||
const literal = (s) => vm.runInContext(s, CTX);
|
||||
const LABEL = /^[a-z0-9_]+(\.[a-z0-9_]+)+$/;
|
||||
|
||||
// English keys used by the game. Calls whose text is not a literal cannot be checked and are skipped.
|
||||
function sourceKeys(html) {
|
||||
const keys = new Set();
|
||||
// The labels the game uses, with problems in how they are used.
|
||||
function sourceLabels(html) {
|
||||
const labels = new Set(), problems = [];
|
||||
const scripts = [...html.matchAll(/<script>([\s\S]*?)<\/script>/g)].map((m) => m[1]).join('\n');
|
||||
for (const m of scripts.matchAll(CALL)) {
|
||||
keys.add(literal(m[2]));
|
||||
if (m[1] === 'trn' && m[3]) keys.add(literal(m[3]));
|
||||
const l = m[2].slice(1, -1);
|
||||
if (!LABEL.test(l)) problems.push(`${m[1]}(${m[2].slice(0, 60)}…): not a label like 'area.some_words'`);
|
||||
else labels.add(l);
|
||||
if (m[1] === 'trn' && m[3]) problems.push(`trn(${m[2]}, ${m[3].slice(0, 30)}…): plurals take one label, trn('area.label', n)`);
|
||||
}
|
||||
const { JSDOM } = require('jsdom');
|
||||
const doc = new JSDOM(html).window.document;
|
||||
for (const el of doc.querySelectorAll('[data-i18n]')) keys.add(el.textContent);
|
||||
for (const a of ['aria-label', 'title']) for (const el of doc.querySelectorAll(`[${a}]`)) keys.add(el.getAttribute(a));
|
||||
return keys;
|
||||
const markup = [];
|
||||
for (const el of doc.querySelectorAll('[data-i18n]')) markup.push([el.dataset.i18n, el.textContent, 'text']);
|
||||
for (const a of ['aria-label', 'title']) for (const el of doc.querySelectorAll(`[data-i18n-${a}]`)) markup.push([el.getAttribute(`data-i18n-${a}`), el.getAttribute(a), a]);
|
||||
for (const el of doc.querySelectorAll('[aria-label]:not([data-i18n-aria-label]),[title]:not([data-i18n-title])')) {
|
||||
if (!el.closest('noscript')) problems.push(`<${el.tagName.toLowerCase()} ${el.hasAttribute('title') ? 'title' : 'aria-label'}="…"> in the markup has no data-i18n-${el.hasAttribute('title') ? 'title' : 'aria-label'} label`);
|
||||
}
|
||||
for (const [l] of markup) LABEL.test(l) ? labels.add(l) : problems.push(`data-i18n="${l}": not a label`);
|
||||
return { labels, markup, problems };
|
||||
}
|
||||
|
||||
function loadDict(file) {
|
||||
const window = {};
|
||||
vm.runInNewContext(fs.readFileSync(file, 'utf8'), { window });
|
||||
const lang = path.basename(file, '.js');
|
||||
return (window.I18N || {})[lang] || {};
|
||||
return D.parse(fs.readFileSync(file, 'utf8'), lang);
|
||||
}
|
||||
|
||||
const placeholders = (s) => (s.match(/\{\w+\}/g) || []).sort().join(' ');
|
||||
const tags = (s) => (s.match(/<\/?[a-z][a-z0-9]*/gi) || []).map((t) => t.toLowerCase()).sort().join(' ');
|
||||
const forms = (v) => (typeof v === 'string' ? [['', v]] : v && typeof v === 'object' ? [['one', v.one], ['other', v.other]] : [['', v]]);
|
||||
|
||||
// Entries whose translation drops or invents a {placeholder} or an HTML tag, or is empty.
|
||||
function brokenEntries(dict) {
|
||||
return Object.entries(dict).filter(([en, xx]) => typeof xx !== 'string' || !xx.trim() ||
|
||||
placeholders(en) !== placeholders(xx) || tags(en) !== tags(xx)).map(([en]) => en);
|
||||
// Entries whose translation is empty, has the wrong shape (plural or not), or drops or invents a
|
||||
// {placeholder} or an HTML tag. Without `en`, an entry is compared with itself (shape and emptiness only).
|
||||
function brokenEntries(dict, en = null) {
|
||||
return Object.entries(dict).filter(([l, xx]) => {
|
||||
const e = en ? en[l] : xx;
|
||||
if (e === undefined) return false; // stale, reported as unused
|
||||
if ((typeof e === 'string') !== (typeof xx === 'string')) return true;
|
||||
const ef = forms(e), xf = forms(xx);
|
||||
return xf.some(([k, x], i) => typeof x !== 'string' || !x.trim() || placeholders(ef[i][1]) !== placeholders(x) || tags(ef[i][1]) !== tags(x)) ||
|
||||
ef.some(([k]) => k && typeof xx[k] !== 'string');
|
||||
}).map(([l]) => l);
|
||||
}
|
||||
|
||||
function check(keys, dict) {
|
||||
function check(en, dict) {
|
||||
return {
|
||||
missing: [...keys].filter((k) => !(k in dict)),
|
||||
unused: Object.keys(dict).filter((k) => !keys.has(k)),
|
||||
broken: brokenEntries(dict),
|
||||
missing: Object.keys(en).filter((k) => !(k in dict)),
|
||||
unused: Object.keys(dict).filter((k) => !(k in en)),
|
||||
broken: brokenEntries(dict, en),
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = { loadDict, brokenEntries };
|
||||
// The English dictionary against the code.
|
||||
function checkEnglish(src, en) {
|
||||
const out = [...src.problems];
|
||||
for (const l of src.labels) if (!(l in en)) out.push(`label used but not in src/i18n/en.js: ${l}`);
|
||||
for (const l of Object.keys(en)) {
|
||||
if (!src.labels.has(l)) out.push(`in src/i18n/en.js but never used: ${l}`);
|
||||
if (!LABEL.test(l)) out.push(`not a label like 'area.some_words': ${l}`);
|
||||
}
|
||||
for (const [l, text, where] of src.markup) {
|
||||
if (typeof en[l] === 'string' && en[l] !== text) out.push(`markup ${where} for ${l} says ${JSON.stringify(text)}, en.js says ${JSON.stringify(en[l])}`);
|
||||
}
|
||||
for (const l of brokenEntries(en)) out.push(`empty or malformed English: ${l}`);
|
||||
if (!D.tidy('en')) out.push('src/i18n/en.js is not sorted one entry per line: run npm run i18n:fix');
|
||||
return out;
|
||||
}
|
||||
|
||||
module.exports = { loadDict, brokenEntries, sourceLabels };
|
||||
|
||||
// A language file only works if the game offers it and the offline cache keeps it.
|
||||
function registration(lang) {
|
||||
const html = page();
|
||||
function registration(lang, html) {
|
||||
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 src/index.html)');
|
||||
if (!sw.includes(`/i18n/${lang}.js`)) out.push('not cached for offline play (CORE in site/sw.js)');
|
||||
if (!require('./lib/dict').tidy(lang)) out.push('not sorted one entry per line: run npm run i18n:fix');
|
||||
if (!D.tidy(lang)) out.push('not sorted one entry per line: run npm run i18n:fix');
|
||||
return out;
|
||||
}
|
||||
|
||||
|
|
@ -77,23 +107,26 @@ if (require.main === module) {
|
|||
const fi = args.indexOf('--fix');
|
||||
if (fi >= 0) {
|
||||
args.splice(fi, 1);
|
||||
const D = require('./lib/dict');
|
||||
for (const lang of args.length ? args : D.langs()) { const { header, dict } = D.read(lang); D.write(lang, dict, header); }
|
||||
for (const lang of ['en', ...(args.length ? args : D.langs())]) { const { header, dict } = D.read(lang); D.write(lang, dict, header); }
|
||||
}
|
||||
const keys = sourceKeys(page());
|
||||
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));
|
||||
const reg = registration(lang);
|
||||
const cov = keys.size ? (keys.size - r.missing.length) / keys.size : 1;
|
||||
const html = page();
|
||||
const en = D.read('en').dict;
|
||||
const src = sourceLabels(html);
|
||||
const enProblems = checkEnglish(src, en);
|
||||
const n = Object.keys(en).length;
|
||||
console.log(`en.js: ${n} labels, ${enProblems.length} problems`);
|
||||
for (const p of enProblems) console.log(` ${p}`);
|
||||
let bad = enProblems.length > 0;
|
||||
const langs = args.length ? args : D.langs();
|
||||
const rows = [], results = {};
|
||||
for (const lang of langs) {
|
||||
const r = results[lang] = check(en, loadDict(D.fileOf(lang)));
|
||||
const reg = registration(lang, html);
|
||||
const cov = n ? (n - r.missing.length) / n : 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)`);
|
||||
console.log(`${lang}.js: ${r.missing.length} missing, ${r.unused.length} unused, ${r.broken.length} broken (${pctTxt} of ${n} labels)`);
|
||||
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)}`);
|
||||
for (const k of list) console.log(` ${label}: ${k}${label === 'missing' ? ` (${JSON.stringify(forms(en[k])[0][1]).slice(0, 80)})` : ''}`);
|
||||
}
|
||||
const ok = r.missing.length + r.unused.length + r.broken.length + reg.length === 0;
|
||||
bad ||= !ok;
|
||||
|
|
@ -101,9 +134,10 @@ if (require.main === module) {
|
|||
}
|
||||
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');
|
||||
let md = `## Translations\n\n${n} labels in the game${enProblems.length ? `, ${enProblems.length} problems with the English` : ''}.\n\n| Language | Coverage | Missing | Unused | Broken | Registered | |\n|---|---|---|---|---|---|---|\n${rows.join('\n')}\n`;
|
||||
md += list('English: problems', enProblems);
|
||||
for (const lang of langs) {
|
||||
const r = results[lang];
|
||||
md += list(`${lang}: missing`, r.missing) + list(`${lang}: broken`, r.broken) + list(`${lang}: unused`, r.unused);
|
||||
}
|
||||
fs.appendFileSync(summary, md + '\n');
|
||||
|
|
|
|||
138
scripts/legacy/labels.js
Normal file
138
scripts/legacy/labels.js
Normal file
|
|
@ -0,0 +1,138 @@
|
|||
// Turns English-keyed translation calls into label-keyed ones, for code written before labels (1.33 and
|
||||
// older): tr('Delivery: about a month') → tr('pnis.delivery_about_a_month'), trn('{n} POP','{n} POPs',n)
|
||||
// → trn('common.n_pop',n), and data-i18n markup → data-i18n="page.…". It is how src/ got its labels, and
|
||||
// transforms.js runs it on old branches so their work lines up with src/ (see port-branch.js).
|
||||
//
|
||||
// Labels for text that already has one come from `known` (the English dictionary, src/i18n/en.js);
|
||||
// new text gets a label made from its first words and where it is used, the same way every time.
|
||||
const vm = require('node:vm');
|
||||
|
||||
const LIT = String.raw`'(?:[^'\\\n]|\\.)*'|"(?:[^"\\\n]|\\.)*"|\x60(?:[^\x60\\$]|\\.|\$(?!\{))*\x60`;
|
||||
const CALL = new RegExp(String.raw`(?<![\w$.])(tr|trn|N_)\(\s*(${LIT})(?:\s*,\s*(${LIT}))?`, 'g');
|
||||
const CTX = vm.createContext({});
|
||||
const literal = (s) => vm.runInContext(s, CTX);
|
||||
|
||||
const hash = (t) => { let h = 2166136261; for (let i = 0; i < t.length; i++) { h ^= t.charCodeAt(i); h = Math.imul(h, 16777619); } return (h >>> 0).toString(16).padStart(8, '0'); };
|
||||
// "<b>Laser shortage!</b> {n} days…" → "laser_shortage_n_days"
|
||||
function slug(text) {
|
||||
const words = text.replace(/<[^>]*>/g, ' ').normalize('NFKD').replace(/[̀-ͯ]/g, '').toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, ' ').trim().split(' ').filter(Boolean);
|
||||
let s = '';
|
||||
for (const w of words) { if (s && (s + '_' + w).length > 36) break; s = s ? s + '_' + w : w; if (s.split('_').length >= 6) break; }
|
||||
return s || 'x';
|
||||
}
|
||||
// "game/70-rules/30-pnis.js" → "pnis"; the shell → "page".
|
||||
const areaOf = (file) => file === 'index.html' ? 'page' : file.split('/').pop().replace(/^\d+-/, '').replace(/\.[a-z]+$/, '').replace(/-/g, '_');
|
||||
const folderOf = (file) => file === 'index.html' ? 'page' : file.split('/').slice(0, -1).pop().replace(/^\d+-/, '').replace(/-/g, '_');
|
||||
// A plural is keyed by both forms.
|
||||
const pkey = (one, other) => `${one}\u0000${other}`;
|
||||
|
||||
const ENT = { amp: '&', lt: '<', gt: '>', quot: '"', '#39': "'", apos: "'", nbsp: ' ' };
|
||||
const decode = (s) => s.replace(/&(amp|lt|gt|quot|#39|apos|nbsp);/g, (m, e) => ENT[e]);
|
||||
|
||||
// Every translatable text in the partials: [{file, kind:'call'|'markup'|'attr', key, one, other, …}].
|
||||
function scan(parts) {
|
||||
const out = [];
|
||||
for (const [file, text] of parts) {
|
||||
if (file.endsWith('.js') || file === 'index.html') {
|
||||
for (const m of text.matchAll(CALL)) {
|
||||
if (file === 'index.html' && !inScript(text, m.index)) continue;
|
||||
if (m[1] === 'trn') {
|
||||
if (!m[3]) throw new Error(`${file}: trn() needs two literal forms: ${m[0].slice(0, 60)}`);
|
||||
const one = literal(m[2]), other = literal(m[3]);
|
||||
out.push({ file, kind: 'call', fn: 'trn', key: pkey(one, other), one, other, at: m.index + m[0].indexOf(m[2]), end: m.index + m[0].length });
|
||||
} else {
|
||||
const t = literal(m[2]);
|
||||
out.push({ file, kind: 'call', fn: m[1], key: t, text: t, at: m.index + m[0].indexOf(m[2]), end: m.index + m[0].indexOf(m[2]) + m[2].length });
|
||||
}
|
||||
}
|
||||
}
|
||||
if (file === 'index.html') {
|
||||
for (const m of text.matchAll(/<([a-z][a-z0-9]*)\b([^>]*?)\sdata-i18n>([^<]*)<\/\1>/g)) {
|
||||
out.push({ file, kind: 'markup', key: decode(m[3]), text: decode(m[3]), at: m.index + m[0].indexOf(' data-i18n>'), end: m.index + m[0].indexOf(' data-i18n>') + ' data-i18n'.length });
|
||||
}
|
||||
for (const m of text.matchAll(/<[a-z][a-z0-9]*\b[^>]*>/g)) {
|
||||
if (inScript(text, m.index)) continue;
|
||||
for (const a of m[0].matchAll(/\s(aria-label|title)="([^"]*)"/g)) {
|
||||
const pos = m.index + a.index + a[0].length;
|
||||
out.push({ file, kind: 'attr', attr: a[1], key: decode(a[2]), text: decode(a[2]), at: pos, end: pos });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
// Inside one of the shell's <script> elements (the language loader), where text is code, not markup.
|
||||
function inScript(html, i) {
|
||||
const open = html.lastIndexOf('<script', i), close = html.lastIndexOf('</script>', i);
|
||||
return open > close;
|
||||
}
|
||||
|
||||
// Labels for every text in the scan: known ones from `known` (Map key → label), new ones made here.
|
||||
function assign(items, known) {
|
||||
const labels = new Map(known), taken = new Set(known.values());
|
||||
const files = new Map();
|
||||
for (const it of items) { if (!files.has(it.key)) files.set(it.key, new Set()); files.get(it.key).add(it.file); }
|
||||
for (const it of items) {
|
||||
if (labels.has(it.key)) continue;
|
||||
const fs = [...files.get(it.key)];
|
||||
const folders = new Set(fs.map(folderOf));
|
||||
// Short, plain words ("Off", "Close", "Customers") are shared vocabulary wherever they appear.
|
||||
const short = !/[{<]/.test(it.one ?? it.text) && slug(it.one ?? it.text).split('_').length <= 2;
|
||||
const area = short ? 'common' : fs.length === 1 ? areaOf(fs[0]) : folders.size === 1 ? [...folders][0] : 'common';
|
||||
const base = `${area}.${slug(it.one ?? it.text)}`;
|
||||
let l = base;
|
||||
if (taken.has(l)) l = `${base}_${hash(it.key).slice(0, 4)}`;
|
||||
if (taken.has(l)) l = `${base}_${hash(it.key)}`;
|
||||
labels.set(it.key, l);
|
||||
taken.add(l);
|
||||
}
|
||||
return labels;
|
||||
}
|
||||
|
||||
// Rewrites the partials to use labels. Returns {parts, en}: en maps each label to its English
|
||||
// (a string, or {one, other} for plurals), for every text these partials use.
|
||||
function labelize(parts, known = new Map()) {
|
||||
const items = scan(parts);
|
||||
const labels = assign(items, known);
|
||||
const en = new Map();
|
||||
const byFile = new Map();
|
||||
for (const it of items) {
|
||||
const l = labels.get(it.key);
|
||||
en.set(l, it.fn === 'trn' ? { one: it.one, other: it.other } : it.text);
|
||||
if (!byFile.has(it.file)) byFile.set(it.file, []);
|
||||
byFile.get(it.file).push({ ...it, label: l });
|
||||
}
|
||||
const out = parts.map(([file, text]) => {
|
||||
const list = (byFile.get(file) || []).sort((a, b) => b.at - a.at);
|
||||
for (const it of list) {
|
||||
const rep = it.kind === 'call' ? `'${it.label}'`
|
||||
: it.kind === 'markup' ? ` data-i18n="${it.label}"`
|
||||
: ` data-i18n-${it.attr}="${it.label}"`;
|
||||
text = text.slice(0, it.at) + rep + text.slice(it.end);
|
||||
}
|
||||
return [file, text];
|
||||
});
|
||||
return { parts: out, en, labels };
|
||||
}
|
||||
|
||||
// The `known` map from an English dictionary object {label: text | {one, other}}.
|
||||
function knownFrom(enDict) {
|
||||
const k = new Map();
|
||||
for (const [l, v] of Object.entries(enDict || {})) k.set(typeof v === 'string' ? v : pkey(v.one, v.other), l);
|
||||
return k;
|
||||
}
|
||||
|
||||
// A dictionary keyed by English text (it.js before labels) → keyed by label, using the labels made above.
|
||||
function relabelDict(old, labels) {
|
||||
const out = {};
|
||||
for (const [key, l] of labels) {
|
||||
if (key.includes('\u0000')) {
|
||||
const [one, other] = key.split('\u0000');
|
||||
if (typeof old[one] === 'string' && typeof old[other] === 'string') out[l] = { one: old[one], other: old[other] };
|
||||
} else if (typeof old[key] === 'string') out[l] = old[key];
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
module.exports = { labelize, knownFrom, relabelDict, scan, slug, pkey, CALL, LIT };
|
||||
|
|
@ -11,7 +11,8 @@
|
|||
// How: your branch's site/index.html and the one you started from are both split with the same rules
|
||||
// that made src/ (split-index-html.js + transforms.js), and each partial gets a three-way merge
|
||||
// (git merge-file) into the src/ file from main. Your dictionary changes (site/i18n/*.js) are applied
|
||||
// entry by entry onto main's sorted dictionaries, so appended lines never conflict.
|
||||
// entry by entry onto main's sorted dictionaries, so appended lines never conflict. Text keyed by English
|
||||
// (before 1.34) gets the labels src/ uses, and new text gets new labels in src/i18n/en.js (labels.js).
|
||||
//
|
||||
// It also works the other way round, merging commits made on the old layout into a branch that has src/.
|
||||
// Options: --base REF (the pre-split commit the old-layout side started from; default: merge-base of HEAD
|
||||
|
|
@ -21,7 +22,8 @@ const os = require('node:os');
|
|||
const path = require('node:path');
|
||||
const { execFileSync, spawnSync } = require('node:child_process');
|
||||
const { split } = require('./split-index-html');
|
||||
const { transform } = require('./transforms');
|
||||
const { transformFull } = require('./transforms');
|
||||
const { relabelDict } = require('./labels');
|
||||
const D = require('../lib/dict');
|
||||
|
||||
const ROOT = path.join(__dirname, '..', '..');
|
||||
|
|
@ -48,7 +50,7 @@ if (!theirPage) die(`${theirs} has no site/index.html; nothing to port`);
|
|||
|
||||
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'tubes-port-'));
|
||||
const parts = (html, label) => {
|
||||
try { return new Map(transform(split(html))); }
|
||||
try { const r = transformFull(split(html)); return Object.assign(new Map(r.parts), { labels: r.labels }); }
|
||||
catch (e) { die(`could not split ${label}'s site/index.html the way src/ was made: ${e.message}`); }
|
||||
};
|
||||
const B = parts(basePage, 'the base'), T = parts(theirPage, 'your branch');
|
||||
|
|
@ -67,12 +69,17 @@ for (const [file, t] of T) {
|
|||
if (r.status !== 0) conflicts.push(`src/${file}`);
|
||||
}
|
||||
|
||||
// English merged cleanly: put it back in canonical order.
|
||||
if (!conflicts.includes('src/i18n/en.js') && fs.existsSync(D.fileOf('en'))) { const e = D.read('en'); D.write('en', e.dict, e.header); }
|
||||
|
||||
// Dictionaries: your additions, edits and removals, applied onto main's sorted files.
|
||||
for (const lang of D.langs()) {
|
||||
const rel = `site/i18n/${lang}.js`;
|
||||
const tb = show(base, rel), tt = show(theirs, rel);
|
||||
if (!tt || tb === tt) continue;
|
||||
const db = tb ? D.parse(tb, lang) : {}, dt = D.parse(tt, lang);
|
||||
// Old dictionaries are keyed by English text: give them the same labels the code got.
|
||||
const old = (t, labels) => { const d = D.parse(t, lang); return Object.keys(d).some((k) => /\s/.test(k)) ? relabelDict(d, labels) : d; };
|
||||
const db = tb ? old(tb, B.labels) : {}, dt = old(tt, T.labels);
|
||||
const main = target ? show(target, rel) : null;
|
||||
// During a merge the working file may hold conflict markers, so start from main's own copy.
|
||||
const { header, dict } = main ? { header: main.slice(0, main.indexOf('(window.I18N')), dict: D.parse(main, lang) } : D.read(lang);
|
||||
|
|
|
|||
|
|
@ -8,6 +8,8 @@
|
|||
// 2. Coordinate tables that lived on one very long line (CITY_LL, CITY_NUDGE, LANDS_LL) get one entry per
|
||||
// line, CITY_LL and CITY_NUDGE sorted by id: adding a city no longer edits a 4,000-character line
|
||||
// that everyone else is editing too.
|
||||
// 3. Translations are keyed by labels, not by English text (labels.js): tr('pnis.delivery_month') with the
|
||||
// English in src/i18n/en.js, inlined into the page by an `<!-- @include i18n -->` line in the shell.
|
||||
|
||||
// [file, first action in it]. A group runs until the next group's first action.
|
||||
const ACT_GROUPS = [
|
||||
|
|
@ -82,7 +84,7 @@ function oneLine(text, re, { sort = false, indent = ' ' } = {}) {
|
|||
}).join('\n');
|
||||
}
|
||||
|
||||
function transform(parts) {
|
||||
function tidy(parts) {
|
||||
const out = [];
|
||||
for (const [file, text] of parts) {
|
||||
if (file === 'game/90-actions/00-actions.js') out.push(...splitActions(text));
|
||||
|
|
@ -95,4 +97,24 @@ function transform(parts) {
|
|||
return out;
|
||||
}
|
||||
|
||||
module.exports = { transform, entries };
|
||||
const EN_HEADER = `// English: the text of every label the game uses (in tr(), trn() and N_() calls in src/, and data-i18n in src/index.html).
|
||||
// Inlined into the page at build. One entry per line, sorted by label; plurals are {"one":…,"other":…}.
|
||||
// Add or change text with scripts/i18n-add.js, check with npm run i18n:check.
|
||||
`;
|
||||
|
||||
// All the tidying, plus labels. `known` maps English text to the labels src/ already has, so text that
|
||||
// exists today keeps its label (default: the current src/i18n/en.js). Returns the parts and the labels used.
|
||||
function transformFull(parts, known) {
|
||||
const D = require('../lib/dict');
|
||||
const { labelize, knownFrom } = require('./labels');
|
||||
if (!known) known = knownFrom(require('node:fs').existsSync(D.fileOf('en')) ? D.read('en').dict : {});
|
||||
const { parts: labelled, en, labels } = labelize(tidy(parts), known);
|
||||
const out = labelled.map(([file, text]) => file === 'index.html'
|
||||
? [file, text.replace('<!-- @include game -->', '<!-- @include i18n -->\n<!-- @include game -->')] : [file, text]);
|
||||
out.push(['i18n/en.js', D.format('en', EN_HEADER, Object.fromEntries(en))]);
|
||||
return { parts: out, labels };
|
||||
}
|
||||
|
||||
const transform = (parts, known) => transformFull(parts, known).parts;
|
||||
|
||||
module.exports = { transform, transformFull, tidy, entries };
|
||||
|
|
|
|||
|
|
@ -1,15 +1,18 @@
|
|||
// Reads and writes the translation dictionaries in site/i18n/<lang>.js.
|
||||
// Reads and writes the dictionaries: English in src/i18n/en.js, translations in site/i18n/<lang>.js.
|
||||
// Each maps a label to its text, or for plurals to {"one": …, "other": …}.
|
||||
//
|
||||
// Every dictionary is kept in one canonical layout: the header comment, then one `"English":"Translation",`
|
||||
// entry per line, sorted by the English text. Sorted entries spread new strings across the file instead of
|
||||
// Every dictionary is kept in one canonical layout: the header comment, then one `"label":"Text",`
|
||||
// entry per line, sorted by label. Sorted entries spread new strings across the file instead of
|
||||
// piling them up at the end, so two pull requests that each add strings rarely touch the same lines.
|
||||
// `npm run i18n:check` reports a dictionary that is out of order; `npm run i18n:fix` puts it back.
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const vm = require('node:vm');
|
||||
|
||||
// English lives with the source (it is inlined into the page); translations are static files in site/i18n/.
|
||||
const DIR = path.join(__dirname, '..', '..', 'site', 'i18n');
|
||||
const fileOf = (lang) => path.join(DIR, `${lang}.js`);
|
||||
const EN = path.join(__dirname, '..', '..', 'src', 'i18n', 'en.js');
|
||||
const fileOf = (lang) => (lang === 'en' ? EN : path.join(DIR, `${lang}.js`));
|
||||
const langs = () => fs.readdirSync(DIR).filter((f) => f.endsWith('.js')).map((f) => path.basename(f, '.js')).sort();
|
||||
|
||||
// Plain code-unit order: the same on every machine, whatever its locale.
|
||||
|
|
@ -43,4 +46,4 @@ function write(lang, dict, header = read(lang).header) {
|
|||
fs.writeFileSync(fileOf(lang), format(lang, header, dict));
|
||||
}
|
||||
|
||||
module.exports = { DIR, fileOf, langs, read, write, format, tidy, parse, cmp };
|
||||
module.exports = { DIR, EN, fileOf, langs, read, write, format, tidy, parse, cmp };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue