++
This commit is contained in:
@@ -1,9 +1,35 @@
|
||||
from fastapi import APIRouter
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, Request
|
||||
|
||||
from app.presets import PRESETS
|
||||
from app.services.i18n import DEFAULT_LOCALE, locale_from_request
|
||||
|
||||
router = APIRouter(prefix="/api")
|
||||
|
||||
|
||||
def _localize(value, locale: str):
|
||||
"""Recursively unwrap {en, zh} dicts to the requested locale.
|
||||
|
||||
Falls back to the default locale, then to the first available value.
|
||||
Strings (and other scalars) are returned as-is.
|
||||
"""
|
||||
if isinstance(value, dict):
|
||||
# Heuristic: dicts with only "en"/"zh" keys are translation maps.
|
||||
keys = set(value.keys())
|
||||
if keys <= {"en", "zh"}:
|
||||
if locale in value:
|
||||
return value[locale]
|
||||
if DEFAULT_LOCALE in value:
|
||||
return value[DEFAULT_LOCALE]
|
||||
return next(iter(value.values()))
|
||||
return {k: _localize(v, locale) for k, v in value.items()}
|
||||
if isinstance(value, list):
|
||||
return [_localize(v, locale) for v in value]
|
||||
return value
|
||||
|
||||
|
||||
@router.get("/presets")
|
||||
async def get_presets():
|
||||
return {"presets": PRESETS}
|
||||
async def get_presets(request: Request):
|
||||
locale = locale_from_request(request)
|
||||
return {"presets": _localize(PRESETS, locale)}
|
||||
Reference in New Issue
Block a user