apply_translations.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. const fs = require('fs');
  2. const files = require('./files_to_translate.json');
  3. const translationMap = require('./translation_map.json');
  4. let totalReplaced = 0;
  5. files.forEach(file => {
  6. let content = fs.readFileSync(file, 'utf8');
  7. let originalContent = content;
  8. // Replace simple strings
  9. const simpleStringRegex = /((?:console|logger|Logger|this\.logger)\.(?:log|error|warn|info|debug|verbose)\(\s*)(['"])(.*?[\u4e00-\u9fa5]+.*?)\2/g;
  10. content = content.replace(simpleStringRegex, (match, prefix, quote, innerString) => {
  11. if (translationMap[innerString]) {
  12. totalReplaced++;
  13. return prefix + quote + translationMap[innerString] + quote;
  14. }
  15. return match;
  16. });
  17. // Replace template literals
  18. const templateRegex = /((?:console|logger|Logger|this\.logger)\.(?:log|error|warn|info|debug|verbose)\(\s*)\`([\s\S]*?)\`/g;
  19. content = content.replace(templateRegex, (match, prefix, innerString) => {
  20. if (/[\\u4e00-\\u9fa5]/.test(innerString) && translationMap[innerString]) {
  21. totalReplaced++;
  22. return prefix + '`' + translationMap[innerString] + '`';
  23. }
  24. // If no direct match, check if there's an exact match in the map
  25. if (translationMap[innerString]) {
  26. totalReplaced++;
  27. return prefix + '`' + translationMap[innerString] + '`';
  28. }
  29. return match;
  30. });
  31. if (content !== originalContent) {
  32. fs.writeFileSync(file, content, 'utf8');
  33. console.log('Updated ' + file);
  34. }
  35. });
  36. console.log('Total replacements made: ' + totalReplaced);