i18n: tighten the zh-Hant check and test its command line

- Without opencc-js the zh-Hant check says to run npm ci, and the other
  checks still run.
- --prune handles zh-CN before zh-Hant: shippedLocales() sorts, since
  git lists untracked catalogs first.
- A key missing from zh-Hant is reported once, with "run npm run i18n --
  --zh-hant" when zh-CN has it; a zh-Hant entry still in English points
  to zh-CN and same-as-english.json.
- 並道 -> 併道 applies only to entries whose English key says merge, so
  并 (and) before 道路 or 道謝 stays 並.
- Two more corrections for strings main has added: 箇中 -> 個中 after a
  count (全部 {n} 個中), and 宣佈 -> 宣布 like 發佈 and 公佈.
- zh-hant-cli.test.mjs runs the command in a throwaway repository, with
  opencc-js linked as a junction, so Windows needs no symlink privilege.

zh-Hant.json does not change.
This commit is contained in:
German Khokhlov committed 2026-11-17 19:44:49 +04:00
1 parent 3822ee9f00
commit b63205f69c
4 files changed
+239 -39

No files matched your search

+34 -4
View File
@@ -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)', () => {
+145
View File
@@ -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', '<p>Unmarked words</p>\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();
}
});