from fastapi import Request from typing import Dict, Any, Optional # Translation dictionaries TRANSLATIONS = { "en": { "job_not_found": "Job not found", "task_not_found": "Task not found", "invalid_extension": "Invalid file extension: {ext}. Allowed: {allowed}", "single_file_only": "Only one file allowed per task", "job_not_completed": "Job not completed. Current status: {status}", "results_not_found": "Results not found on disk", "job_deleted": "Job {job_id} deleted successfully", "task_not_in_queue": "Task is not in queue", "use_create_endpoint": "Use POST /api/v1/jobs/create for now", "upload_endpoint": "Upload endpoint", "queue_update_triggered": "Queue update triggered" }, "zh": { "job_not_found": "未找到任务", "task_not_found": "未找到任务", "invalid_extension": "文件后缀无效: {ext}。允许: {allowed}", "single_file_only": "每个任务仅允许上传一个文件", "job_not_completed": "任务未完成。当前状态: {status}", "results_not_found": "未在磁盘上找到结果文件", "job_deleted": "任务 {job_id} 已成功删除", "task_not_in_queue": "任务不在队列中", "use_create_endpoint": "请使用 POST /api/v1/jobs/create 接口", "upload_endpoint": "上传接口", "queue_update_triggered": "已触发队列更新" } } DEFAULT_LOCALE = "en" def get_locale(request: Request) -> str: """ Get locale from Accept-Language header. Simple implementation: checks if 'zh' is in the header. """ accept_language = request.headers.get("accept-language", "") if "zh" in accept_language.lower(): return "zh" return "en" class I18n: def __init__(self, request: Request): self.locale = get_locale(request) def t(self, key: str, **kwargs) -> str: """ Translate a key. If key not found, return key. Supports string formatting with kwargs. """ messages = TRANSLATIONS.get(self.locale, TRANSLATIONS[DEFAULT_LOCALE]) message = messages.get(key, TRANSLATIONS[DEFAULT_LOCALE].get(key, key)) if kwargs: try: return message.format(**kwargs) except KeyError: return message return message async def get_i18n(request: Request) -> I18n: """Dependency for getting I18n instance""" return I18n(request)