Переключение языка по аргументу Accept-Language в запросе
This commit is contained in:
parent
44dab7402d
commit
200d47754c
5 changed files with 347 additions and 36 deletions
|
|
@ -1,11 +1,14 @@
|
|||
import os
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
import polib
|
||||
from docutils.nodes import entry
|
||||
from fastapi import APIRouter, HTTPException, Depends
|
||||
from starlette.responses import FileResponse
|
||||
from services.settings_service import SettingsService
|
||||
from utils.i18n import get_translations, _
|
||||
from babel.support import Translations
|
||||
from models.request import LanguageRequest
|
||||
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
|
@ -138,4 +141,28 @@ async def export_wifi(translations: Translations = Depends(get_translations("api
|
|||
path=WIFI_PATH,
|
||||
filename="wifi.json",
|
||||
media_type="application/json"
|
||||
)
|
||||
)
|
||||
|
||||
@router.get("/{locale}/{domain}")
|
||||
async def get_translations(locale: str, domain: str):
|
||||
"""
|
||||
|
||||
Args:
|
||||
locale:
|
||||
domain:
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
try:
|
||||
po_file = Path(f"locales/{locale}/LC_MESSAGES/{domain}.po")
|
||||
if not po_file.exists():
|
||||
logger.warning(f"Файл интернационализации для {locale} и домена {domain} не найдены")
|
||||
raise HTTPException(status_code=404, detail=f"Файл интернационализации для {locale} и домена {domain} не найдены")
|
||||
po = polib.pofile(str(po_file))
|
||||
translations = {entry.msgid: entry.msgstr or entry.msgid for entry in po}
|
||||
logger.info(f"Загрузка {len(translations)} перевода для локали {locale} и домена {domain}")
|
||||
return translations
|
||||
except Exception as e:
|
||||
logger.error(f"Ошибка загрузки перевода для локали {locale} и домена {domain}: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail=f"Ошибка на сервере: {str(e)}")
|
||||
Loading…
Add table
Add a link
Reference in a new issue