File size: 1,469 Bytes
246d201 |
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 |
const fs = require("fs");
const path = require("path");
const i18n = require("../src/i18n/translation.json");
// { [lang]: { [key]: content } }
const translationMap = {};
Object.entries(i18n).forEach(([key, transMap]) => {
Object.entries(transMap).forEach(([lang, content]) => {
if (!translationMap[lang]) {
translationMap[lang] = {};
}
translationMap[lang][key] = content;
})
});
// remove old locales directory
const localesPath = path.join(__dirname, "../public/locales");
if (fs.existsSync(localesPath)) {
fs.rmSync(localesPath, { recursive: true });
}
// write translation files
Object.entries(translationMap).forEach(([lang, transMap]) => {
const filePath = path.join(__dirname, `../public/locales/${lang}/translation.json`);
if (!fs.existsSync(filePath)) {
fs.mkdirSync(path.dirname(filePath), { recursive: true });
}
fs.writeFileSync(filePath, JSON.stringify(transMap, null, 2));
});
// write translation key enum
const transKeys = Object.keys(translationMap.en);
const transKeyDeclareFilePath = path.join(__dirname, "../src/i18n/declaration.ts");
if (!fs.existsSync(transKeyDeclareFilePath)) {
fs.mkdirSync(path.dirname(transKeyDeclareFilePath), { recursive: true });
}
fs.writeFileSync(transKeyDeclareFilePath, `
// this file generate by script, don't modify it manually!!!
export enum I18nKey {
${transKeys.map(key => ` ${key} = "${key}",`).join('\n')}
}`.trim() + '\n');
|