File size: 586 Bytes
bbd259b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from deep_translator import GoogleTranslator
import time

def translate_to_english(text: str, source="auto") -> str:
    """
    Translates input text to English using Google Translator.
    Retries once on failure.
    """
    try:
        # Use simple 'auto' detection or specific source
        translator = GoogleTranslator(source=source, target='en')
        translated = translator.translate(text)
        return translated
    except Exception as e:
        print(f"[WARNING] Translation failed: {e}")
        # Fallback to original text if translation fails
        return text