make-i18n-translations.cjs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. const fs = require("fs");
  2. const path = require("path");
  3. const i18n = require("../src/i18n/translation.json");
  4. // { [lang]: { [key]: content } }
  5. const translationMap = {};
  6. Object.entries(i18n).forEach(([key, transMap]) => {
  7. Object.entries(transMap).forEach(([lang, content]) => {
  8. if (!translationMap[lang]) {
  9. translationMap[lang] = {};
  10. }
  11. translationMap[lang][key] = content;
  12. })
  13. });
  14. // remove old locales directory
  15. const localesPath = path.join(__dirname, "../public/locales");
  16. if (fs.existsSync(localesPath)) {
  17. fs.rmSync(localesPath, { recursive: true });
  18. }
  19. // write translation files
  20. Object.entries(translationMap).forEach(([lang, transMap]) => {
  21. const filePath = path.join(__dirname, `../public/locales/${lang}/translation.json`);
  22. if (!fs.existsSync(filePath)) {
  23. fs.mkdirSync(path.dirname(filePath), { recursive: true });
  24. }
  25. fs.writeFileSync(filePath, JSON.stringify(transMap, null, 2));
  26. });
  27. // write translation key enum
  28. const transKeys = Object.keys(translationMap.en);
  29. const transKeyDeclareFilePath = path.join(__dirname, "../src/i18n/declaration.ts");
  30. if (!fs.existsSync(transKeyDeclareFilePath)) {
  31. fs.mkdirSync(path.dirname(transKeyDeclareFilePath), { recursive: true });
  32. }
  33. fs.writeFileSync(transKeyDeclareFilePath, `
  34. // this file generate by script, don't modify it manually!!!
  35. export enum I18nKey {
  36. ${transKeys.map(key => ` ${key} = "${key}",`).join('\n')}
  37. }`.trim() + '\n');