diff --git a/docs/translations/zh-Hant.md b/docs/translations/zh-Hant.md index bedef3d8ba..7875ac9ab9 100644 --- a/docs/translations/zh-Hant.md +++ b/docs/translations/zh-Hant.md @@ -10,7 +10,7 @@ npm run i18n -- --zh-hant # rewrite zh-Hant.json from zh-CN.json `npm test` fails when `zh-Hant.json` differs from the conversion of `zh-CN.json`, naming the entry, and `npm run i18n -- --missing zh-Hant` points to this command instead of printing a template. -The command converts with OpenCC `s2tw` ([opencc-js](https://github.com/nk2028/opencc-js), `cn` → `tw`), which changes characters only. Never `s2twp`: it also swaps in Taiwan vocabulary (影片, 伺服器, 載入, 圖示), which this catalog does not use. After the conversion it fixes the characters OpenCC picks wrongly here: 年曆史 → 年歷史, 下遊 → 下游, 並道 → 併道 (not in 並道路, 並道歉, 並道理), 發佈 → 發布, 公佈 → 公布, 控制檯 → 控制台, and 周 → 週 where it means a week (1 週/秒). The list is `ZH_HANT_CORRECTIONS` in `scripts/i18n.mjs`; when the conversion picks another wrong character, add the pair there and run the command. +The command converts with OpenCC `s2tw` ([opencc-js](https://github.com/nk2028/opencc-js), `cn` → `tw`), which changes characters only. Never `s2twp`: it also swaps in Taiwan vocabulary (影片, 伺服器, 載入, 圖示), which this catalog does not use. After the conversion it fixes the characters OpenCC picks wrongly here: 年曆史 → 年歷史, 下遊 → 下游, 並道 → 併道 (only in entries whose English key says merge), 發佈 → 發布, 公佈 → 公布, 宣佈 → 宣布, 控制檯 → 控制台, 箇中 → 個中 (全部 {n} 個中), and 周 → 週 in ` 周/秒` (1 週/秒, weeks per second). OpenCC writes 週 itself in 每週, 本週, 上週, 一週 and 兩週, but not after a number (過去 2 周, {count} 周前): a new string like that needs its own pair. The list is `ZH_HANT_CORRECTIONS` in `scripts/i18n.mjs`; when the conversion picks another wrong character, add the pair there (with a key pattern if it is right only in some entries) and run the command. An entry `zh-CN` keeps in English on purpose needs the same line under `zh-Hant` in `src/i18n/same-as-english.json`. diff --git a/scripts/i18n.mjs b/scripts/i18n.mjs index b125a7a53d..c007424d5e 100644 --- a/scripts/i18n.mjs +++ b/scripts/i18n.mjs @@ -513,10 +513,11 @@ export function readCatalog(locale) { return JSON.parse(readFileSync(catalogPath(locale), 'utf8')); } +/** Sorted: git lists untracked files first, and main needs zh-CN before zh-Hant. */ export function shippedLocales() { - return trackedFiles('src/i18n/locales/*.json').map((file) => - path.basename(file, '.json'), - ); + return trackedFiles('src/i18n/locales/*.json') + .map((file) => path.basename(file, '.json')) + .sort(); } /** Messages a locale keeps exactly as in English, on purpose. */ @@ -535,7 +536,9 @@ const blank = (value) => value.trim() === ''; const hasLetters = (value) => /\p{L}{2}/u.test(value.replace(/\{[^}]*\}/g, '')); const STILL_ENGLISH = (text, locale) => - `"${text}" is still English: translate it, or list it under "${locale}" in src/i18n/same-as-english.json`; + locale === ZH_HANT + ? `"${text}" is still English: translate it in ${ZH_HANT_SOURCE} and run ${ZH_HANT_COMMAND}, or list it under "${ZH_HANT}" in src/i18n/same-as-english.json as under "${ZH_HANT_SOURCE}"` + : `"${text}" is still English: translate it, or list it under "${locale}" in src/i18n/same-as-english.json`; /** * Compare one catalog with the extracted messages. @@ -631,64 +634,79 @@ const ZH_HANT_SOURCE = 'zh-CN'; const ZH_HANT_COMMAND = 'npm run i18n -- --zh-hant'; // Characters s2tw picks wrongly in this text, replaced after the conversion. +// A pair with a third item applies only to entries whose English key matches it. const ZH_HANT_CORRECTIONS = [ ['年曆史', '年歷史'], ['下遊', '下游'], - // 併道 is merging lanes; 並 stays when 道 starts 道路, 道歉 or 道理. - [/並道(?![路歉理])/g, '併道'], + // 併道 is merging lanes; elsewhere 並 (and) stays before 道路, 道謝 and the like. + ['並道', '併道', /\bmerge\b/i], ['發佈', '發布'], ['公佈', '公布'], + ['宣佈', '宣布'], ['控制檯', '控制台'], + // 個 after a count ({n} 个中, of {n}), not the literary 箇中. + ['箇中', '個中'], // 周 as a week: the time-lapse speed in weeks per second. [' 周/秒', ' 週/秒'], ]; let s2tw; -/** zh-CN text in Traditional characters, as zh-Hant shows it. */ -export function toZhHant(text) { - // Loaded on first use: extracting messages needs no converter, and a copy - // of this script without node_modules (stale-index.test.mjs) still runs. - s2tw ??= createRequire(import.meta.url)('opencc-js/cn2t').Converter({ - from: 'cn', - to: 'tw', - }); +// Loaded on first use, so extracting messages needs no node_modules +// (stale-index.test.mjs); main reports a missing package as a zh-Hant error. +function converter() { + try { + s2tw ??= createRequire(import.meta.url)('opencc-js/cn2t').Converter({ + from: 'cn', + to: 'tw', + }); + } catch (error) { + if (error.code !== 'MODULE_NOT_FOUND') throw error; + throw new Error('opencc-js is not installed: run npm ci', { cause: error }); + } + return s2tw; +} + +export function toZhHant(text, key = '') { return ZH_HANT_CORRECTIONS.reduce( - (out, [wrong, right]) => out.replaceAll(wrong, right), - s2tw(text), + (out, [wrong, right, keys]) => + !keys || keys.test(key) ? out.replaceAll(wrong, right) : out, + converter()(text), ); } /** The zh-Hant catalog for a zh-CN catalog: the same keys in the same order. */ export function zhHantCatalog(zhCN) { - const convert = (entry) => { - if (typeof entry === 'string') return toZhHant(entry); + const convert = (entry, text) => { + if (typeof entry === 'string') return toZhHant(entry, text); if (entry && typeof entry === 'object') return Object.fromEntries( Object.entries(entry).map(([category, form]) => [ category, - convert(form), + convert(form, text), ]), ); return entry; }; return Object.fromEntries( - Object.entries(zhCN).map(([text, entry]) => [text, convert(entry)]), + Object.entries(zhCN).map(([text, entry]) => [text, convert(entry, text)]), ); } /** * Where a zh-Hant catalog is not the conversion of the zh-CN one. + * @param {Set} [reported] Keys already reported missing from zh-Hant. * @returns {string[]} One error per entry, naming its key. */ -export function checkZhHant(zhCN, zhHant) { +export function checkZhHant(zhCN, zhHant, reported = new Set()) { const expected = zhHantCatalog(zhCN); const fix = `zh-Hant is generated: edit ${ZH_HANT_SOURCE}, then run ${ZH_HANT_COMMAND}`; const errors = []; for (const [text, entry] of Object.entries(expected)) { - if (!Object.hasOwn(zhHant, text)) - errors.push(`"${text}" is in ${ZH_HANT_SOURCE} but not here. ${fix}`); - else if (!isDeepStrictEqual(zhHant[text], entry)) + if (!Object.hasOwn(zhHant, text)) { + if (!reported.has(text)) + errors.push(`"${text}" is in ${ZH_HANT_SOURCE} but not here. ${fix}`); + } else if (!isDeepStrictEqual(zhHant[text], entry)) errors.push( `"${text}" is ${JSON.stringify(zhHant[text])}, not ${ZH_HANT_SOURCE} converted (${JSON.stringify(entry)}). ${fix}`, ); @@ -697,9 +715,9 @@ export function checkZhHant(zhCN, zhHant) { if (!Object.hasOwn(expected, text)) errors.push(`"${text}" is not in ${ZH_HANT_SOURCE}. ${fix}`); } - if (!errors.length) { - const order = Object.keys(expected); - const keys = Object.keys(zhHant); + const order = Object.keys(expected); + const keys = Object.keys(zhHant); + if (!errors.length && keys.length === order.length) { const at = order.findIndex((text, i) => keys[i] !== text); if (at >= 0) errors.push( @@ -711,7 +729,6 @@ export function checkZhHant(zhCN, zhHant) { const serialize = (catalog) => `${JSON.stringify(catalog, null, 2)}\n`; -/** Rewrite zh-Hant.json as the conversion of zh-CN.json. */ export function writeZhHant() { const catalog = zhHantCatalog(readCatalog(ZH_HANT_SOURCE)); writeFileSync(catalogPath(ZH_HANT), serialize(catalog)); @@ -758,14 +775,22 @@ function main(args) { for (const text of stale) console.log(` ${JSON.stringify(text)}`); } // shippedLocales() is sorted, so --prune has already pruned zh-CN. - if (locale === ZH_HANT) - errors.push(...checkZhHant(readCatalog(ZH_HANT_SOURCE), catalog)); + const zhCN = locale === ZH_HANT ? readCatalog(ZH_HANT_SOURCE) : null; + if (zhCN) { + try { + errors.push(...checkZhHant(zhCN, catalog, new Set(missing))); + } catch (error) { + if (error.cause?.code !== 'MODULE_NOT_FOUND') throw error; + errors.push(error.message); + } + } if (missing.length) { failed = true; - const fill = - locale === ZH_HANT - ? `add them to ${ZH_HANT_SOURCE}, then ${ZH_HANT_COMMAND}` - : `npm run i18n -- --missing ${locale}`; + const fill = !zhCN + ? `npm run i18n -- --missing ${locale}` + : missing.every((text) => Object.hasOwn(zhCN, text)) + ? `run ${ZH_HANT_COMMAND}` + : `add them to ${ZH_HANT_SOURCE}, then ${ZH_HANT_COMMAND}`; console.log(`${locale}: ${missing.length} missing (${fill}):`); for (const text of missing) console.log(` ${JSON.stringify(text)} ${messages.get(text).where}`); diff --git a/src/i18n/catalog.test.mjs b/src/i18n/catalog.test.mjs index 010e533cdf..1b35c41417 100644 --- a/src/i18n/catalog.test.mjs +++ b/src/i18n/catalog.test.mjs @@ -54,12 +54,27 @@ test('toZhHant converts characters, not vocabulary, and fixes what s2tw picks wr assert.equal(toZhHant('50 年历史'), '50 年歷史'); assert.equal(toZhHant('发布于 {date}'), '發布於 {date}'); assert.equal(toZhHant('1 周/秒'), '1 週/秒'); + assert.equal(toZhHant('已宣布紧急情况'), '已宣布緊急情況'); + assert.equal(toZhHant('全部 {n} 个中'), '全部 {n} 個中'); + // 併道 only where the English key is about merging. + assert.equal(toZhHant('前方并道路段', 'Merge ahead'), '前方併道路段'); assert.equal( - toZhHant('{direction}并道,进入 {road}'), - '{direction}併道,進入 {road}', + toZhHant('事故并道路封闭', 'Crash, road closed'), + '事故並道路封閉', + ); + assert.equal(toZhHant('挥手并道谢', 'Waved and thanked'), '揮手並道謝'); + assert.deepEqual( + zhHantCatalog({ + 'Merge {direction} onto {road}': '{direction}并道,进入 {road}', + 'maneuver|Merge': '并道', + 'Closed and apologized': '已关闭并道歉', + }), + { + 'Merge {direction} onto {road}': '{direction}併道,進入 {road}', + 'maneuver|Merge': '併道', + 'Closed and apologized': '已關閉並道歉', + }, ); - assert.equal(toZhHant('事故并道路封闭'), '事故並道路封閉'); - assert.equal(toZhHant('已关闭并道歉'), '已關閉並道歉'); }); test('checkZhHant names each entry that is not the conversion of zh-CN', () => { @@ -96,6 +111,21 @@ test('checkZhHant names each entry that is not the conversion of zh-CN', () => { assert.deepEqual(checkZhHant(zhCN, { Server, ...withoutServer }), [ 'entries leave the zh-CN order at "Video": run npm run i18n -- --zh-hant', ]); + // A key checkCatalog already lists as missing is not reported twice. + assert.deepEqual(checkZhHant(zhCN, withoutServer, new Set(['Server'])), []); +}); + +test('a zh-Hant entry still in English points to zh-CN and its same-as-English line', () => { + const fixture = new Map([['Bloom', { plural: false }]]); + const errors = (locale) => + checkCatalog(locale, { Bloom: 'Bloom' }, fixture, new Set()).errors; + assert.deepEqual(errors('zh-Hant'), [ + '"Bloom" is still English: translate it in zh-CN and run npm run i18n -- --zh-hant, or list it under "zh-Hant" in src/i18n/same-as-english.json as under "zh-CN"', + ]); + assert.match( + errors('zh-CN')[0], + /^"Bloom" is still English: translate it, or/, + ); }); test('every UI string is marked for translation or kept on purpose (npm run i18n)', () => { diff --git a/src/i18n/zh-hant-cli.test.mjs b/src/i18n/zh-hant-cli.test.mjs new file mode 100644 index 0000000000..47b7e62664 --- /dev/null +++ b/src/i18n/zh-hant-cli.test.mjs @@ -0,0 +1,145 @@ +// The zh-Hant branches of `npm run i18n` (main in scripts/i18n.mjs), run in a +// throwaway repository like stale-index.test.mjs. +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { execFileSync, spawnSync } from 'node:child_process'; +import { + copyFileSync, + mkdirSync, + mkdtempSync, + readFileSync, + rmSync, + symlinkSync, + writeFileSync, +} from 'node:fs'; +import { createRequire } from 'node:module'; +import { tmpdir } from 'node:os'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const REPO = path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + '../..', +); +const json = (catalog) => `${JSON.stringify(catalog, null, 2)}\n`; + +function sandbox(zhCN, { modules = true } = {}) { + const root = mkdtempSync(path.join(tmpdir(), 'zh-hant-cli-')); + const write = (file, text) => { + mkdirSync(path.dirname(path.join(root, file)), { recursive: true }); + writeFileSync(path.join(root, file), text); + }; + mkdirSync(path.join(root, 'scripts')); + copyFileSync( + path.join(REPO, 'scripts/i18n.mjs'), + path.join(root, 'scripts/i18n.mjs'), + ); + if (modules) { + // The package wherever node finds it, from its dist/umd entry; a junction + // needs no symlink privilege on Windows. + const entry = createRequire(import.meta.url).resolve('opencc-js/cn2t'); + mkdirSync(path.join(root, 'node_modules')); + symlinkSync( + path.resolve(path.dirname(entry), '../..'), + path.join(root, 'node_modules/opencc-js'), + 'junction', + ); + } + write('.gitignore', 'node_modules\n'); + write('src/a.js', "export const a = [t('Video'), t('Server')];\n"); + write('src/i18n/same-as-english.json', '{}\n'); + write('src/i18n/locales/zh-CN.json', json(zhCN)); + const git = (...args) => + execFileSync('git', args, { cwd: root, encoding: 'utf8' }); + git('init', '-q'); + return { + git, + write, + read: (file) => readFileSync(path.join(root, file), 'utf8'), + run: (...args) => + spawnSync(process.execPath, ['scripts/i18n.mjs', ...args], { + cwd: root, + encoding: 'utf8', + }), + done: () => rmSync(root, { recursive: true, force: true }), + }; +} + +const ZH_CN = { Video: '视频', Server: '服务器' }; +const ZH_HANT = { Video: '視頻', Server: '服務器' }; + +test('--missing zh-Hant points to --zh-hant instead of printing a template', () => { + const box = sandbox(ZH_CN); + try { + for (const locale of ['zh-Hant', 'zh-hant']) { + const result = box.run('--missing', locale); + assert.equal(result.status, 1); + assert.equal(result.stdout, ''); + assert.match(result.stderr, /then run npm run i18n -- --zh-hant/); + } + } finally { + box.done(); + } +}); + +test('--zh-hant writes zh-CN converted, in its key order, indent 2 and a final newline', () => { + const box = sandbox(ZH_CN); + try { + const result = box.run('--zh-hant'); + assert.equal(result.status, 0, result.stdout + result.stderr); + assert.equal(box.read('src/i18n/locales/zh-Hant.json'), json(ZH_HANT)); + assert.match(result.stdout, /zh-Hant: 2 messages, complete/); + } finally { + box.done(); + } +}); + +test('--prune drops a stale entry from both catalogs, whichever git lists first', () => { + const box = sandbox({ ...ZH_CN, Old: '旧' }); + try { + // zh-CN tracked and zh-Hant not: git ls-files lists zh-Hant first. + box.git('add', '-A'); + box.run('--zh-hant'); + const result = box.run('--prune'); + assert.equal(result.status, 0, result.stdout + result.stderr); + assert.match(result.stdout, /zh-CN: pruned 1 unused entries/); + assert.match(result.stdout, /zh-Hant: pruned 1 unused entries/); + assert.equal(box.read('src/i18n/locales/zh-Hant.json'), json(ZH_HANT)); + } finally { + box.done(); + } +}); + +test('a missing zh-Hant entry says to add it to zh-CN only when zh-CN lacks it too', () => { + for (const [zhCN, fill] of [ + [ZH_CN, 'run npm run i18n -- --zh-hant'], + [{ Video: '视频' }, 'add them to zh-CN, then npm run i18n -- --zh-hant'], + ]) { + const box = sandbox(zhCN); + try { + box.write('src/i18n/locales/zh-Hant.json', json({ Video: '視頻' })); + const result = box.run(); + assert.equal(result.status, 1); + assert.ok( + result.stdout.includes(`zh-Hant: 1 missing (${fill}):`), + result.stdout, + ); + } finally { + box.done(); + } + } +}); + +test('without node_modules the zh-Hant check says to install and the other checks still run', () => { + const box = sandbox(ZH_CN, { modules: false }); + try { + box.write('src/i18n/locales/zh-Hant.json', json(ZH_HANT)); + box.write('index.html', '

Unmarked words

\n'); + const result = box.run(); + assert.equal(result.status, 1); + assert.match(result.stdout, /zh-Hant: .*run npm ci/); + assert.match(result.stdout, /1 UI strings not marked for translation/); + } finally { + box.done(); + } +});