const fs=require('fs'),vm=require('vm'),path=require('path'); const root=path.resolve(__dirname,'..'); const p=path.join(root,'src/app.js'); let s=fs.readFileSync(p,'utf8'); const ko={}; for(const line of fs.readFileSync(path.join(root,'src/ko.tsv'),'utf8').trim().split(/\r?\n/)){ const at=line.indexOf('\t');if(at<1)throw Error('Invalid translation'); const k=line.slice(0,at);if(k in ko)throw Error('Duplicate '+k); ko[k]=line.slice(at+1).replace(/\\n/g,'\n'); } const T=vm.runInNewContext(s.slice(s.indexOf('const T = {'),s.indexOf('\nlet lang'))+';T'); for(const [k,v] of Object.entries({...T.de,...T.en})){ if(!(k in ko))throw Error('Missing '+k); const vars=x=>(x.match(/\{\w+\}/g)||[]).sort().join('|'); if(vars(v)!==vars(ko[k]))throw Error('Placeholders: '+k); } s=s.replace(/\/\* ITOSM_KO_BEGIN \*\/[\s\S]*?\/\* ITOSM_KO_END \*\/\n?/,''); s=s.replace("let lang = 'de';",'/* ITOSM_KO_BEGIN */\nT.ko = '+JSON.stringify(ko,null,2)+';\nT.en.menuKo="Korean A+C device menu";\nT.en.menuKoHint="Experimental Korean device menu: PC preview and build checks only; hardware, save and restore are not yet verified. Back up your ROM and saves before applying. Original game, emulator settings and names read from your ROM remain unchanged.";\nT.de.menuKo=T.en.menuKo;T.de.menuKoHint=T.en.menuKoHint;\n/* ITOSM_KO_END */\nlet lang = \'de\';'); s=s.replace("localStorage.getItem('paramix-lang') || (navigator.language || 'de').slice(0, 2)","localStorage.getItem('paramix-lang') || 'ko'"); if(!s.includes("if (lang === 'ko')"))s=s.replace("if (lang === 'en') return s;","if (lang === 'en') return s;\n if (lang === 'ko') { for (const [re, ko] of MSG_KO) if(re.test(s)) return s.replace(re,ko); return s; }"); // Korean module errors are separate from the upstream German dictionary. if(!s.includes('const MSG_KO ='))s=s.replace('const MSG_DE = [',fs.readFileSync(path.join(root,'src/errors-ko.js'),'utf8')+'\nconst MSG_DE = ['); fs.writeFileSync(p,s); const ep=path.join(root,'src/emu.js'); const emu=fs.readFileSync(ep,'utf8'); fs.writeFileSync(ep,emu.replaceAll("opts.lang === 'en' ? 1 : 0", "(opts.lang === 'en' || opts.lang === 'ko') ? 1 : 0")); console.log('Korean translations:',Object.keys(ko).length,'; placeholders checked');