实装translate!

This commit is contained in:
shenjack-mac 2023-02-03 00:05:32 +08:00
parent b52536a2c0
commit d25ddb359f
3 changed files with 29 additions and 16 deletions

View File

@ -28,9 +28,11 @@ build_version = Version("1.1.0.0") # 编译文件版本(与游戏本体无关)
DR_rust_version = Version("0.1.7.0") # DR 的 Rust 编写部分的版本 DR_rust_version = Version("0.1.7.0") # DR 的 Rust 编写部分的版本
__version__ = game_version __version__ = game_version
long_version: int = 11 long_version: int = 12
""" """
long_version: 一个用于标记内部协议的整数 long_version: 一个用于标记内部协议的整数
12: 去除 DR_runtime global_logger
logging 自己拿去
11: DR_option 添加 use_DR_rust 11: DR_option 添加 use_DR_rust
修复了一些拼写错误 修复了一些拼写错误
10: DR_runtime 添加 DR_Rust_get_version 10: DR_runtime 添加 DR_Rust_get_version
@ -114,7 +116,7 @@ class _DR_runtime(Options):
server_setup_cause_ns: int = None server_setup_cause_ns: int = None
# game runtimes # game runtimes
global_logger: logging.Logger # global_logger: logging.Logger
# game options # game options
_language = 'zh-CN' _language = 'zh-CN'

View File

@ -60,7 +60,7 @@ class Game:
self.server = server.Server(net_mode='local') self.server = server.Server(net_mode='local')
def python_version_check(self) -> None: # best 3.8+ and write at 3.8.10 def python_version_check(self) -> None: # best 3.8+ and write at 3.8.10
self.logger.info(f"{tr['main']['version.now_on']} {self.on_python_v}") self.logger.info(f"{tr().main['version.now_on']} {self.on_python_v}")
if self.on_python_v_info[0] == 2: if self.on_python_v_info[0] == 2:
self.logger.critical(f"{tr['main']['version.need3+']}") self.logger.critical(f"{tr['main']['version.need3+']}")
raise SystemError(f"{tr['main']['version.need3+']}") raise SystemError(f"{tr['main']['version.need3+']}")

View File

@ -83,9 +83,15 @@ class Translates:
elif DR_option.report_translate_not_found: elif DR_option.report_translate_not_found:
frame = inspect.currentframe() frame = inspect.currentframe()
if frame is not None: if frame is not None:
frame = frame.f_back frame = frame.f_back.f_back
if frame.f_back.f_code is not self.__getattr__.__code__: code_list = [self.__getitem__.__code__, self.__getattr__.__code__,
frame = frame.f_back self.__copy__.__code__, self.copy.__code__,
Tr.lang.__code__, Tr.__getitem__.__code__,
Tr.__call__.__code__] # 调用堆栈上的不需要的东西
while True:
if frame.f_code not in code_list: # 直到调用堆栈不是不需要的东西
break
frame = frame.f_back # 继续向上寻找
frame = f'call at {frame.f_code.co_filename}:{frame.f_lineno}' frame = f'call at {frame.f_code.co_filename}:{frame.f_lineno}'
else: else:
frame = 'but No Frame environment' frame = 'but No Frame environment'
@ -123,9 +129,11 @@ class Translates:
return self.__copy__() return self.__copy__()
def __copy__(self) -> 'Translates': def __copy__(self) -> 'Translates':
return Translates(value=self.value, config=self.config, get_list=self.get_lists) return Translates(value=self.value, config=self.config, get_list=self.get_list)
def __getattr__(self, item: key_type) -> "Translates": def __getattr__(self, item: key_type) -> "Translates":
if self.config.is_final and hasattr(self.value, item):
return getattr(self.value, item)
# 实际上我这里完全不需要处理正常需求,因为 __getattribute__ 已经帮我处理过了 # 实际上我这里完全不需要处理正常需求,因为 __getattribute__ 已经帮我处理过了
return self.__getitem__(item) return self.__getitem__(item)
@ -149,10 +157,10 @@ class Tr:
:param language: Tr 所使用的的语言 :param language: Tr 所使用的的语言
:param config: 配置 :param config: 配置
""" """
self.language_name = language or DR_runtime.language self.language_name = language if language is not None else DR_runtime.language
self.translates: Dict[str, Union[str, Dict]] = tools.load_file(f'configs/lang/{self.language_name}.toml') self.translates: Dict[str, Union[str, Dict]] = tools.load_file(f'configs/lang/{self.language_name}.toml')
self.default_translate: Dict = tools.load_file(f'configs/lang/{DR_runtime.default_language}.toml') self.default_translate: Dict = tools.load_file(f'configs/lang/{DR_runtime.default_language}.toml')
self.default_config = config.set('source', self) or 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 default(self, items: Union[str, List[str]]) -> Translates: def default(self, items: Union[str, List[str]]) -> Translates:
@ -165,13 +173,16 @@ class Tr:
return self.default_translate[items] return self.default_translate[items]
def lang(self, *items): def lang(self, *items):
return self.__getattr__(items) cache = self.translates_cache.copy()
for item in items:
cache = cache[item]
return cache
def __getattr__(self, item) -> Translates: def __getitem__(self, item: Union[str, int]) -> Translates:
return self.translates_cache[item] return self.translates_cache.copy()[item]
def __getitem__(self, item: Union[str, int]): def __call__(self, *args, **kwargs) -> Translates:
return self.__getattr__(item) return self.translates_cache.copy()
class Lang: class Lang:
@ -242,6 +253,6 @@ class Lang:
if __name__ == '__main__': if __name__ == '__main__':
tr_ = Tr() tr_ = Tr()
tr_().window.xxxx
else: tr = Tr()
tr = Lang()