collect-chinese.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // collect-chinese.js
  2. const glob = require('glob');
  3. const fs = require('fs');
  4. const path = require('path');
  5. const projectPath = './sheep'; // 修改为你的项目路径
  6. const outputPath = './zh-cn-sheep-config.json'; // 配置表输出路径
  7. const chineseRegex = /['"`]([^'"`\n]*[\u4e00-\u9fa5]+[^'"`\n]*)['"`]/g;
  8. let configObject = {};
  9. function collectChinese() {
  10. try {
  11. const files = glob.sync(`${projectPath}/**/*.{js,vue,ts,html}`);
  12. files.forEach((file) => {
  13. const content = fs.readFileSync(file, 'utf8');
  14. let match;
  15. while ((match = chineseRegex.exec(content)) !== null) {
  16. const chineseText = match[1];
  17. if (!configObject[chineseText]) {
  18. configObject[chineseText] = '';
  19. }
  20. }
  21. });
  22. saveConfigFile();
  23. } catch (err) {
  24. console.error('Error while scanning files:', err);
  25. }
  26. }
  27. function saveConfigFile() {
  28. const sortedConfig = Object.keys(configObject).sort().reduce((obj, key) => {
  29. obj[key] = configObject[key];
  30. return obj;
  31. }, {});
  32. fs.writeFileSync(outputPath, JSON.stringify(sortedConfig, null, 2));
  33. console.log(`Config file saved to ${outputPath}`);
  34. }
  35. collectChinese();