进一步的 kvmd 国际化(汉化)支持,添加配置入口

yaml 配置示例:
```
languages:
    console: zh
    web: zh
```
This commit is contained in:
mofeng-git
2024-08-14 22:54:12 +08:00
parent 5b25b3661f
commit 35397c5414
47 changed files with 567 additions and 181 deletions

View File

@@ -22,6 +22,8 @@
import dataclasses
from ...languages import Languages
from ...logging import get_logger
from ... import aiotools
@@ -30,7 +32,7 @@ from ... import aiotools
# =====
class VncAuthError(Exception):
def __init__(self, path: str, lineno: int, msg: str) -> None:
super().__init__(f"Syntax error at {path}:{lineno}: {msg}")
super().__init__(Languages().gettext(f"Syntax error at {path}:{lineno}: {msg}"))
# =====
@@ -49,6 +51,7 @@ class VncAuthManager:
self.__path = path
self.__enabled = enabled
self.gettext=Languages().gettext
async def read_credentials(self) -> tuple[dict[str, VncAuthKvmdCredentials], bool]:
if self.__enabled:
@@ -57,7 +60,7 @@ class VncAuthManager:
except VncAuthError as err:
get_logger(0).error(str(err))
except Exception:
get_logger(0).exception("Unhandled exception while reading VNCAuth passwd file")
get_logger(0).exception(self.gettext("Unhandled exception while reading VNCAuth passwd file"))
return ({}, (not self.__enabled))
async def __inner_read_credentials(self) -> dict[str, VncAuthKvmdCredentials]:
@@ -68,19 +71,19 @@ class VncAuthManager:
continue
if " -> " not in line:
raise VncAuthError(self.__path, lineno, "Missing ' -> ' operator")
raise VncAuthError(self.__path, lineno, self.gettext("Missing ' -> ' operator"))
(vnc_passwd, kvmd_userpass) = map(str.lstrip, line.split(" -> ", 1))
if ":" not in kvmd_userpass:
raise VncAuthError(self.__path, lineno, "Missing ':' operator in KVMD credentials (right part)")
raise VncAuthError(self.__path, lineno, self.gettext("Missing ':' operator in KVMD credentials (right part)"))
(kvmd_user, kvmd_passwd) = kvmd_userpass.split(":")
kvmd_user = kvmd_user.strip()
if len(kvmd_user) == 0:
raise VncAuthError(self.__path, lineno, "Empty KVMD user (right part)")
raise VncAuthError(self.__path, lineno, self.gettext("Empty KVMD user (right part)"))
if vnc_passwd in credentials:
raise VncAuthError(self.__path, lineno, "Duplicating VNC password (left part)")
raise VncAuthError(self.__path, lineno, self.gettext("Duplicating VNC password (left part)"))
credentials[vnc_passwd] = VncAuthKvmdCredentials(kvmd_user, kvmd_passwd)
return credentials