import { readdir } from 'node:fs/promises'; import { join, relative } from 'node:path'; import { fileURLToPath } from 'node:url'; import { readGameText } from './eu4_clausewitz/src/codec.mjs'; import { loadManifest, parsedText } from './history_language_manifest.mjs'; export const CJK = /[\u3400-\u9fff\uf900-\ufaff]/u; async function walkTxtFiles(root, current = root) { const files = []; for (const entry of await readdir(current, { withFileTypes: true })) { const path = join(current, entry.name); if (entry.isDirectory()) files.push(...await walkTxtFiles(root, path)); else if (entry.isFile() && entry.name.endsWith('.txt')) files.push(relative(root, path).replaceAll('\\', '/')); } return files; } function dynastyTokens(items) { const tokens = []; for (const item of items) { if (item.kind !== 'pair') continue; const key = item.keyToken.value; if (key === 'dynasty_names' && item.value.type === 'block') { for (const value of item.value.items) { if (value.kind === 'bare') tokens.push(value.token); } continue; } if ((key === 'dynasty' || key === 'heir_dynasty') && item.value.type === 'scalar' && item.value.token) { tokens.push(item.value.token); } if (item.value.type === 'block') tokens.push(...dynastyTokens(item.value.items)); } return tokens; } function isDynastyManifestEntry(entry) { return entry.selector.blocks.some((block) => block.key === 'dynasty_names') || (entry.selector.target.kind === 'pair' && ['dynasty', 'heir_dynasty'].includes(entry.selector.target.key)); } export async function auditDynastyLanguage(root, manifest, translations = {}) { const findings = []; const files = []; for (const scope of ['history', 'common']) { for (const path of await walkTxtFiles(join(root, scope))) files.push(`${scope}/${path}`); } for (const path of files) { const { text } = await readGameText(join(root, path)); const { tree } = parsedText(text); for (const token of dynastyTokens(tree.items)) { if (!CJK.test(token.value)) findings.push({ id: 'dynasty-not-chinese', path, line: token.line, value: token.value }); } } const byEnglish = new Map(); for (const file of manifest.files) { for (const entry of file.entries) { if (!isDynastyManifestEntry(entry) || !translations[entry.english]) continue; const chinese = byEnglish.get(entry.english) ?? new Set(); chinese.add(entry.chinese); byEnglish.set(entry.english, chinese); } } for (const [english, chinese] of byEnglish) { if (chinese.size > 1) findings.push({ id: 'dynasty-inconsistent-translation', english, chinese: [...chinese].sort() }); } return findings; } function printFindings(findings) { for (const finding of findings) { if (finding.id === 'dynasty-not-chinese') { console.log(`${finding.path}:${finding.line}: ${finding.id}: ${finding.value}`); } else { console.log(`${finding.id}: ${finding.english} -> ${finding.chinese.join(' / ')}`); } } } async function main() { const root = join(fileURLToPath(new URL('..', import.meta.url))); const [manifest, translationMap] = await Promise.all([ loadManifest(join(root, 'history_language_manifest.json')), import('./dynasty_translation_map.json', { with: { type: 'json' } }), ]); const findings = await auditDynastyLanguage(root, manifest, translationMap.default.translations); printFindings(findings); console.log(`Dynasty language audit: ${findings.length} findings.`); process.exitCode = findings.length > 0 ? 1 : 0; } if (process.argv[1] === fileURLToPath(import.meta.url)) await main();