进一步的 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

@@ -105,7 +105,9 @@ from ..validators.hw import valid_otg_gadget
from ..validators.hw import valid_otg_id
from ..validators.hw import valid_otg_ethernet
from ..lanuages import Lanuages
from ..validators.languages import valid_languages
from ..languages import Languages
# =====
def init(
@@ -127,16 +129,16 @@ def init(
add_help=add_help,
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
_ = translation(domain="message",localedir="/kvmd/i18n",languages=["zh"]).gettext
parser.add_argument("-c", "--config", default="/etc/kvmd/main.yaml", type=valid_abs_file,
help=_("Set config file path"), metavar="<file>")
help="Set config file path", metavar="<file>")
parser.add_argument("-o", "--set-options", default=[], nargs="+",
help=_("Override config options list (like sec/sub/opt=value)"), metavar="<k=v>",)
help="Override config options list (like sec/sub/opt=value)", metavar="<k=v>",)
parser.add_argument("-m", "--dump-config", action="store_true",
help=_("View current configuration (include all overrides)"))
help="View current configuration (include all overrides)")
if check_run:
parser.add_argument("--run", dest="run", action="store_true",
help=_("Run the service"))
help="Run the service")
(options, remaining) = parser.parse_known_args(argv)
if options.dump_config:
@@ -151,9 +153,18 @@ def init(
))
raise SystemExit()
config = _init_config(options.config, options.set_options, **load)
logging.captureWarnings(True)
logging.config.dictConfig(config.logging)
if isinstance(config.get("languages"), dict) and isinstance(config["languages"].get("console"), str):
i18n_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+"/i18n"
Languages.init("message", i18n_path, config["languages"]["console"])
gettext = Languages().gettext
logging.addLevelName(20, gettext("INFO"))
logging.addLevelName(30, gettext("WARNING"))
logging.addLevelName(40, gettext("ERROR"))
if cli_logging:
logging.getLogger().handlers[0].setFormatter(logging.Formatter(
"-- {levelname:>7} -- {message}",
@@ -162,7 +173,7 @@ def init(
if check_run and not options.run:
raise SystemExit(
_("To prevent accidental startup, you must specify the --run option to start.\n")+_("Try the --help option to find out what this service does.\n")+_("Make sure you understand exactly what you are doing!"))
gettext("To prevent accidental startup, you must specify the --run option to start.\n")+gettext("Try the --help option to find out what this service does.\n")+gettext("Make sure you understand exactly what you are doing!"))
return (parser, remaining, config)
@@ -787,4 +798,9 @@ def _get_config_scheme() -> dict:
"timeout": Option(300, type=valid_int_f1),
"interval": Option(30, type=valid_int_f1),
},
"languages": {
"console": Option("default", type=valid_languages),
"web": Option("default", type=valid_languages),
},
}