| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- /**
- * 将 en.json 中仍为中文的条目按 zh.json 对应值翻译为英文
- * 用法: node scripts/patch-en-chinese.cjs
- */
- const fs = require('fs');
- const root = 'd:/project/2026项目/DeepAICloud';
- const zhPath = `${root}/src/locales/zh.json`;
- const enPath = `${root}/src/locales/en.json`;
- const zhToEnManual = require('./en-translations-manual.cjs');
- const zh = JSON.parse(fs.readFileSync(zhPath, 'utf8'));
- const en = JSON.parse(fs.readFileSync(enPath, 'utf8'));
- let patched = 0;
- function patchByZh(zhPart, enPart) {
- if (typeof zhPart === 'string' && typeof enPart === 'string') {
- if (/[\u4e00-\u9fff]/.test(enPart) && Object.prototype.hasOwnProperty.call(zhToEnManual, zhPart)) {
- enPart = zhToEnManual[zhPart];
- patched++;
- }
- return enPart;
- }
- if (Array.isArray(zhPart) && Array.isArray(enPart)) {
- return zhPart.map((item, i) => patchByZh(item, enPart[i]));
- }
- if (zhPart && enPart && typeof zhPart === 'object' && typeof enPart === 'object') {
- for (const key of Object.keys(zhPart)) {
- enPart[key] = patchByZh(zhPart[key], enPart[key]);
- }
- return enPart;
- }
- return enPart;
- }
- patchByZh(zh, en);
- fs.writeFileSync(enPath, `${JSON.stringify(en, null, 2)}\n`);
- const remaining = [];
- function countRemaining(zhPart, enPart, path = '') {
- if (typeof zhPart === 'string' && typeof enPart === 'string') {
- if (/[\u4e00-\u9fff]/.test(enPart)) remaining.push({ path, zh: zhPart, en: enPart });
- return;
- }
- if (zhPart && enPart && typeof zhPart === 'object' && !Array.isArray(zhPart)) {
- for (const key of Object.keys(zhPart)) {
- countRemaining(zhPart[key], enPart[key], path ? `${path}.${key}` : key);
- }
- }
- }
- countRemaining(zh, en);
- console.log(`Patched ${patched} strings. Remaining Chinese in en.json: ${remaining.length}`);
|