import os import json import logging import gradio as gr logger = logging.getLogger("app.translation") def get_translation(text, target_lang, cache): if target_lang.lower() in ["en", "english"]: return text if target_lang not in cache: cache[target_lang] = {} if text in cache[target_lang]: return cache[target_lang][text] try: from deep_translator import GoogleTranslator # Reverse map if needed, but localize_init passes the full name "German" # We need to robustly map "German" -> "de" or just use the code # Actually localize_init sets `lang = code_map.get(lang_code, "English")` # So we are passing "German". Deep Translator needs "de". # Let's add a robust mapper inside get_translation or strict code usage # Simple mapper for standard languages lang_map = { "german": "de", "french": "fr", "spanish": "es", "italian": "it", "portuguese": "pt", "russian": "ru", "japanese": "ja", "chinese": "zh-CN", "english": "en" } target_iso = lang_map.get(target_lang.lower(), target_lang.lower()) translator = GoogleTranslator(source='auto', target=target_iso) trans = translator.translate(text) cache[target_lang][text] = trans return trans except Exception as e: logger.error(f"Translation error: {e}") return text def localize_init(lang_code="en"): # Map codes code_map = {"de": "German", "fr": "French", "es": "Spanish", "it": "Italian", "pt": "Portuguese", "ru": "Russian", "ja": "Japanese", "zh": "Chinese"} lang = code_map.get(lang_code, "English") cache_file = "translation_cache.json" cache = {} if os.path.exists(cache_file): try: with open(cache_file, "r") as f: cache = json.load(f) except: pass dirty = False def translate(text): nonlocal dirty t = get_translation(text, lang, cache) if t != text: dirty = True return t # Static Welcome Table for Robustness on HF STATIC_WELCOME = { "English": "Hello. I am Sage 6.5. I can consult the Oracle for your Name, a specific Topic, or a specific Date. How shall I assist you?", "German": "Hallo. Ich bin Sage 6.5. Ich kann das Orakel nach Ihrem Namen, einem bestimmten Thema oder einem bestimmten Datum fragen. Wie soll ich Ihnen helfen?", "French": "Bonjour. Je suis Sage 6.5. Je peux consulter l'Oracle pour votre Nom, un Sujet spécifique ou une Date spécifique. Comment puis-je vous aider ?", "Spanish": "Hola. Soy sabio 6.5. Puedo consultar el Oráculo por tu Nombre, un Tema específico o una Fecha específica. ¿Cómo puedo ayudarte?", "Italian": "Ciao. Sono Saggio 6.5. Posso consultare l'Oracolo per il tuo Nome, un Argomento specifico o una Data specifica. Come posso aiutarti?", "Portuguese": "Olá. Eu sou o Sábio 6.5. Posso consultar o Oráculo para o seu Nome, um Tópico específico ou uma Data específica. Como devo ajudá-lo?", "Russian": "Привет. Я Сейдж 6,5. Я могу узнать у Оракула ваше имя, конкретную тему или конкретную дату. Как я могу вам помочь?", "Japanese": "こんにちは。私はセージ6.5です。あなたの名前、特定のトピック、または特定の日付についてオラクルに問い合わせることができます。どのようにお手伝いさせていただけますか?", "Chinese": "你好。我是圣人6.5。 I can consult the Oracle for your Name, a specific Topic, or a specific Date.我该如何帮助你?" } res = { "welcome": STATIC_WELCOME.get(lang, STATIC_WELCOME["English"]), "label_short": translate("Short Answers"), "placeholder": translate("Type your message..."), "lang": lang } if dirty: try: with open(cache_file, "w") as f: json.dump(cache, f, indent=2) except: pass return res