fix: use Google Cloud API key for classic mode + translation verification
All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 2s
All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 2s
Two critical fixes: 1. Provider "google" (default classic mode) now checks for a Google Cloud API key (GOOGLE_CLOUD_API_KEY in env or admin settings). If present, uses GoogleCloudTranslationProvider (official API). Previously it always fell through to deep_translator (free scraper) which gets blocked in production, silently returning untranslated text. 2. Added translation verification: each translator now tracks how many texts were attempted vs actually changed. If 0 texts were translated, the job is marked as FAILED with a clear error message instead of returning the original file as "completed". Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -63,6 +63,7 @@ class PDFTranslator:
|
||||
def __init__(self, provider=None):
|
||||
self._provider = provider
|
||||
self._font_path: Optional[str] = None
|
||||
self._translation_stats = {"attempted": 0, "changed": 0}
|
||||
|
||||
def _get_font_path(self) -> Optional[str]:
|
||||
"""Resolve a Unicode-capable TTF/OTF font file."""
|
||||
@@ -825,18 +826,31 @@ class PDFTranslator:
|
||||
self, texts: List[str], target_language: str, source_language: str
|
||||
) -> List[str]:
|
||||
"""Translate a batch of texts."""
|
||||
non_empty = [t for t in texts if t and t.strip()]
|
||||
self._translation_stats["attempted"] += len(non_empty)
|
||||
|
||||
translated = None
|
||||
if self._provider is not None:
|
||||
try:
|
||||
return self._provider.translate_batch(texts, target_language, source_language)
|
||||
translated = self._provider.translate_batch(texts, target_language, source_language)
|
||||
except Exception as e:
|
||||
logger.warning("provider_translate_failed", error=str(e))
|
||||
|
||||
from services.translation_service import translation_service
|
||||
try:
|
||||
return translation_service.translate_batch(texts, target_language, source_language)
|
||||
except Exception as e:
|
||||
logger.warning("legacy_translate_failed", error=str(e))
|
||||
return texts
|
||||
if translated is None:
|
||||
from services.translation_service import translation_service
|
||||
try:
|
||||
translated = translation_service.translate_batch(texts, target_language, source_language)
|
||||
except Exception as e:
|
||||
logger.warning("legacy_translate_failed", error=str(e))
|
||||
translated = texts
|
||||
|
||||
changed = sum(1 for orig, trans in zip(texts, translated) if orig != trans and trans.strip())
|
||||
self._translation_stats["changed"] += changed
|
||||
|
||||
return translated
|
||||
|
||||
def get_translation_stats(self) -> dict:
|
||||
return dict(self._translation_stats)
|
||||
|
||||
def _validate_file(self, file_path: Path) -> None:
|
||||
if not file_path.exists():
|
||||
|
||||
Reference in New Issue
Block a user