Initial commit - Migrated to Dev environment
This commit is contained in:
46
backend/app/services/translation_service.py
Executable file
46
backend/app/services/translation_service.py
Executable file
@@ -0,0 +1,46 @@
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select, update
|
||||
from app.models.translation import Translation
|
||||
from typing import Dict
|
||||
|
||||
class TranslationService:
|
||||
# Ez a memória-cache tárolja az élesített szövegeket
|
||||
_published_cache: Dict[str, Dict[str, str]] = {}
|
||||
|
||||
@classmethod
|
||||
async def load_cache(cls, db: AsyncSession):
|
||||
"""Betölti az összes PUBLIKÁLT fordítást az adatbázisból a memóriába."""
|
||||
result = await db.execute(
|
||||
select(Translation).where(Translation.is_published == True)
|
||||
)
|
||||
translations = result.scalars().all()
|
||||
|
||||
cls._published_cache = {}
|
||||
for t in translations:
|
||||
if t.lang_code not in cls._published_cache:
|
||||
cls._published_cache[t.lang_code] = {}
|
||||
cls._published_cache[t.lang_code][t.key] = t.value
|
||||
print(f"🌍 i18n Cache: {len(translations)} szöveg élesítve.")
|
||||
|
||||
@classmethod
|
||||
def get_text(cls, key: str, lang: str = "en") -> str:
|
||||
"""Villámgyors lekérés a memóriából Fallback logikával."""
|
||||
# 1. Kért nyelv
|
||||
text = cls._published_cache.get(lang, {}).get(key)
|
||||
if text: return text
|
||||
|
||||
# 2. Fallback: Angol
|
||||
if lang != "en":
|
||||
text = cls._published_cache.get("en", {}).get(key)
|
||||
if text: return text
|
||||
|
||||
return f"[{key}]"
|
||||
|
||||
@classmethod
|
||||
async def publish_all(cls, db: AsyncSession):
|
||||
"""Élesíti a piszkozatokat és frissíti a szerver memóriáját."""
|
||||
await db.execute(
|
||||
update(Translation).where(Translation.is_published == False).values(is_published=True)
|
||||
)
|
||||
await db.commit()
|
||||
await cls.load_cache(db)
|
||||
Reference in New Issue
Block a user