I run a practice-test site for the California DMV written test. It has English, Simplified Chinese and Traditional Chinese versions, and the Traditional pages are produced from the Simplified content at render time by one function, toTraditional().
It started as a hand-written character map. That kept missing characters: a post would go live on the Traditional side with a few Simplified characters left in the middle of a sentence. This is how I replaced it with a generated table and kept the hand-written part only for the cases a table can't handle.
Step 1: generate a one-to-one map from ICU
macOS ships ICU, and ICU has a Hans-Hant transform. You can reach it from JavaScript for Automation, so a Node script can call osascript and get the mapping without downloading anything.
The script walks the CJK Unified Ideographs block (U+4E00 to U+9FFF) one character at a time and keeps only the characters where the transform gives back a different, single character:
const jxa = `ObjC.import("Foundation"); var out = []; for (var c = 0x4E00; c <= 0x9FFF; c++) { var ch = String.fromCharCode(c); var t = $.NSString.alloc.initWithUTF8String(ch) .stringByApplyingTransformReverse("Hans-Hant", false).js; if (t !== ch && t.length === 1) out.push(ch + t); } out.join("")`; const pairs = execFileSync('osascript', ['-l', 'JavaScript', '-e', jxa], { encoding: 'utf8', maxBuffer: 1 << 24, }).trim(); The output goes into a generated file as two strings, where S2T_FROM[i] converts to S2T_TO[i]. In my case that's 2,544 characters. The script runs on macOS only, but the file it writes is plain JS and works anywhere.
Step 2: leave out the characters with more than one Traditional form
Converting one character at a time is wrong for some characters, because a single Simplified character can stand for several Traditional ones and ICU only returns one. These are the ones that hit my content:
- 干 can be 乾 (dry), 幹 (trunk, as in 題幹 "question stem") or stay 干 (干擾 "interfere")
- 发 is 發 in most words but 髮 in hair words (頭髮, 理髮)
- 里 is 裡 for "inside" but stays 里 in 里程 (mileage)
- 余, 台, 只, 面, 系, 制, 松, 卷, 斗, 丑, 脏, 舍 and 沉 have the same problem
So the generator skips them:
const EXCLUDE = new Set([...'干舍沉斗丑脏里余制台只面卷松系']); It also swaps a couple of ICU's choices for the forms Taiwanese readers expect (鉴 to 鑑, 启 to 啟).
Step 3: phrases first, longest match wins
The excluded characters get converted inside known phrases instead. The phrase table is small and hand-written: 头发 → 頭髮, 干净 → 乾淨, 题干 → 題幹, 干扰 → 干擾, 余光 → 餘光, 宿舍 → 宿舍, and so on.
Phrases are grouped by their first character and sorted longest first. The converter makes a single pass: at each position it tries the phrases that start with the current character, and only falls back to the character map when none match.
const phrasesByFirst = new Map(); Object.entries(phraseMap) .sort(([a], [b]) => b.length - a.length) .forEach(([source, target]) => { if (!phrasesByFirst.has(source[0])) phrasesByFirst.set(source[0], []); phrasesByFirst.get(source[0]).push([source, target]); }); export const toTraditional = (text = '') => { const input = String(text || ''); let out = ''; for (let i = 0; i < input.length;) { const match = phrasesByFirst.get(input[i]) ?.find(([source]) => input.startsWith(source, i)); if (match) { out += match[1]; i += match[0].length; } else { out += charMap.get(input[i]) || input[i]; i += 1; } } return out; }; Because it's one pass, text that a phrase produced is never converted again. My earlier version ran phrase replacements and then character replacements over the whole string, and the second step could undo the first.
The character map is built from the generated strings, then the few hand-written entries are applied on top, so a manual fix always beats the generated table.
Step 4: lock it down with tests
Sentences that came out wrong on the live site went into tests with node:test:
test('keeps words whose character has several Traditional forms', () => { assert.equal( toTraditional('宿舍地址、题干里写着、主干道、干扰、沉默、里程'), '宿舍地址、題幹裡寫著、主幹道、干擾、沉默、里程', ); assert.equal(toTraditional('理发、头发、出发前'), '理髮、頭髮、出發前'); assert.equal(toTraditional('道路干燥,眼角余光'), '道路乾燥,眼角餘光'); }); Trade-offs
This is not a full converter. It only switches regional vocabulary for the phrases I've added (打印 becomes 列印, but 软件 stays 軟件 instead of 軟體 until I add it), and any new word built on an excluded character needs a phrase entry. For a site whose content I write myself, that's manageable: the phrase list is about a hundred entries, and the tests catch regressions.
If you want to see the output, the Traditional Chinese version of the site is here: https://www.jiazhoujiazhao.com/zh-hant

