2021-09-08 23:38:34 +08:00
|
|
|
|
# -------------------------------
|
|
|
|
|
# Difficult Rocket
|
2022-07-07 18:28:29 +08:00
|
|
|
|
# Copyright © 2021 by shenjackyuanjie
|
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
|
|
|
|
|
"""
|
|
|
|
|
|
2021-09-28 22:47:19 +08:00
|
|
|
|
from typing import Union
|
2021-09-22 06:21:48 +08:00
|
|
|
|
|
2022-07-07 18:28:29 +08:00
|
|
|
|
from SRtool.api import tools
|
|
|
|
|
from SRtool.api.Exp import *
|
2022-07-01 13:59:08 +08:00
|
|
|
|
|
2021-09-28 22:47:19 +08:00
|
|
|
|
"""
|
|
|
|
|
这部分代码使用了中文编程,why?
|
|
|
|
|
你觉得呢?
|
|
|
|
|
"""
|
|
|
|
|
|
2021-09-02 22:47:10 +08:00
|
|
|
|
|
|
|
|
|
class Lang:
|
|
|
|
|
"""
|
|
|
|
|
用于创建一个对应语言的翻译类
|
|
|
|
|
感谢Fallen的MCDR提供idea
|
|
|
|
|
https://github.com/Fallen-Breath/MCDReforged
|
2021-09-22 06:21:48 +08:00
|
|
|
|
可以用
|
|
|
|
|
lang['language'] = 'abc' 或
|
|
|
|
|
lang['lang'] = 'abc'
|
|
|
|
|
的方式直接更改并刷新翻译
|
2021-10-04 20:41:41 +08:00
|
|
|
|
用
|
|
|
|
|
lang.lang(xxx, xxx)来获取翻译过的值
|
2021-09-02 22:47:10 +08:00
|
|
|
|
"""
|
2021-09-05 00:50:05 +08:00
|
|
|
|
|
2022-07-07 18:28:29 +08:00
|
|
|
|
def __init__(self, language: str = 'zh-CN'):
|
2021-10-22 06:36:06 +08:00
|
|
|
|
self.语言 = language
|
2022-07-07 18:28:29 +08:00
|
|
|
|
self.翻译结果 = tools.load_file(f'configs/lang/{language}.json5')
|
|
|
|
|
self.默认翻译 = tools.load_file('configs/lang/zh-CN.json5')
|
2021-09-02 22:47:10 +08:00
|
|
|
|
|
2021-09-22 06:21:48 +08:00
|
|
|
|
def __str__(self) -> str:
|
2021-10-22 06:36:06 +08:00
|
|
|
|
return self.语言
|
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]:
|
2021-09-22 06:21:48 +08:00
|
|
|
|
try:
|
|
|
|
|
return self.翻译结果[item]
|
|
|
|
|
except KeyError:
|
2021-09-28 22:47:19 +08:00
|
|
|
|
try:
|
|
|
|
|
return self.默认翻译[item]
|
|
|
|
|
except KeyError:
|
2022-07-07 18:28:29 +08:00
|
|
|
|
raise LanguageError(f'there\'s no key {item} in both {self.语言} and zh-CN')
|
2021-09-02 22:47:10 +08:00
|
|
|
|
|
2022-07-07 18:28:29 +08:00
|
|
|
|
def __setitem__(self, key, value):
|
2021-09-22 06:21:48 +08:00
|
|
|
|
if key == 'language' or key == 'lang':
|
|
|
|
|
try:
|
2022-07-07 18:28:29 +08:00
|
|
|
|
self.翻译结果 = tools.load_file(f'configs/lang/{value}.json5')
|
2021-10-22 06:36:06 +08:00
|
|
|
|
self.语言 = value
|
2021-09-22 06:21:48 +08:00
|
|
|
|
except FileNotFoundError:
|
2022-07-07 18:28:29 +08:00
|
|
|
|
raise LanguageError(f'{value}\'s language json5 file not found')
|
2021-09-22 06:21:48 +08:00
|
|
|
|
else:
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
2021-10-22 06:36:06 +08:00
|
|
|
|
def set_language(self, language) -> None:
|
2021-09-22 06:21:48 +08:00
|
|
|
|
try:
|
2022-07-07 18:28:29 +08:00
|
|
|
|
self.翻译结果 = tools.load_file(f'configs/lang/{language}.json5')
|
2021-10-22 06:36:06 +08:00
|
|
|
|
self.语言 = language
|
2021-09-22 06:21:48 +08:00
|
|
|
|
except FileNotFoundError:
|
2022-07-07 18:28:29 +08:00
|
|
|
|
raise LanguageError(f'{language}\'s language json5 file not found')
|
2021-09-22 06:21:48 +08:00
|
|
|
|
|
2021-10-22 06:36:06 +08:00
|
|
|
|
def lang(self, *args) -> Union[int, str, list, dict]:
|
2021-09-28 22:47:19 +08:00
|
|
|
|
try:
|
|
|
|
|
结果 = self.翻译结果
|
|
|
|
|
for 选项 in args:
|
|
|
|
|
结果 = 结果[选项]
|
|
|
|
|
return 结果
|
|
|
|
|
except KeyError:
|
|
|
|
|
try:
|
|
|
|
|
结果 = self.默认翻译
|
|
|
|
|
for 选项 in args:
|
|
|
|
|
结果 = 结果[选项]
|
|
|
|
|
return 结果
|
|
|
|
|
except KeyError:
|
2022-07-07 18:28:29 +08:00
|
|
|
|
raise LanguageError(f'there\'s no key {args} in both {self.语言} and zh-CN')
|
2021-09-28 22:47:19 +08:00
|
|
|
|
|
2021-10-22 06:36:06 +08:00
|
|
|
|
def 翻译(self, *args) -> Union[int, str, list, dict]:
|
|
|
|
|
return self.lang(args)
|
2021-10-04 20:41:41 +08:00
|
|
|
|
|
2021-09-22 06:21:48 +08:00
|
|
|
|
|
2021-10-01 23:12:01 +08:00
|
|
|
|
tr = Lang('zh-CN')
|
|
|
|
|
|
|
|
|
|
# font's value
|
|
|
|
|
|
2021-10-06 18:36:10 +08:00
|
|
|
|
HOS = 'HarmonyOS Sans'
|
|
|
|
|
HOS_S = 'HarmonyOS Sans SC'
|
|
|
|
|
HOS_T = 'HarmonyOS Sans TC'
|
2022-07-07 18:28:29 +08:00
|
|
|
|
HOS_I = 'HarmonyOS Sans Italic'
|
2021-10-06 18:36:10 +08:00
|
|
|
|
HOS_C = 'HarmonyOS Sans Condensed'
|
2022-07-07 18:28:29 +08:00
|
|
|
|
HOS_CI = 'HarmonyOS Sans Condensed Italic'
|
|
|
|
|
HOS_NA = 'HarmonyOS Sans Naskh Arabic'
|
|
|
|
|
HOS_NAU = 'HarmonyOS Sans Naskh Arabic_UI'
|
2021-10-01 23:12:01 +08:00
|
|
|
|
|
|
|
|
|
鸿蒙字体 = HOS
|
|
|
|
|
鸿蒙简体 = HOS_S
|
|
|
|
|
鸿蒙繁体 = HOS_T
|
2022-07-07 18:28:29 +08:00
|
|
|
|
鸿蒙斜体 = HOS_I
|
2021-10-01 23:12:01 +08:00
|
|
|
|
鸿蒙窄体 = HOS_C
|
2022-07-07 18:28:29 +08:00
|
|
|
|
鸿蒙斜窄体 = HOS_CI
|
|
|
|
|
鸿蒙阿拉伯 = HOS_NA
|
|
|
|
|
鸿蒙阿拉伯UI = HOS_NAU
|