diff --git a/nodes/show_translate_string.py b/nodes/show_translate_string.py index 03e9011..043bb6a 100644 --- a/nodes/show_translate_string.py +++ b/nodes/show_translate_string.py @@ -1,4 +1,4 @@ -from ..translation_utils import TranslationUtils +from ..translation_utils import translate_texts class ShowTranslateString: @classmethod @@ -32,6 +32,6 @@ class ShowTranslateString: strings_to_translate = [str(input_strings)] #逐个翻译每个字符串 - translated_strings = TranslationUtils.translate_with_length_check(strings_to_translate, target_language) + translated_strings = translate_texts(strings_to_translate, target_language) return {"ui": {"translated_strings": translated_strings}, "result": (translated_strings,)} diff --git a/nodes/string_list.py b/nodes/string_list.py index e970375..131c1a3 100644 --- a/nodes/string_list.py +++ b/nodes/string_list.py @@ -2,7 +2,7 @@ import random import os import csv import chardet -from ..translation_utils import TranslationUtils +from ..translation_utils import translate_texts class BaseStringList: """Base class for string list operations""" @@ -108,7 +108,7 @@ class BaseStringList: def translate_strings(self, strings): """Translate a list of strings to English using Bing translator with auto language detection""" try: - translated_strings = TranslationUtils.translate_with_length_check(strings, 'en') + translated_strings = translate_texts(strings, 'en') return translated_strings except Exception as e: print(f"Translation error: {str(e)}") @@ -453,7 +453,7 @@ class StringListToCSV(BaseStringList): # Prepare rows for CSV rows_to_write = [] for string_value in string_list: - zh_text = TranslationUtils.translate_with_length_check([string_value], 'zh')[0] if translate else '' + zh_text = translate_texts([string_value], 'zh')[0] if translate else '' rows_to_write.append({ 'string': string_value, 'zh': zh_text, diff --git a/translation_utils.py b/translation_utils.py index 3ecca96..8d31b32 100644 --- a/translation_utils.py +++ b/translation_utils.py @@ -1,49 +1,59 @@ -import translators.server as tss +import translators as ts import re -class TranslationUtils: - @staticmethod - def translate_with_length_check(strings, target_language): - translated_strings = [] - for text in strings: - # Check if the string is too long and split if necessary - if len(text) > 500: - parts = text.split('\n') # Split by paragraphs - temp_translated_strings = [] - for part in parts: - if len(part) > 500: - sub_parts = re.split(r'(?<=[.!?]) +', part) # Split long paragraphs by sentences - for sub_part in sub_parts: - if len(sub_part) > 500: - sub_sub_parts = [sub_part[i:i+500] for i in range(0, len(sub_part), 500)] - for sub_sub_part in sub_sub_parts: - try: - translated_string = tss.bing(sub_sub_part, from_language='auto', to_language=target_language) - temp_translated_strings.append(translated_string) - except Exception as e: - temp_translated_strings.append(f"Translation error: {str(e)}") - else: - try: - translated_string = tss.bing(sub_part, from_language='auto', to_language=target_language) - temp_translated_strings.append(translated_string) - except Exception as e: - temp_translated_strings.append(f"Translation error: {str(e)}") - else: - try: - translated_string = tss.bing(part, from_language='auto', to_language=target_language) - temp_translated_strings.append(translated_string) - except Exception as e: - temp_translated_strings.append(f"Translation error: {str(e)}") - # Merge translated strings if they were split - if len(temp_translated_strings) > 1: - translated_strings.append(' '.join(temp_translated_strings)) - else: - translated_strings.append(temp_translated_strings[0] if temp_translated_strings else '') - else: - try: - translated_string = tss.bing(text, from_language='auto', to_language=target_language) - translated_strings.append(translated_string) - except Exception as e: - translated_strings.append(f"Translation error: {str(e)}") +def split_text_for_translation(text): + """ + 按段落和完整句子拆分文本,返回句子列表(保持原有段落结构)。 + """ + paragraphs = text.split('\n') + split_paragraphs = [] + # 句子分隔符:句号、问号、感叹号(中英文) + sentence_pattern = re.compile(r'([^。!?!?\r\n]+[。!?!?])', re.M) + for para in paragraphs: + if not para.strip(): + split_paragraphs.append(['']) + continue + sentences = sentence_pattern.findall(para) + # 若正则未能分出句子,则整体作为一句 + if not sentences: + sentences = [para] + split_paragraphs.append(sentences) + return split_paragraphs - return translated_strings +def translate_text(text, to_lang='zh'): + """ + 自动处理长文本,按段落和句子拆分,逐句翻译,组合返回。 + """ + if not text: + return "" + try: + # 拆分文本为段落和句子 + split_paragraphs = split_text_for_translation(text) + translated_paragraphs = [] + for sentences in split_paragraphs: + if sentences == ['']: + translated_paragraphs.append('') + continue + translated_sentences = [] + for sent in sentences: + sent = sent.strip() + if not sent: + continue + # 逐句翻译 + translated = ts.translate_text(sent, translator='bing', from_language='auto', to_language=to_lang) + translated_sentences.append(translated) + translated_paragraphs.append(''.join(translated_sentences)) + return '\n'.join(translated_paragraphs) + except Exception as e: + return f"[翻译失败] {str(e)}" + +def translate_texts(strings, target_language): + translated_strings = [] + for text in strings: + try: + translated_string = translate_text(text, target_language) + translated_strings.append(translated_string) + except Exception as e: + translated_strings.append(f"Translation error: {str(e)}") + + return translated_strings