patch-en-chinese.cjs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * 将 en.json 中仍为中文的条目按 zh.json 对应值翻译为英文
  3. * 用法: node scripts/patch-en-chinese.cjs
  4. */
  5. const fs = require('fs');
  6. const root = 'd:/project/2026项目/DeepAICloud';
  7. const zhPath = `${root}/src/locales/zh.json`;
  8. const enPath = `${root}/src/locales/en.json`;
  9. const zhToEnManual = require('./en-translations-manual.cjs');
  10. const zh = JSON.parse(fs.readFileSync(zhPath, 'utf8'));
  11. const en = JSON.parse(fs.readFileSync(enPath, 'utf8'));
  12. let patched = 0;
  13. function patchByZh(zhPart, enPart) {
  14. if (typeof zhPart === 'string' && typeof enPart === 'string') {
  15. if (/[\u4e00-\u9fff]/.test(enPart) && Object.prototype.hasOwnProperty.call(zhToEnManual, zhPart)) {
  16. enPart = zhToEnManual[zhPart];
  17. patched++;
  18. }
  19. return enPart;
  20. }
  21. if (Array.isArray(zhPart) && Array.isArray(enPart)) {
  22. return zhPart.map((item, i) => patchByZh(item, enPart[i]));
  23. }
  24. if (zhPart && enPart && typeof zhPart === 'object' && typeof enPart === 'object') {
  25. for (const key of Object.keys(zhPart)) {
  26. enPart[key] = patchByZh(zhPart[key], enPart[key]);
  27. }
  28. return enPart;
  29. }
  30. return enPart;
  31. }
  32. patchByZh(zh, en);
  33. fs.writeFileSync(enPath, `${JSON.stringify(en, null, 2)}\n`);
  34. const remaining = [];
  35. function countRemaining(zhPart, enPart, path = '') {
  36. if (typeof zhPart === 'string' && typeof enPart === 'string') {
  37. if (/[\u4e00-\u9fff]/.test(enPart)) remaining.push({ path, zh: zhPart, en: enPart });
  38. return;
  39. }
  40. if (zhPart && enPart && typeof zhPart === 'object' && !Array.isArray(zhPart)) {
  41. for (const key of Object.keys(zhPart)) {
  42. countRemaining(zhPart[key], enPart[key], path ? `${path}.${key}` : key);
  43. }
  44. }
  45. }
  46. countRemaining(zh, en);
  47. console.log(`Patched ${patched} strings. Remaining Chinese in en.json: ${remaining.length}`);