50 lines
1.9 KiB
JavaScript
50 lines
1.9 KiB
JavaScript
/**
|
|
* scripts/extract-i18n-domain.mjs
|
|
* 从 i18n.ts 内联 messages 提取指定域至 locales/{locale}/{domain}.json 并移除内联键。
|
|
*
|
|
* Usage: node scripts/extract-i18n-domain.mjs <domain>
|
|
* Example: node scripts/extract-i18n-domain.mjs workspace
|
|
*/
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
const domain = process.argv[2];
|
|
if (!domain) {
|
|
console.error('Usage: node scripts/extract-i18n-domain.mjs <domain>');
|
|
process.exit(1);
|
|
}
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
const root = path.resolve(__dirname, '..');
|
|
const i18nPath = path.resolve(root, 'src/i18n.ts');
|
|
const localesRoot = path.resolve(root, 'src/locales');
|
|
const locales = ['en', 'zh-CN', 'zh-TW', '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 locale of locales) {
|
|
const block = messages[locale]?.[domain];
|
|
if (!block) {
|
|
console.error(`Missing messages.${locale}.${domain}`);
|
|
process.exit(1);
|
|
}
|
|
const outDir = path.join(localesRoot, locale);
|
|
fs.mkdirSync(outDir, { recursive: true });
|
|
const outPath = path.join(outDir, `${domain}.json`);
|
|
fs.writeFileSync(outPath, `${JSON.stringify(block, null, 2)}\n`, 'utf8');
|
|
delete messages[locale][domain];
|
|
console.log(`Wrote ${path.relative(root, 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 ${domain} from i18n.ts`);
|