diff --git a/Difficult_Rocket/client/__init__.py b/Difficult_Rocket/client/__init__.py index 7c7c477..91f9bf2 100644 --- a/Difficult_Rocket/client/__init__.py +++ b/Difficult_Rocket/client/__init__.py @@ -40,6 +40,7 @@ from Difficult_Rocket import DR_runtime, DR_option from Difficult_Rocket.utils.new_thread import new_thread from Difficult_Rocket.client.fps.fps_log import FpsLogger from Difficult_Rocket.client.guis.widgets import InputBox +from Difficult_Rocket.exception.language import LanguageNotFound from Difficult_Rocket.exception.command import CommandError from Difficult_Rocket.client.render.sr1_ship import SR1ShipRender from Difficult_Rocket.client.screen import BaseScreen, DRScreen, DRDEBUGScreen @@ -229,12 +230,13 @@ class ClientWindow(Window): @new_thread('window save_info') def save_info(self): - self.logger.info('save_info start') + self.logger.info(tr().client.save_info.start()) config_file = tools.load_file('./configs/main.toml') config_file['window']['width'] = self.width config_file['window']['height'] = self.height + config_file['runtime']['language'] = DR_runtime.language rtoml.dump(config_file, open('./configs/main.toml', 'w')) - self.logger.info('save_info end') + self.logger.info(tr().client.save_info.done()) """ draws and some event @@ -306,6 +308,15 @@ class ClientWindow(Window): elif command.re_match('default'): self.set_size(int(self.main_config['window_default']['width']), int(self.main_config['window_default']['height'])) + elif command.re_match('lang'): + try: + lang = command.text[5:] + tr._language = lang + self.logger.info(tr().language_set_to()) + except LanguageNotFound: + self.logger.info(tr().language_available().format(os.listdir('./configs/lang'))) + self.save_info() + # self.command_tree.parse(command.plain_command) @_call_screen_after diff --git a/Difficult_Rocket/client/render/sr1_ship.py b/Difficult_Rocket/client/render/sr1_ship.py index 1f1aed9..b517203 100644 --- a/Difficult_Rocket/client/render/sr1_ship.py +++ b/Difficult_Rocket/client/render/sr1_ship.py @@ -363,7 +363,6 @@ class SR1ShipRender(BaseScreen): if self.load_xml(path): # 加载成功一个就停下 break self.render_ship() - print(paths) if __name__ == '__main__': diff --git a/Difficult_Rocket/command/api.py b/Difficult_Rocket/command/api.py index dec8467..0ddbf27 100644 --- a/Difficult_Rocket/command/api.py +++ b/Difficult_Rocket/command/api.py @@ -14,7 +14,7 @@ gitee: @shenjackyuanjie # system function import re -from typing import Union, Optional, Type, Tuple, List +from typing import Union, Optional # DR # from Difficult_Rocket.exception.command import * @@ -31,6 +31,7 @@ class CommandText: self.plain_command = text self.text = text self.error = False + self.value_list = [] self.value_dict = {} def counter(self, start: Optional[int] = 0) -> int: diff --git a/Difficult_Rocket/exception/language.py b/Difficult_Rocket/exception/language.py index 1b68ed2..269f2aa 100644 --- a/Difficult_Rocket/exception/language.py +++ b/Difficult_Rocket/exception/language.py @@ -9,7 +9,7 @@ from Difficult_Rocket.exception import BaseError __all__ = ['LanguageNotFound', 'TranslateError', 'TranslateKeyNotFound', - 'TranslateFileNotFound'] + 'TranslateFileError'] class LanguageNotFound(BaseError): @@ -30,8 +30,8 @@ class TranslateKeyNotFound(TranslateError): return f"{self.__class__.__name__}: Can't get item {'. '.join(self.item_names)} from: {self.value}" -class TranslateFileNotFound(TranslateError): - """翻译文件缺失""" +class TranslateFileError(TranslateError): + """翻译文件错误""" diff --git a/Difficult_Rocket/main.py b/Difficult_Rocket/main.py index 873a582..ab26c2a 100644 --- a/Difficult_Rocket/main.py +++ b/Difficult_Rocket/main.py @@ -50,6 +50,7 @@ class Game: logging.config.dictConfig(log_config) self.logger = logging.getLogger('main') self.logger.info(tr().main.logger.mkdir()) + self.logger.info(tr().language_set_to()) self.logger.info(tr().main.logger.created()) # version check self.python_version_check() diff --git a/Difficult_Rocket/utils/translate.py b/Difficult_Rocket/utils/translate.py index 03632de..cbb6168 100644 --- a/Difficult_Rocket/utils/translate.py +++ b/Difficult_Rocket/utils/translate.py @@ -11,6 +11,7 @@ github: @shenjackyuanjie gitee: @shenjackyuanjie """ +import os import inspect from dataclasses import dataclass @@ -18,7 +19,8 @@ from typing import Union, Tuple, Any, List, Dict, Hashable, Optional from Difficult_Rocket import DR_runtime, DR_option from Difficult_Rocket.utils import tools -from Difficult_Rocket.exception.language import * +from Difficult_Rocket.exception.language import (LanguageNotFound, + TranslateKeyNotFound) @dataclass @@ -162,10 +164,35 @@ class Tr: self.default_config = config.set('source', self) if config is not None else TranslateConfig(source=self) self.translates_cache = Translates(value=self.translates, config=self.default_config.copy()) - def init_translate(self): - self.translates: Dict[str, Union[str, Dict]] = tools.load_file(f'configs/lang/{self.language_name}.toml') + @property + def _language(self) -> str: + return self.language_name + + @_language.setter + def _language(self, value: str): + self.init_translate(value) + + def init_translate(self, lang: Optional[str] = None) -> bool: + """ + 初始化语言文件 + :param lang: 要初始化的语言 + :return: + """ + # 首先判定是否存在对应的语言文件 + if lang == self.language_name: + return False + if lang == ' ' or lang == '': + raise LanguageNotFound('Can not be empty') + lang = lang or self.language_name + if not os.path.exists(f'./configs/lang/{lang}.toml'): + print(f"lang: {os.path.exists(f'./configs/lang/{lang}.toml')} language = {lang} {self.language_name=}") + raise LanguageNotFound(lang) + self.translates: Dict[str, Union[str, Dict]] = tools.load_file(f'configs/lang/{lang}.toml') self.default_translate: Dict = tools.load_file(f'configs/lang/{DR_runtime.default_language}.toml') self.translates_cache = Translates(value=self.translates, config=self.default_config.copy()) + self.language_name = lang + DR_runtime.language = self.language_name + return True def update_lang(self) -> bool: if DR_runtime.language != self.language_name: diff --git a/configs/lang/en-us.toml b/configs/lang/en-us.toml index c50e07e..3d523fa 100644 --- a/configs/lang/en-us.toml +++ b/configs/lang/en-us.toml @@ -1,4 +1,6 @@ language = "English" +language_set_to = "Language set to: English" +language_available = "Available languages list: {}" [main] version.now_on = "Difficult Rocket is running on Python Vision" @@ -19,6 +21,8 @@ game_start.at = "The main thread of the game starts with : " setup.start = "Client start loading" setup.use_time = "Client loading has used: {} second" setup.use_time_ns = "Client loading has used: {} nano second" +config.save.start = "Saving config……" +config.save.done = "Config saved" [window] setup.start = "Window start loading" diff --git a/configs/lang/zh-CN.toml b/configs/lang/zh-CN.toml index f2367f7..1198c71 100644 --- a/configs/lang/zh-CN.toml +++ b/configs/lang/zh-CN.toml @@ -1,4 +1,6 @@ language = "简体中文" +language_set_to = "语言已经设置为:简体中文" +language_available = "可用语言列表: {}" [main] version.now_on = "困难火箭的运行Python环境为" @@ -19,6 +21,8 @@ game_start.at = "游戏主线程开始于:" setup.start = "客户端加载开始" setup.use_time = "客户端载入花费: {} 秒" setup.use_time_ns = "客户端载入花费: {} 纳秒" +config.save.start = "正在保存配置文件" +config.save.done = "配置文件保存完成" [window] setup.start = "游戏窗口加载开始" diff --git a/docs/src/update_logs.md b/docs/src/update_logs.md index e42ad5a..3942330 100644 --- a/docs/src/update_logs.md +++ b/docs/src/update_logs.md @@ -22,6 +22,50 @@ - [![Readme-gitee](https://img.shields.io/badge/Readme-中文(点我!)-blue.svg?style=flat-square)](../../README.md) - Using [SemVer 2.0.0](https://semver.org/) to manage version +## 20230405 DR `0.7.2.0` + +### Translate + +- 添加 + - `language_set_to`: 设置到对应语言时输出 + - Display when setting to the corresponding language + - `language_available`: 可用的语言列表 + - List of available languages + - `client` + - `config.save.start` + - `config.save.done` +- `Difficult_Rocket.utils.translate` + - `Tr` + - 添加了 `_language`(`@property`) 字段 + - 用于存储和修改语言 + - Added the `_language`(`@property`) field + - Used to store and modify languages + - `init_translate` + - 添加了 `lang` 参数 + - 用于设置语言 + - Added the `lang` parameter + - Used to set the language + +### Exception + +- `language` + - `TranslateFileNotFound` -> `TranslateFileError` + +### Command + +- `lang ` + - 添加了 `lang` 命令 + - 直接调用时,会显示可用的语言列表 + - 用法:`lang ` + - Added the `lang` command + - When called directly, the available language list will be displayed + - Usage: `lang ` + +### Changes + +- 现在很多输出信息的地方会使用 i18n + - Now many places that output information will use i18n + ## 20230302 DR `0.7.1.5` + Build `1.2.1.0` + DR_rs `0.2.5.6`~`0.2.6.0` + DR_api `0.0.1.0` ### 文档