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
49 lines
2.3 KiB
JavaScript
49 lines
2.3 KiB
JavaScript
// 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 `"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 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.
|
|
const cmp = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
|
|
function parse(text, lang) {
|
|
const window = {};
|
|
vm.runInNewContext(text, { window });
|
|
return (window.I18N || {})[lang] || {};
|
|
}
|
|
|
|
function read(lang) {
|
|
const text = fs.readFileSync(fileOf(lang), 'utf8');
|
|
const open = text.indexOf(`(window.I18N=window.I18N||{}).${lang}={`);
|
|
if (open < 0) throw new Error(`${lang}.js: missing the (window.I18N=window.I18N||{}).${lang}={ line`);
|
|
return { text, header: text.slice(0, open), dict: parse(text, lang) };
|
|
}
|
|
|
|
function format(lang, header, dict) {
|
|
const keys = Object.keys(dict).sort(cmp);
|
|
return `${header}(window.I18N=window.I18N||{}).${lang}={\n${keys.map((k) => `${JSON.stringify(k)}:${JSON.stringify(dict[k])},\n`).join('')}};\n`;
|
|
}
|
|
|
|
// True when the file is already in the canonical layout.
|
|
function tidy(lang) {
|
|
const { text, header, dict } = read(lang);
|
|
return text === format(lang, header, dict);
|
|
}
|
|
|
|
function write(lang, dict, header = read(lang).header) {
|
|
fs.writeFileSync(fileOf(lang), format(lang, header, dict));
|
|
}
|
|
|
|
module.exports = { DIR, EN, fileOf, langs, read, write, format, tidy, parse, cmp };
|