Difficult-Rocket/Difficult_Rocket/utils/translate.py

137 lines
3.6 KiB
Python
Raw Normal View History

2021-09-08 23:38:34 +08:00
# -------------------------------
# Difficult Rocket
2022-06-27 16:51:14 +08:00
# Copyright © 2021-2022 by shenjackyuanjie 3695888@qq.com
2021-09-08 23:38:34 +08:00
# All rights reserved
# -------------------------------
2021-09-02 22:47:10 +08:00
"""
writen by shenjackyuanjie
mail: 3695888@qq.com
github: @shenjackyuanjie
gitee: @shenjackyuanjie
"""
2022-07-01 13:59:08 +08:00
import inspect
from typing import Union
2022-08-12 21:07:36 +08:00
from Difficult_Rocket import DR_runtime, DR_option
2022-04-26 22:05:58 +08:00
from Difficult_Rocket.utils import tools
2022-07-04 10:36:19 +08:00
from Difficult_Rocket.exception.language import *
"""
这部分代码使用了中文编程why
你觉得呢
"""
2021-09-02 22:47:10 +08:00
2022-07-01 13:59:08 +08:00
class Tr:
"""
2022-08-16 13:25:44 +08:00
我不装了我就抄了tr
GOOD
2022-07-01 13:59:08 +08:00
"""
2022-09-04 13:53:09 +08:00
2022-07-01 13:59:08 +08:00
def __init__(self):
2022-07-04 10:36:19 +08:00
self.config_regs = {}
2022-07-01 13:59:08 +08:00
2022-07-04 10:36:19 +08:00
def add_config(self, configs: dict) -> None:
frame = inspect.currentframe()
self.config_regs[frame.f_back.f_code.co_filename] = configs
def __call__(self, *args, **kwargs):
frame = inspect.currentframe()
if frame.f_back.f_code.co_filename in self.config_regs:
...
else:
...
2022-07-01 13:59:08 +08:00
2021-09-02 22:47:10 +08:00
class Lang:
"""
用于创建一个对应语言的翻译类
感谢Fallen的MCDR提供idea
https://github.com/Fallen-Breath/MCDReforged
可以用
lang['language'] = 'abc'
lang['lang'] = 'abc'
的方式直接更改并刷新翻译
lang.lang(xxx, xxx)来获取翻译过的值
2021-09-02 22:47:10 +08:00
"""
2021-09-05 00:50:05 +08:00
2022-08-12 21:07:36 +08:00
def __init__(self) -> None:
self.翻译结果 = tools.load_file(f'configs/lang/{DR_runtime.language}.toml')
self.默认翻译 = tools.load_file('configs/lang/zh-CN.toml')
2022-06-14 20:52:03 +08:00
self.直接返回原始数据 = True
2021-09-02 22:47:10 +08:00
def __str__(self) -> str:
2022-08-12 21:07:36 +08:00
return DR_option.language
2021-09-02 22:47:10 +08:00
2021-10-22 06:36:06 +08:00
def __getitem__(self, item) -> Union[int, str, list, dict]:
try:
return self.翻译结果[item]
except KeyError:
try:
return self.默认翻译[item]
except KeyError:
2022-08-12 21:07:36 +08:00
raise TranslateKeyNotFound(f'there\'s no key {item} in both {DR_option.language} and zh-CN')
2021-10-22 06:36:06 +08:00
def lang(self, *args) -> Union[int, str, list, dict]:
2022-07-04 10:36:19 +08:00
# 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:
结果 = self.翻译结果
for 选项 in args:
结果 = 结果[选项]
return 结果
except KeyError:
try:
结果 = self.默认翻译
for 选项 in args:
结果 = 结果[选项]
return 结果
except KeyError:
2022-06-14 20:52:03 +08:00
if self.直接返回原始数据:
return args
2022-08-12 21:07:36 +08:00
raise TranslateKeyNotFound(f'there\'s no key {args} in both {DR_option.language} and zh-CN')
2021-10-22 06:36:06 +08:00
def 翻译(self, *args) -> Union[int, str, list, dict]:
return self.lang(args)
2022-08-12 21:07:36 +08:00
def _update_lang(self) -> str:
"""
用于更新语言(内部调用)
@return: 设置完成后的语言
"""
self.翻译结果 = tools.load_file(f'configs/lang/{DR_option.language}.toml')
return DR_option.language
2022-08-12 21:07:36 +08:00
if not __name__ == '__main__':
tr = Lang()
2021-10-01 23:12:01 +08:00
# font's value
HOS = 'HarmonyOS Sans'
HOS_S = 'HarmonyOS Sans SC'
HOS_T = 'HarmonyOS Sans TC'
HOS_C = 'HarmonyOS Sans Condensed'
2021-10-01 23:12:01 +08:00
鸿蒙字体 = HOS
鸿蒙简体 = HOS_S
鸿蒙繁体 = HOS_T
鸿蒙窄体 = HOS_C
2021-11-06 19:07:32 +08:00
CC = 'Cascadia Code'
CM = 'Cascadia Mono'
CCPL = 'Cascadia Code PL'
CMPL = 'Cascadia Mono PL'
微软等宽 = CC
微软等宽无线 = CM
微软等宽带电线 = CCPL
微软等宽带电线无线 = CMPL