extract_strings.js 1.4 KB

1234567891011121314151617181920212223242526272829
  1. const fs = require('fs');
  2. const files = require('./files_to_translate.json');
  3. const translationMap = {};
  4. const logRegex = /(?:console|logger|Logger|this\.logger)\.(?:log|error|warn|info|debug|verbose)\(\s*((?:[\'\"\`])(?:.*?)[\u4e00-\u9fa5]+(?:.*?)(?:[\'\"\`]))/g;
  5. // We also need to catch template literals that have variables, e.g. `User ${userId} created`
  6. const logRegexTemplate = /(?:console|logger|Logger|this\.logger)\.(?:log|error|warn|info|debug|verbose)\(\s*(\`(?:.*?)\`)/gs;
  7. files.forEach(file => {
  8. const content = fs.readFileSync(file, 'utf8');
  9. let match;
  10. // Match single/double quotes with Chinese
  11. const simpleStringRegex = /(?:console|logger|Logger|this\.logger)\.(?:log|error|warn|info|debug|verbose)\(\s*(['"])(.*?[\u4e00-\u9fa5]+.*?)\1/g;
  12. while ((match = simpleStringRegex.exec(content)) !== null) {
  13. translationMap[match[2]] = ""; // The inner string
  14. }
  15. // Match template literals with Chinese
  16. const templateRegex = /(?:console|logger|Logger|this\.logger)\.(?:log|error|warn|info|debug|verbose)\(\s*\`([\s\S]*?)\`/g;
  17. while ((match = templateRegex.exec(content)) !== null) {
  18. if (/[\u4e00-\u9fa5]/.test(match[1])) {
  19. translationMap[match[1]] = "";
  20. }
  21. }
  22. });
  23. fs.writeFileSync('translation_map.json', JSON.stringify(translationMap, null, 2));
  24. console.log('Extracted ' + Object.keys(translationMap).length + ' distinct strings.');