#!/usr/bin/env node /** * verify-i18n.mjs — static checks for the documentation site's zh-CN/en switch. * No runtime dependencies; exits non-zero when a source key is missing or a * module-level translated value can go stale after a language change. */ import { readFileSync, existsSync } from 'node:fs'; import { join } from 'node:path'; const ROOT = join(import.meta.dirname, '..'); const appPath = join(ROOT, 'site', 'app.js'); const i18nPath = join(ROOT, 'site', 'i18n.js'); const indexPath = join(ROOT, 'site', 'index.html'); const app = readFileSync(appPath, 'utf8'); const i18n = readFileSync(i18nPath, 'utf8'); const index = readFileSync(indexPath, 'utf8'); const failures = []; const checks = []; function check(name, ok, detail = '') { checks.push({ name, ok, detail }); if (!ok) failures.push(`${name}${detail ? `: ${detail}` : ''}`); } check('dictionary exists', /var\s+EN\s*=\s*\{/.test(i18n)); check('language storage key', /LANG_KEY\s*=\s*['"]kole-lang['"]/.test(i18n)); check('language values', /saved\s*===\s*['"]en['"]/.test(i18n) && /saved\s*===\s*['"]zh['"]/.test(i18n)); check('HTML lang sync', /setAttribute\(['"]lang['"],\s*lang\s*===\s*['"]en['"]\s*\?\s*['"]en['"]\s*:\s*['"]zh-CN['"]/.test(i18n)); check('language select markup', /id="lang-trigger"/.test(index) && /id="lang-menu"/.test(index) && /role="listbox"/.test(index) && /class="lang-opt"[^>]*data-lang="zh"/.test(index) && /class="lang-opt"[^>]*data-lang="en"/.test(index)); check('language select binding', /function\s+bindLangSelect\s*\(/.test(app) && /function\s+applyLang\(next\)[\s\S]*?I18N\.setLang\(next\)/.test(app) && /applyLang\(opt\.getAttribute\(['"]data-lang['"]\)\)/.test(app)); check('no legacy lang toggle left', !/lang-toggle/.test(index) && !/bindLangToggle/.test(app) && !/\.lang-zh\b/.test(index)); check('English document title', /Kole UI · Component Library/.test(app)); check('source-key navigation map', /var\s+NAV_KEY\s*=\s*\{[^}]*home:\s*['"]首页['"]/.test(app)); check('search stores category key', /return\s*\{\s*c:\s*c,[\s\S]*?category:\s*c\.category\s*\}/.test(app)); check('related translations evaluated at render', /why:\s*T\(rule\.why\)/.test(app)); check('theme labels evaluated at render', /function\s+themeMeta\s*\(\)[\s\S]*?T\(m\[1\]\)/.test(app)); check('theme presets evaluated at render', /function\s+themePresets\s*\(\)[\s\S]*?T\(p\.label\)/.test(app)); const relatedDecl = app.match(/var\s+RELATED_MAP\s*=\s*\[[\s\S]*?\n\s*\];/); const themeDecl = app.match(/var\s+THEME_META_KEYS\s*=\s*\[[\s\S]*?\n\s*\];/); check('no stale module-level translated caches', !(relatedDecl && /\bT\s*\(/.test(relatedDecl[0])) && !(themeDecl && /\bT\s*\(/.test(themeDecl[0]))); const dictBlock = i18n.match(/var\s+EN\s*=\s*\{([\s\S]*?)\n\s*\};/); const dictionaryKeys = new Set(); if (dictBlock) { for (const match of dictBlock[1].matchAll(/^\s*(['"])((?:\\.|(?!\1)[\s\S])*?)\1\s*:/gm)) { dictionaryKeys.add(match[2].replace(/\\(['"])/g, '$1')); } } const sourceKeys = new Set(); for (const match of app.matchAll(/\bT\(\s*(['"])((?:\\.|(?!\1)[\s\S])*?)\1\s*\)/g)) { sourceKeys.add(match[2].replace(/\\(['"])/g, '$1')); } const missing = [...sourceKeys].filter((key) => !dictionaryKeys.has(key)); check('literal T() coverage', missing.length === 0, missing.length ? missing.join(' | ') : `${sourceKeys.size} keys covered`); const detailDir = join(ROOT, 'site', 'details'); check('detail payload directory', existsSync(detailDir)); for (const result of checks) { console.log(`[i18n] ${result.ok ? 'OK' : 'FAIL'} ${result.name}${result.detail ? ` — ${result.detail}` : ''}`); } if (failures.length) { console.error(`\n[i18n] ${failures.length} check(s) failed`); process.exit(1); } console.log(`\n[i18n] OK — ${checks.length} checks passed`);