You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

35 lines
1.1 KiB

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(request: Request):
locale = locale_from_request(request)
return {"presets": _localize(PRESETS, locale)}