File size: 3,654 Bytes
7b07ba9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | 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();
|