清理没必要的东西+规范Tr用法
This commit is contained in:
parent
b481d8e012
commit
419eea20c8
@ -119,7 +119,7 @@ class _DR_runtime(Options):
|
|||||||
# global_logger: logging.Logger
|
# global_logger: logging.Logger
|
||||||
|
|
||||||
# game options
|
# game options
|
||||||
_language = 'zh-CN'
|
language: str = 'zh-CN'
|
||||||
default_language: str = 'zh-CN'
|
default_language: str = 'zh-CN'
|
||||||
|
|
||||||
def init(self, **kwargs) -> None:
|
def init(self, **kwargs) -> None:
|
||||||
@ -131,25 +131,6 @@ class _DR_runtime(Options):
|
|||||||
warnings.warn(f'DR_rust builtin version is {self.DR_Rust_version} but true version is {get_version_str()}.\n'
|
warnings.warn(f'DR_rust builtin version is {self.DR_Rust_version} but true version is {get_version_str()}.\n'
|
||||||
f'Builtin version {relationship} than true version')
|
f'Builtin version {relationship} than true version')
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
|
||||||
self.__options = {'language': self.language}
|
|
||||||
super().__init__(**kwargs)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def language(self):
|
|
||||||
return self._language
|
|
||||||
|
|
||||||
@language.setter
|
|
||||||
def language(self, value: str):
|
|
||||||
if value == self._language:
|
|
||||||
return
|
|
||||||
assert isinstance(value, str), "DR language MUST be string"
|
|
||||||
self._language = value
|
|
||||||
from Difficult_Rocket.utils import translate
|
|
||||||
translate.tr._update_lang()
|
|
||||||
|
|
||||||
|
|
||||||
_DR_runtime.add_option('language', _DR_runtime.language)
|
|
||||||
|
|
||||||
|
|
||||||
DR_option = _DR_option()
|
DR_option = _DR_option()
|
||||||
|
@ -89,11 +89,11 @@ class Client:
|
|||||||
resizable=tools.format_bool(self.config['window']['resizable']),
|
resizable=tools.format_bool(self.config['window']['resizable']),
|
||||||
visible=tools.format_bool(self.config['window']['visible']),
|
visible=tools.format_bool(self.config['window']['visible']),
|
||||||
file_drops=file_drop)
|
file_drops=file_drop)
|
||||||
self.logger.info(tr().client.setup.done)
|
self.logger.info(tr().client.setup.done())
|
||||||
end_time = time.time_ns()
|
end_time = time.time_ns()
|
||||||
self.use_time = end_time - start_time
|
self.use_time = end_time - start_time
|
||||||
self.logger.info(tr().client.setup.use_time.format(Decimal(self.use_time) / 1000000000))
|
self.logger.info(tr().client.setup.use_time().format(Decimal(self.use_time) / 1000000000))
|
||||||
self.logger.debug(tr().client.setup.use_time_ns.format(self.use_time))
|
self.logger.debug(tr().client.setup.use_time_ns().format(self.use_time))
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
DR_runtime.running = True
|
DR_runtime.running = True
|
||||||
@ -295,7 +295,7 @@ class ClientWindow(Window):
|
|||||||
|
|
||||||
@_call_screen_after
|
@_call_screen_after
|
||||||
def on_command(self, command: line.CommandText):
|
def on_command(self, command: line.CommandText):
|
||||||
self.logger.info(tr().window.command.text.format(command))
|
self.logger.info(tr().window.command.text().format(command))
|
||||||
if command.re_match('stop'):
|
if command.re_match('stop'):
|
||||||
# self.dispatch_event('on_exit')
|
# self.dispatch_event('on_exit')
|
||||||
pyglet.app.platform_event_loop.stop()
|
pyglet.app.platform_event_loop.stop()
|
||||||
@ -316,7 +316,7 @@ class ClientWindow(Window):
|
|||||||
|
|
||||||
@_call_screen_after
|
@_call_screen_after
|
||||||
def on_message(self, message: line.CommandLine.text):
|
def on_message(self, message: line.CommandLine.text):
|
||||||
self.logger.info(tr().window.message.text.format(message))
|
self.logger.info(tr().window.message.text().format(message))
|
||||||
|
|
||||||
"""
|
"""
|
||||||
keyboard and mouse input
|
keyboard and mouse input
|
||||||
@ -400,16 +400,16 @@ class ClientWindow(Window):
|
|||||||
@_call_screen_after
|
@_call_screen_after
|
||||||
def on_text_motion(self, motion):
|
def on_text_motion(self, motion):
|
||||||
motion_string = key.motion_string(motion)
|
motion_string = key.motion_string(motion)
|
||||||
self.logger.debug(tr().window.text.motion.format(motion_string)())
|
self.logger.debug(tr().window.text.motion().format(motion_string)())
|
||||||
|
|
||||||
@_call_screen_after
|
@_call_screen_after
|
||||||
def on_text_motion_select(self, motion):
|
def on_text_motion_select(self, motion):
|
||||||
motion_string = key.motion_string(motion)
|
motion_string = key.motion_string(motion)
|
||||||
self.logger.debug(tr().window.text.motion_select.format(motion_string)())
|
self.logger.debug(tr().window.text.motion_select().format(motion_string)())
|
||||||
|
|
||||||
@_call_screen_before
|
@_call_screen_before
|
||||||
def on_close(self, source: str = 'window') -> None:
|
def on_close(self, source: str = 'window') -> None:
|
||||||
self.logger.info(tr().window.game.stop_get().format(tr().game[source]))
|
self.logger.info(tr().window.game.stop_get().format(tr().game[source]()))
|
||||||
self.logger.info(tr().window.game.stop())
|
self.logger.info(tr().window.game.stop())
|
||||||
self.fps_log.check_list = False
|
self.fps_log.check_list = False
|
||||||
DR_runtime.running = False
|
DR_runtime.running = False
|
||||||
|
@ -120,10 +120,7 @@ class Translates:
|
|||||||
return self
|
return self
|
||||||
|
|
||||||
def __call__(self, *args, **kwargs) -> Union[dict, list, int, str]:
|
def __call__(self, *args, **kwargs) -> Union[dict, list, int, str]:
|
||||||
if len(self.get_list) == 0:
|
return self.__str__()
|
||||||
return self.__str__()
|
|
||||||
if any([x[0] for x in self.get_list]):
|
|
||||||
return self.__str__()
|
|
||||||
|
|
||||||
def copy(self):
|
def copy(self):
|
||||||
return self.__copy__()
|
return self.__copy__()
|
||||||
@ -165,6 +162,9 @@ class Tr:
|
|||||||
self.default_config = config.set('source', self) if config is not None else TranslateConfig(source=self)
|
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())
|
self.translates_cache = Translates(value=self.translates, config=self.default_config.copy())
|
||||||
|
|
||||||
|
def update_lang(self) -> bool:
|
||||||
|
if
|
||||||
|
|
||||||
def default(self, items: Union[str, List[str]]) -> Translates:
|
def default(self, items: Union[str, List[str]]) -> Translates:
|
||||||
if isinstance(items, list):
|
if isinstance(items, list):
|
||||||
cache_translate = self.default_translate
|
cache_translate = self.default_translate
|
||||||
@ -187,74 +187,4 @@ class Tr:
|
|||||||
return self.translates_cache.copy()
|
return self.translates_cache.copy()
|
||||||
|
|
||||||
|
|
||||||
class Lang:
|
tr = Tr()
|
||||||
"""
|
|
||||||
用于创建一个对应语言的翻译类
|
|
||||||
感谢Fallen的MCDR提供idea
|
|
||||||
https://github.com/Fallen-Breath/MCDReforged
|
|
||||||
可以用
|
|
||||||
lang['language'] = 'abc' 或
|
|
||||||
lang['lang'] = 'abc'
|
|
||||||
的方式直接更改并刷新翻译
|
|
||||||
用
|
|
||||||
lang.lang(xxx, xxx)来获取翻译过的值
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self) -> None:
|
|
||||||
self.translates = tools.load_file(f'configs/lang/{DR_runtime.language}.toml')
|
|
||||||
self.default_translates = tools.load_file('configs/lang/zh-CN.toml')
|
|
||||||
self.直接返回原始数据 = True
|
|
||||||
|
|
||||||
def __str__(self) -> str:
|
|
||||||
return DR_option.language
|
|
||||||
|
|
||||||
def __getitem__(self, item) -> Union[int, str, list, dict]:
|
|
||||||
try:
|
|
||||||
return self.translates[item]
|
|
||||||
except KeyError:
|
|
||||||
try:
|
|
||||||
return self.default_translates[item]
|
|
||||||
except KeyError:
|
|
||||||
raise TranslateKeyNotFound
|
|
||||||
# raise TranslateKeyNotFound(f'there\'s no key {item} in both {DR_option.language} and zh-CN')
|
|
||||||
|
|
||||||
def lang(self, *args) -> Union[int, str, list, dict, tuple]:
|
|
||||||
# frame = inspect.currentframe()
|
|
||||||
# # print("调用当前log的文件名:", frame.f_back.f_code.co_filename)
|
|
||||||
# objprint.objprint(frame.f_back.f_code,
|
|
||||||
# honor_existing=False,
|
|
||||||
# depth=2)
|
|
||||||
try:
|
|
||||||
result = self.translates
|
|
||||||
for option in args:
|
|
||||||
result = result[option]
|
|
||||||
return result
|
|
||||||
except KeyError:
|
|
||||||
try:
|
|
||||||
result = self.default_translates
|
|
||||||
for option in args:
|
|
||||||
result = result[option]
|
|
||||||
return result
|
|
||||||
except KeyError as e:
|
|
||||||
if self.直接返回原始数据:
|
|
||||||
return args
|
|
||||||
raise TranslateKeyNotFound from e
|
|
||||||
# raise TranslateKeyNotFound(f'there\'s no key {args} in both {DR_option.language} and zh-CN') from e
|
|
||||||
|
|
||||||
def 翻译(self, *args) -> Union[int, str, list, dict]:
|
|
||||||
return self.lang(args)
|
|
||||||
|
|
||||||
def _update_lang(self) -> str:
|
|
||||||
"""
|
|
||||||
用于更新语言(内部调用)
|
|
||||||
:return: 设置完成后的语言
|
|
||||||
"""
|
|
||||||
self.translates = tools.load_file(f'configs/lang/{DR_option.language}.toml')
|
|
||||||
return DR_option.language
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
tr_ = Tr()
|
|
||||||
tr_().window.xxxx
|
|
||||||
|
|
||||||
tr = Tr()
|
|
||||||
|
Loading…
Reference in New Issue
Block a user