Difficult-Rocket/Difficult_Rocket/utils/translate.py

184 lines
5.8 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
"""
2022-11-26 21:48:55 +08:00
writen by shenjackyuanjie <3695888@qq.com>
2021-09-02 22:47:10 +08:00
mail: 3695888@qq.com
github: @shenjackyuanjie
gitee: @shenjackyuanjie
"""
2022-07-01 13:59:08 +08:00
import inspect
2022-11-26 21:48:55 +08:00
# import dataclasses
2022-07-01 13:59:08 +08:00
2022-11-26 21:48:55 +08:00
from typing import Union, Tuple, Any, Type, List, Dict, Hashable
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 *
2022-11-26 22:45:30 +08:00
2022-11-26 23:35:49 +08:00
class Translates:
2022-11-27 13:45:59 +08:00
def __init__(self,
value: Union[Dict[str, Any], list, tuple, str],
raise_error: bool = False,
get_list: list = None,
final: bool = False):
"""
一个用于
:param value: 翻译键节点
:param raise_error: 是否抛出错误
:param get_list: 尝试获取的列表
:param final: 是否为翻译结果 (偷懒用的)
"""
2022-11-26 22:45:30 +08:00
self.value: Union[Dict[str, Any], list, tuple] = value
self.raise_error = raise_error
self.get_list = get_list or []
2022-11-26 23:35:49 +08:00
self.final = final
2022-11-26 22:45:30 +08:00
2022-11-27 13:45:59 +08:00
def __getitem__(self, item: Union[str, int, Hashable]) -> Union["Translates", str]:
2022-11-26 22:45:30 +08:00
cache_get_list = self.get_list.copy()
cache_get_list.append(item)
try:
2022-11-27 13:45:59 +08:00
if not self.final:
cache = self.value[item]
2022-11-26 22:45:30 +08:00
except (KeyError, TypeError):
if DR_option.report_translate_no_found:
frame = inspect.currentframe()
last_frame = frame.f_back
if last_frame.f_code == self.__getattr__.__code__:
last_frame = last_frame.f_back
call_info = f'Translate Not Found at {last_frame.f_code.co_name} by {".".join(cache_get_list)} at:' \
f'{last_frame.f_code.co_filename}:{last_frame.f_lineno}'
print(call_info)
if not self.raise_error:
2022-11-26 23:35:49 +08:00
return Translates(value='.'.join(cache_get_list), raise_error=False, final=True)
2022-11-26 22:45:30 +08:00
else:
2022-11-27 13:45:59 +08:00
raise TranslateKeyNotFound(item_names=cache_get_list) from None
if self.final:
return self
else:
return Translates(value=cache, raise_error=self.raise_error, get_list=cache_get_list)
2022-11-26 21:48:55 +08:00
2022-11-26 23:35:49 +08:00
def __getattr__(self, item: Union[str, Hashable]) -> Union["Translates"]:
2022-11-26 21:48:55 +08:00
if hasattr(object, item):
return getattr(object, item)
return self.__getitem__(item)
def __str__(self):
2022-11-27 13:45:59 +08:00
if self.final:
return f'{self.final}.{".".join(self.get_list)}'
2022-11-26 21:48:55 +08:00
return str(self.value)
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-11-27 13:45:59 +08:00
def __init__(self, language: str = None):
self.language_name = language or DR_runtime.language
self.translates: Dict = tools.load_file(f'configs/lang/{self.language_name}.toml')
2022-11-26 21:48:55 +08:00
self.default_translate: Dict = tools.load_file(f'configs/lang/{DR_runtime.default_language}.toml')
self.不抛出异常 = False
2022-07-04 10:36:19 +08:00
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:
2022-11-26 21:48:55 +08:00
self.translates = tools.load_file(f'configs/lang/{DR_runtime.language}.toml')
self.default_translates = 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:
2022-11-26 21:48:55 +08:00
return self.translates[item]
except KeyError:
try:
2022-11-26 21:48:55 +08:00
return self.default_translates[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:
2022-11-26 21:48:55 +08:00
result = self.translates
for option in args:
result = result[option]
return result
except KeyError:
try:
2022-11-26 21:48:55 +08:00
result = self.default_translates
for option in args:
result = result[option]
return result
except KeyError as e:
2022-06-14 20:52:03 +08:00
if self.直接返回原始数据:
return args
2022-11-26 21:48:55 +08:00
raise TranslateKeyNotFound(f'there\'s no key {args} in both {DR_option.language} and zh-CN') from e
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:
"""
用于更新语言(内部调用)
2022-11-26 21:48:55 +08:00
:return: 设置完成后的语言
2022-08-12 21:07:36 +08:00
"""
2022-11-26 21:48:55 +08:00
self.translates = tools.load_file(f'configs/lang/{DR_option.language}.toml')
2022-08-12 21:07:36 +08:00
return DR_option.language
2022-08-12 21:07:36 +08:00
if not __name__ == '__main__':
tr = Lang()
2022-11-27 13:45:59 +08:00
else:
tr_ = Tr()
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
2022-11-26 21:48:55 +08:00
得意黑 = '得意黑'
2022-11-27 13:45:59 +08:00
# SS = smiley-sans
SS = 得意黑