Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion app/services/translation.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
import asyncio
import re

from async_lru import alru_cache
from deep_translator import GoogleTranslator
from loguru import logger

# Pre-defined translations for known static catalog names.
# Ensures consistent informal (du-form) German regardless of Google Translate output.
_STATIC_TRANSLATIONS: dict[tuple[str, str], str] = {
("de", "Top Picks for You"): "Top Picks für dich",
("de", "From your favourite Creators"): "Von deinen Lieblingsschöpfern",
("de", "Based on what you loved"): "Basierend auf dem, was du geliebt hast",
("de", "Based on what you liked"): "Basierend auf dem, was du gemocht hast",
}


def _normalize_german_formality(text: str) -> str:
"""Normalize German text to use the informal (du) address form consistently."""
text = re.sub(r"\bWeil Sie\b", "Weil du", text)
text = re.sub(r"\bwas Sie\b", "was du", text)
text = re.sub(r"\bfür Sie\b", "für dich", text)
text = re.sub(r"\bIhren\b", "deinen", text)
text = re.sub(r"\bIhrem\b", "deinem", text)
text = re.sub(r"\bIhrer\b", "deiner", text)
text = re.sub(r"\bIhres\b", "deines", text)
text = re.sub(r"\bIhre\b", "deine", text)
if text.endswith(" haben"):
text = text[:-6] + " hast"
return text
Comment on lines +18 to +30

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This function attempts to convert formal German ('Sie') to informal ('du') using regular expressions, but this approach is highly unreliable and will introduce significant grammatical errors and potentially change the meaning of sentences.

Here are a few examples of the issues:

  • Pronoun Ambiguity: Words like Sie, Ihre, and Ihren are ambiguous in German. They can refer to the formal 'you', but also 'they' or 'her'. Replacing them automatically will lead to incorrect translations. For example, a phrase like 'Das ist für sie' ('This is for them') could be incorrectly changed to 'Das ist für dich' ('This is for you').
  • Fragile Verb Conjugation: The text.endswith(' haben') check is a critical flaw. It will incorrectly change Wir haben ('we have') to Wir hast, which is grammatically incorrect. This happens because 'haben' is used for multiple pronouns ('wir', 'sie', 'Sie').

This function is very likely to make the German translations worse, not better. A much safer approach is to use the _STATIC_TRANSLATIONS dictionary to manually correct specific phrases. I strongly recommend removing this function and the call to it to avoid introducing bugs.



class TranslationService:
@alru_cache(maxsize=1000, ttl=7 * 24 * 60 * 60)
Expand All @@ -16,13 +41,20 @@ async def translate(self, text: str, target_lang: str | None) -> str:
if lang == "en":
return text

static_key = (lang, text)
if static_key in _STATIC_TRANSLATIONS:
return _STATIC_TRANSLATIONS[static_key]

try:
loop = asyncio.get_running_loop()

translated = await loop.run_in_executor(
None, lambda: GoogleTranslator(source="auto", target=lang).translate(text)
)
return translated if translated else text
result = translated if translated else text
if lang == "de":
result = _normalize_german_formality(result)
return result
except Exception as e:
logger.exception(f"Translation failed for '{text}' to '{lang}': {e}")
return text
Expand Down