/** * scripts/extract-i18n-kb.mjs * 将 i18n.ts 内联的 kb 相关文案外置至 locales 下 kb.json,并从 i18n.ts 移除。 */ import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const i18nPath = path.resolve(__dirname, '../src/i18n.ts'); const localesDir = path.resolve(__dirname, '../src/locales'); const KB_KEYS = ['kbDifySettings', 'kbDifyHealth', 'kbAdmin', 'kbWorkspace']; const LOCALE_DIRS = { en: 'en', 'zh-CN': 'zh-CN', 'zh-TW': 'zh-TW', th: 'th' }; const content = fs.readFileSync(i18nPath, 'utf8'); const start = content.indexOf('const messages = {') + 'const messages = '.length; const end = content.indexOf('const i18n = createI18n'); const messagesStr = content.slice(start, end).trim().replace(/,\s*$/, ''); const messages = new Function(`return ${messagesStr}`)(); for (const [loc, dir] of Object.entries(LOCALE_DIRS)) { const kbPayload = {}; for (const key of KB_KEYS) { if (messages[loc]?.[key]) kbPayload[key] = messages[loc][key]; delete messages[loc][key]; } const outPath = path.join(localesDir, dir, 'kb.json'); fs.writeFileSync(outPath, `${JSON.stringify(kbPayload, null, 2)}\n`, 'utf8'); console.log('Wrote', outPath); } const header = content.slice(0, content.indexOf('const messages = {')); const footer = content.slice(end); const serialized = JSON.stringify(messages, null, 2); const newContent = `${header}const messages = ${serialized};\n\n${footer}`; fs.writeFileSync(i18nPath, newContent, 'utf8'); console.log('Stripped kb keys from i18n.ts');