Commit 3652f85f7565d23df3a5140726df0c59703f06fc

Authored by tangwang
1 parent bb9c626c

trans for index

Showing 1 changed file with 29 additions and 72 deletions   Show diff stats
query/translator.py
... ... @@ -136,11 +136,11 @@ class Translator:
136 136  
137 137 # Optimization: Skip translation if not needed
138 138 if target_lang == 'en' and self._is_english_text(text):
139   - logger.debug(f"[Translator] Text is already English, skipping translation: '{text[:50]}...'")
  139 + logger.info(f"[Translator] Text is already English, skipping translation: '{text[:50]}...'")
140 140 return text
141 141  
142 142 if target_lang == 'zh' and (self._contains_chinese(text) or self._is_pure_number(text)):
143   - logger.debug(f"[Translator] Text contains Chinese or is pure number, skipping translation: '{text[:50]}...'")
  143 + logger.info(f"[Translator] Text contains Chinese or is pure number, skipping translation: '{text[:50]}...'")
144 144 return text
145 145  
146 146 # Use provided context or default context
... ... @@ -157,6 +157,10 @@ class Translator:
157 157 if self.use_cache and self.redis_client:
158 158 cached = self._get_cached_translation_redis(text, target_lang, source_lang, translation_context, prompt)
159 159 if cached:
  160 + logger.info(
  161 + f"[Translator] Cache hit: source={source_lang or 'auto'} "
  162 + f"target={target_lang} | text='{text[:80]}...' -> '{cached[:80]}...'"
  163 + )
160 164 return cached
161 165  
162 166 # If no API key, return mock translation (for testing)
... ... @@ -164,19 +168,24 @@ class Translator:
164 168 logger.debug(f"[Translator] No API key, returning original text (mock mode)")
165 169 return text
166 170  
167   - # Translate using DeepL with fallback
  171 + # Translate using DeepL (Pro endpoint only, no free fallback)
  172 + logger.info(
  173 + f"[Translator] Translating text: target={target_lang}, "
  174 + f"source={source_lang or 'auto'}, context={translation_context}, "
  175 + f"prompt={'yes' if prompt else 'no'} | text='{text[:80]}...'"
  176 + )
168 177 result = self._translate_deepl(text, target_lang, source_lang, translation_context, prompt)
169 178  
170   - # If translation failed, try fallback to free API
171   - if result is None and "api.deepl.com" in self.DEEPL_API_URL:
172   - logger.debug(f"[Translator] Pro API failed, trying free API...")
173   - result = self._translate_deepl_free(text, target_lang, source_lang, translation_context, prompt)
174   -
175 179 # If still failed, return original text with warning
176 180 if result is None:
177 181 logger.warning(f"[Translator] Translation failed for '{text[:50]}...', returning original text")
178 182 result = text
179 183  
  184 + logger.info(
  185 + f"[Translator] Translation completed: source={source_lang or 'auto'} "
  186 + f"target={target_lang} | original='{text[:80]}...' -> '{result[:80]}...'"
  187 + )
  188 +
180 189 # Cache result
181 190 if result and self.use_cache and self.redis_client:
182 191 self._set_cached_translation_redis(text, target_lang, result, source_lang, translation_context, prompt)
... ... @@ -268,68 +277,9 @@ class Translator:
268 277 logger.error(f"[Translator] Translation failed: {e}", exc_info=True)
269 278 return None
270 279  
271   - def _translate_deepl_free(
272   - self,
273   - text: str,
274   - target_lang: str,
275   - source_lang: Optional[str],
276   - context: Optional[str] = None,
277   - prompt: Optional[str] = None
278   - ) -> Optional[str]:
279   - """
280   - Translate using DeepL Free API.
281   -
282   - Note: Free API may not support glossary_id parameter.
283   - """
284   - # Map to DeepL language codes
285   - target_code = self.LANG_CODE_MAP.get(target_lang, target_lang.upper())
286   -
287   - headers = {
288   - "Authorization": f"DeepL-Auth-Key {self.api_key}",
289   - "Content-Type": "application/json",
290   - }
291   -
292   - # Use prompt as context parameter for DeepL API
293   - api_context = prompt if prompt else context
294   -
295   - payload = {
296   - "text": [text],
297   - "target_lang": target_code,
298   - }
299   -
300   - if source_lang:
301   - source_code = self.LANG_CODE_MAP.get(source_lang, source_lang.upper())
302   - payload["source_lang"] = source_code
303   -
304   - # Add context parameter
305   - if api_context:
306   - payload["context"] = api_context
307   -
308   - # Note: Free API typically doesn't support glossary_id
309   - # But we can still use context hints in the text
310   -
311   - try:
312   - response = requests.post(
313   - "https://api-free.deepl.com/v2/translate",
314   - headers=headers,
315   - json=payload,
316   - timeout=self.timeout
317   - )
318   -
319   - if response.status_code == 200:
320   - data = response.json()
321   - if "translations" in data and len(data["translations"]) > 0:
322   - return data["translations"][0]["text"]
323   - else:
324   - logger.error(f"[Translator] DeepL Free API error: {response.status_code} - {response.text}")
325   - return None
326   -
327   - except requests.Timeout:
328   - logger.warning(f"[Translator] Free API request timed out")
329   - return None
330   - except Exception as e:
331   - logger.error(f"[Translator] Free API translation failed: {e}", exc_info=True)
332   - return None
  280 + # NOTE: _translate_deepl_free is intentionally not implemented.
  281 + # We do not support automatic fallback to the free endpoint, to avoid
  282 + # mixing Pro keys with https://api-free.deepl.com and related 403 errors.
333 283  
334 284 def translate_multi(
335 285 self,
... ... @@ -493,8 +443,12 @@ class Translator:
493 443 if value:
494 444 # Sliding expiration: reset expiration time on access
495 445 self.redis_client.expire(cache_key, self.expire_time)
496   - logger.debug(f"[Translator] Cache hit for translation: {text} -> {target_lang}")
  446 + logger.info(
  447 + f"[Translator] Redis cache hit: key={cache_key}, "
  448 + f"target={target_lang}, value='{value[:80]}...'"
  449 + )
497 450 return value
  451 + logger.debug(f"[Translator] Redis cache miss: key={cache_key}, target={target_lang}")
498 452 return None
499 453 except Exception as e:
500 454 logger.error(f"[Translator] Redis error during get translation cache: '{text}' {target_lang}: {e}")
... ... @@ -516,7 +470,10 @@ class Translator:
516 470 try:
517 471 cache_key = f"{self.cache_prefix}:{target_lang.upper()}:{text}"
518 472 self.redis_client.setex(cache_key, self.expire_time, translation)
519   - logger.debug(f"[Translator] Cached translation: {text} -> {target_lang}: {translation}")
  473 + logger.info(
  474 + f"[Translator] Cached translation: key={cache_key}, "
  475 + f"target={target_lang}, value='{translation}...'"
  476 + )
520 477 except Exception as e:
521 478 logger.error(f"[Translator] Redis error during set translation cache: '{text}' {target_lang}: {e}")
522 479  
... ...