translate.cjs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. require('dotenv').config();
  2. const fs = require('fs');
  3. const path = require('path');
  4. const API_URL = 'https://api.deepseek.com/v1/chat/completions';
  5. const API_KEY = process.env.DEEPSEEK_API_KEY;
  6. const SOURCE_LANG = 'zh-cn';
  7. const LOCALE_FILE = path.resolve(__dirname, '../lang/index.json');
  8. // 引入进度条库
  9. const cliProgress = require('cli-progress');
  10. const colors = require('colors');
  11. async function translateBatch(texts, targetLang) {
  12. // 构建批量内容:每行以[index]开头
  13. const batchContent = texts.map((text, index) => `[${index}] ${text}`).join('\n');
  14. const response = await fetch(API_URL, {
  15. method: 'POST',
  16. headers: {
  17. 'Content-Type': 'application/json',
  18. Authorization: `Bearer ${API_KEY}`,
  19. },
  20. body: JSON.stringify({
  21. model: 'deepseek-chat',
  22. messages: [
  23. {
  24. role: 'system',
  25. content: `作为企业软件系统专业翻译,严格遵守以下铁律:
  26. ■ 核心原则
  27. 1. 严格逐符号翻译(${SOURCE_LANG}→${targetLang})
  28. 2. 禁止添加/删除/改写任何内容
  29. 3. 保持批量翻译的编号格式
  30. ■ 符号保留规则
  31. ! 所有符号必须原样保留:
  32. • 编程符号:\${ } <% %> @ # & |
  33. • UI占位符:{0} %s [ ]
  34. • 货币单位:¥100.00 kg cm²
  35. • 中文符号:【 】 《 》 :
  36. ■ 中文符号位置规范
  37. # 三级处理机制:
  38. 1. 成对符号必须保持完整结构:
  39. ✓ 正确:【Warning】Text
  40. ✗ 禁止:Warning【 】Text
  41. 2. 独立符号位置:
  42. • 优先句尾 → Text】?
  43. • 次选句首 → 】Text?
  44. • 禁止句中 → Text】Text?
  45. 3. 跨字符串符号处理:
  46. • 前段含【时 → 保留在段尾("Synchronize【")
  47. • 后段含】时 → 保留在段首("】authorization data?")
  48. • 符号后接字母时添加空格:】 Authorization
  49. ■ 语法规范
  50. • 外文 → 被动语态("Item was created")
  51. • 中文 → 主动语态("已创建项目")
  52. • 禁止推测上下文(只翻译当前字符串内容)
  53. ■ 错误预防(绝对禁止)
  54. ✗ 将中文符号改为西式符号(】→])
  55. ✗ 移动非中文符号位置
  56. ✗ 添加原文不存在的内容
  57. ✗ 合并/拆分原始字符串
  58. ■ 批量处理
  59. ▸ 严格保持原始JSON结构
  60. ▸ 语言键名精确匹配(zh-cn/en/it等)`
  61. },
  62. {
  63. role: 'user',
  64. content: batchContent,
  65. },
  66. ],
  67. temperature: 0.3,
  68. max_tokens: 4000,
  69. }),
  70. });
  71. const data = await response.json();
  72. if (!response.ok || !data.choices || !data.choices[0]?.message?.content) {
  73. const errorMsg = data.error?.message || `HTTP ${response.status}: ${response.statusText}`;
  74. throw new Error(`翻译API返回错误:${errorMsg}`);
  75. }
  76. // 解析批量响应
  77. const batchResult = data.choices[0].message.content.trim();
  78. const translations = {};
  79. // 按行分割结果
  80. const lines = batchResult.split('\n');
  81. for (const line of lines) {
  82. // 使用更精确的匹配模式
  83. const match = line.match(/^\[(\d+)\]\s*(.+)/);
  84. if (match) {
  85. const index = parseInt(match[1]);
  86. translations[index] = match[2].trim();
  87. }
  88. }
  89. return translations;
  90. }
  91. function extractTargetLangs(localeData) {
  92. const allLangs = new Set();
  93. for (const translations of Object.values(localeData)) {
  94. for (const lang of Object.keys(translations)) {
  95. if (lang !== SOURCE_LANG) {
  96. allLangs.add(lang);
  97. }
  98. }
  99. }
  100. return [...allLangs];
  101. }
  102. function groupTasksByLang(localeData, targetLangs) {
  103. const tasks = {};
  104. for (const lang of targetLangs) {
  105. tasks[lang] = {
  106. keys: [],
  107. texts: [],
  108. };
  109. }
  110. for (const [key, translations] of Object.entries(localeData)) {
  111. const sourceText = translations[SOURCE_LANG];
  112. if (!sourceText) {
  113. console.warn(`⚠️ 缺少源语言(${SOURCE_LANG})文本: ${key}`);
  114. continue;
  115. }
  116. for (const lang of targetLangs) {
  117. if (!translations[lang] || translations[lang].trim() === '') {
  118. tasks[lang].keys.push(key);
  119. tasks[lang].texts.push(sourceText);
  120. }
  121. }
  122. }
  123. return tasks;
  124. }
  125. async function main() {
  126. // 读取语言文件
  127. const rawData = fs.readFileSync(LOCALE_FILE);
  128. const localeData = JSON.parse(rawData);
  129. const TARGET_LANGS = extractTargetLangs(localeData);
  130. const langTasks = groupTasksByLang(localeData, TARGET_LANGS);
  131. let totalUpdated = 0;
  132. const BATCH_SIZE = 10;
  133. // 创建多进度条容器
  134. const multibar = new cliProgress.MultiBar(
  135. {
  136. format: '{lang} |' + colors.cyan('{bar}') + '| {percentage}% | {value}/{total} 条',
  137. barCompleteChar: '\u2588',
  138. barIncompleteChar: '\u2591',
  139. hideCursor: true,
  140. clearOnComplete: true,
  141. stopOnComplete: true,
  142. },
  143. cliProgress.Presets.shades_grey
  144. );
  145. // 为每个语言创建进度条
  146. const progressBars = {};
  147. for (const lang of TARGET_LANGS) {
  148. if (langTasks[lang].texts.length > 0) {
  149. progressBars[lang] = multibar.create(langTasks[lang].texts.length, 0, {
  150. lang: lang.padEnd(6, ' '),
  151. });
  152. }
  153. }
  154. // 并行处理所有语言
  155. await Promise.all(
  156. Object.entries(langTasks).map(async ([lang, task]) => {
  157. if (task.texts.length === 0) return;
  158. // 分批处理
  159. for (let i = 0; i < task.texts.length; i += BATCH_SIZE) {
  160. const batchKeys = task.keys.slice(i, i + BATCH_SIZE);
  161. const batchTexts = task.texts.slice(i, i + BATCH_SIZE);
  162. try {
  163. const batchResults = await translateBatch(batchTexts, lang);
  164. // 更新翻译结果
  165. batchKeys.forEach((key, index) => {
  166. if (batchResults[index] !== undefined) {
  167. localeData[key][lang] = batchResults[index];
  168. totalUpdated++;
  169. } else {
  170. console.error(`❌ 缺失翻译结果 [${key}@${lang}]`);
  171. localeData[key][lang] = `[BATCH_ERROR] ${localeData[key][SOURCE_LANG]}`;
  172. }
  173. });
  174. // 更新进度条
  175. progressBars[lang].increment(batchTexts.length);
  176. // 每批处理后保存进度
  177. fs.writeFileSync(LOCALE_FILE, JSON.stringify(localeData, null, 2));
  178. // 添加请求间隔避免速率限制
  179. await new Promise((resolve) => setTimeout(resolve, 300));
  180. } catch (error) {
  181. console.error(`\n❌ 批次翻译失败 [${lang}]:`, error.message);
  182. // 标记失败条目
  183. batchKeys.forEach((key) => {
  184. localeData[key][lang] = `[TRANSLATION_FAILED] ${localeData[key][SOURCE_LANG]}`;
  185. });
  186. // 跳过当前批次继续处理
  187. progressBars[lang].increment(batchTexts.length);
  188. }
  189. }
  190. })
  191. );
  192. // 停止所有进度条
  193. multibar.stop();
  194. // 最终保存
  195. fs.writeFileSync(LOCALE_FILE, JSON.stringify(localeData, null, 2));
  196. // 显示最终结果
  197. if (totalUpdated > 0) {
  198. console.log(`\n✅ 翻译完成! 共更新 ${totalUpdated} 处翻译`);
  199. } else {
  200. console.log('\nℹ️ 没有需要更新的翻译');
  201. }
  202. }
  203. main().catch(console.error);