(chinese_texts, lang)
| 62 | |
| 63 | # 翻译并返回翻译结果 |
| 64 | def translate_and_collect(chinese_texts, lang): |
| 65 | translations = {} |
| 66 | threads = [] |
| 67 | chunk_size = len(chinese_texts) // 5 or 1 # 每次访问api为5个. |
| 68 | for i in range(0, len(chinese_texts), chunk_size): |
| 69 | chunk = chinese_texts[i:i + chunk_size] |
| 70 | thread = threading.Thread(target=translate_worker, args=(chunk, translations, lang)) |
| 71 | threads.append(thread) |
| 72 | thread.start() |
| 73 | |
| 74 | for thread in threads: |
| 75 | thread.join() |
| 76 | translation_output = "" |
| 77 | for line_number, chinese_text, translated_text in [(ln, ct, translations.get((ln, ct), None)) for ln, ct in chinese_texts if (ln, ct) in translations]: |
| 78 | translated_text = clean_text(translated_text) |
| 79 | if line_number == 0: # 第一行永远是标题 |
| 80 | translation_output += f'// @name:{lang} {translated_text}\n' |
| 81 | else: |
| 82 | translation_output += f'// @description:{lang} {translated_text}' |
| 83 | return translation_output |
| 84 | |
| 85 | |
| 86 | def translate_task(file_path, lang, chinese_texts, file_lock): |
no test coverage detected