i18n_scanner.cjs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. var fs = require('fs');
  2. var path = require('path');
  3. var Parser = require('i18next-scanner').Parser;
  4. const zhExclude = require('./i18n_exclude_zh.json');
  5. const enExclude = require('./i18n_exclude_en.json');
  6. const ruExclude = require('./i18n_exclude_ru.json');
  7. const esExclude = require('./i18n_exclude_es.json');
  8. const existedEn = require('../src/i18n/en.json');
  9. const existedRu = require('../src/i18n/ru.json');
  10. const existedEs = require('../src/i18n/es.json');
  11. var directoryPaths = ['src/pages', 'src/components'];
  12. var existedEnTranslation = existedEn.translation;
  13. var existedRuTranslation = existedRu.translation;
  14. var existedEsTranslation = existedEs.translation;
  15. var zhHandler = function (key, parser) {
  16. // 读取特殊规则,如果有特殊规则,就从特殊规则取值
  17. if (zhExclude[key]) {
  18. parser.set(key, zhExclude[key]);
  19. return;
  20. }
  21. parser.set(key, key);
  22. };
  23. var otherLanguageHandler = function (key, parser, excludeValue, existedValue) {
  24. // 读取特殊规则,如果有特殊规则,就从特殊规则取值
  25. if (excludeValue[key]) {
  26. parser.set(key, excludeValue[key]);
  27. return;
  28. }
  29. // 如果已经有翻译,就从已有翻译取值
  30. if (existedValue[key]) {
  31. parser.set(key, existedValue[key]);
  32. return;
  33. }
  34. var defaultValue = '__TRANSLATION__';
  35. parser.set(key, defaultValue);
  36. };
  37. var zhParser = new Parser();
  38. var enParser = new Parser();
  39. var ruParser = new Parser();
  40. var esParser = new Parser();
  41. function processDirectory(directoryPath) {
  42. const files = fs.readdirSync(directoryPath);
  43. for (let file of files) {
  44. let fullPath = path.join(directoryPath, file);
  45. if (fs.statSync(fullPath).isDirectory()) {
  46. processDirectory(fullPath);
  47. } else {
  48. let content = fs.readFileSync(fullPath, 'utf-8');
  49. zhParser.parseFuncFromString(content, { list: ['t'] }, (key) =>
  50. zhHandler(key, zhParser),
  51. );
  52. enParser.parseFuncFromString(content, { list: ['t'] }, (key) =>
  53. otherLanguageHandler(key, enParser, enExclude, existedEnTranslation),
  54. );
  55. ruParser.parseFuncFromString(content, { list: ['t'] }, (key) =>
  56. otherLanguageHandler(key, ruParser, ruExclude, existedRuTranslation),
  57. );
  58. esParser.parseFuncFromString(content, { list: ['t'] }, (key) =>
  59. otherLanguageHandler(key, esParser, esExclude, existedEsTranslation),
  60. );
  61. }
  62. }
  63. }
  64. // 处理所有目录
  65. for (let directoryPath of directoryPaths) {
  66. processDirectory(directoryPath);
  67. }
  68. var zhTranslations = zhParser.get({ sort: true });
  69. var enTranslations = enParser.get({ sort: true });
  70. var ruTranslations = ruParser.get({ sort: true });
  71. var esTranslations = esParser.get({ sort: true });
  72. // 写入翻译到目标json文件
  73. fs.writeFileSync(
  74. 'src/i18n/zh.json',
  75. JSON.stringify(zhTranslations.en, null, 2),
  76. );
  77. fs.writeFileSync(
  78. 'src/i18n/en.json',
  79. JSON.stringify(enTranslations.en, null, 2),
  80. );
  81. fs.writeFileSync(
  82. 'src/i18n/ru.json',
  83. JSON.stringify(ruTranslations.en, null, 2),
  84. );
  85. fs.writeFileSync(
  86. 'src/i18n/es.json',
  87. JSON.stringify(esTranslations.en, null, 2),
  88. );
  89. var fileTranslations = Object.keys(zhTranslations.en.translation).length;
  90. console.log('✅ 翻译查找完成,共找到 ' + fileTranslations + ' 个翻译项');