e4dcfc49aa
Tests / Import Check (Python 3.13) (push) Has been cancelled
Tests / Import Check (Python 3.14) (push) Has been cancelled
Tests / Python Tests (Python 3.11) (push) Has been cancelled
Tests / Python Tests (Python 3.12) (push) Has been cancelled
Tests / Python Tests (Python 3.14) (push) Has been cancelled
Tests / Test Summary (push) Has been cancelled
Tests / Lint and Format (push) Has been cancelled
Tests / Web Node Tests (push) Has been cancelled
Tests / Import Check (Python 3.11) (push) Has been cancelled
Tests / Import Check (Python 3.12) (push) Has been cancelled
Tests / Python Tests (Python 3.13) (push) Has been cancelled
86 lines
4.0 KiB
Python
86 lines
4.0 KiB
Python
"""Small runtime i18n helper for backend-facing user messages."""
|
||
|
||
from __future__ import annotations
|
||
|
||
from typing import Any
|
||
|
||
|
||
def _parse_language(language: str | None) -> str:
|
||
raw = (language or "en").strip().lower()
|
||
if raw.startswith("zh") or raw in {"cn", "chinese"}:
|
||
return "zh"
|
||
return "en"
|
||
|
||
|
||
_MESSAGES: dict[str, dict[str, str]] = {
|
||
"en": {
|
||
"api.content_required": "content is required",
|
||
"api.invalid_channels_config": "Invalid channels config",
|
||
"api.partner_already_exists": "Partner '{name}' already exists",
|
||
"api.partner_not_found": "Partner not found",
|
||
"api.partner_not_found_or_not_running": "Partner not found or not running",
|
||
"api.partner_not_running": "Partner not running",
|
||
"api.partner_stopped_start_required": "Partner is stopped. Start it before chatting.",
|
||
"api.persona_already_exists": "Persona already exists: {name}",
|
||
"api.persona_name_required": "Persona name is required",
|
||
"api.persona_not_found": "Persona not found: {name}",
|
||
"api.soul_already_exists": "Soul '{name}' already exists",
|
||
"api.soul_content_empty": "Custom soul content is empty",
|
||
"api.soul_library_not_found": "Soul '{name}' not found in library",
|
||
"api.soul_not_found": "Soul not found",
|
||
"api.tool_not_found": "Tool '{name}' not found",
|
||
"mcp.configure_command_or_url": "Server {name!r}: configure either a command (stdio) or a url.",
|
||
"mcp.configure_before_testing": "Configure either a command (stdio) or a url before testing.",
|
||
"mcp.server_error": "Server {name!r}: {error}",
|
||
"sandbox.command_blocked": "Error: command blocked by safety guard (dangerous pattern).",
|
||
"sandbox.disabled_for_account": "Code execution is disabled for your account.",
|
||
"sandbox.no_backend": "no sandbox backend available",
|
||
},
|
||
"zh": {
|
||
"api.content_required": "content 不能为空",
|
||
"api.invalid_channels_config": "渠道配置无效",
|
||
"api.partner_already_exists": "伙伴 '{name}' 已存在",
|
||
"api.partner_not_found": "未找到伙伴",
|
||
"api.partner_not_found_or_not_running": "未找到伙伴或伙伴未运行",
|
||
"api.partner_not_running": "伙伴未运行",
|
||
"api.partner_stopped_start_required": "伙伴已停止。请先启动后再聊天。",
|
||
"api.persona_already_exists": "Persona 已存在:{name}",
|
||
"api.persona_name_required": "Persona 名称不能为空",
|
||
"api.persona_not_found": "未找到 Persona:{name}",
|
||
"api.soul_already_exists": "Soul '{name}' 已存在",
|
||
"api.soul_content_empty": "自定义 soul 内容为空",
|
||
"api.soul_library_not_found": "素材库中未找到 soul '{name}'",
|
||
"api.soul_not_found": "未找到 soul",
|
||
"api.tool_not_found": "未找到工具 '{name}'",
|
||
"mcp.configure_command_or_url": "服务器 {name!r}:请配置 command(stdio)或 url。",
|
||
"mcp.configure_before_testing": "测试前请先配置 command(stdio)或 url。",
|
||
"mcp.server_error": "服务器 {name!r}:{error}",
|
||
"sandbox.command_blocked": "错误:命令被安全防护拦截(匹配危险模式)。",
|
||
"sandbox.disabled_for_account": "你的账号已禁用代码执行。",
|
||
"sandbox.no_backend": "没有可用的沙箱后端",
|
||
},
|
||
}
|
||
|
||
|
||
def current_language(default: str = "en") -> str:
|
||
try:
|
||
from deeptutor.services.settings.interface_settings import get_ui_language
|
||
|
||
return _parse_language(get_ui_language(default=default))
|
||
except Exception:
|
||
return _parse_language(default)
|
||
|
||
|
||
def t(key: str, default: str = "", *, language: str | None = None, **kwargs: Any) -> str:
|
||
lang = _parse_language(language) if language else current_language()
|
||
text = _MESSAGES.get(lang, {}).get(key) or _MESSAGES["en"].get(key) or default
|
||
if kwargs:
|
||
try:
|
||
return text.format(**kwargs)
|
||
except (KeyError, IndexError, ValueError):
|
||
return text
|
||
return text
|
||
|
||
|
||
__all__ = ["current_language", "t"]
|