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:
Frederic Jung 2026-10-27 11:05:09 +01:00
parent d0ba85ef8f
commit e817beed1a
No known key found for this signature in database
94 changed files with 13681 additions and 10633 deletions

View file

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