2021-09-08 23:38:34 +08:00
|
|
|
# -------------------------------
|
|
|
|
# Difficult Rocket
|
|
|
|
# Copyright © 2021 by shenjackyuanjie
|
|
|
|
# All rights reserved
|
|
|
|
# -------------------------------
|
|
|
|
|
2021-09-02 22:47:10 +08:00
|
|
|
"""
|
|
|
|
writen by shenjackyuanjie
|
|
|
|
mail: 3695888@qq.com
|
|
|
|
github: @shenjackyuanjie
|
|
|
|
gitee: @shenjackyuanjie
|
|
|
|
"""
|
|
|
|
|
2021-09-22 06:21:48 +08:00
|
|
|
from typing import List
|
|
|
|
|
|
|
|
from Difficult_Rocket.api import tools
|
|
|
|
from Difficult_Rocket.api.Exp import *
|
|
|
|
|
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-09-02 22:47:10 +08:00
|
|
|
"""
|
2021-09-05 00:50:05 +08:00
|
|
|
|
2021-09-22 06:21:48 +08:00
|
|
|
def __init__(self, language: str = 'zh-CN'):
|
|
|
|
self.language = language
|
|
|
|
self.翻译结果 = tools.config(f'configs/lang/{language}.json5')
|
|
|
|
self.默认翻译 = tools.config('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:
|
|
|
|
return self.language
|
2021-09-02 22:47:10 +08:00
|
|
|
|
|
|
|
def __getitem__(self, item):
|
2021-09-22 06:21:48 +08:00
|
|
|
try:
|
|
|
|
return self.翻译结果[item]
|
|
|
|
except KeyError:
|
|
|
|
return self.默认翻译[item]
|
2021-09-02 22:47:10 +08:00
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
2021-09-22 06:21:48 +08:00
|
|
|
if key == 'language' or key == 'lang':
|
|
|
|
try:
|
|
|
|
self.翻译结果 = tools.config(f'configs/lang/{value}.json5')
|
|
|
|
self.language = value
|
|
|
|
except FileNotFoundError:
|
|
|
|
raise LanguageError(f'{value}\'s language json5 file not found')
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def set_language(self, language):
|
|
|
|
try:
|
|
|
|
self.翻译结果 = tools.config(f'configs/lang/{language}.json5')
|
|
|
|
self.language = language
|
|
|
|
except FileNotFoundError:
|
|
|
|
raise LanguageError(f'{language}\'s language json5 file not found')
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
tr = Lang('zh-CN')
|
|
|
|
except FileNotFoundError:
|
|
|
|
import os
|
|
|
|
|
|
|
|
os.chdir('..')
|
|
|
|
os.chdir('..')
|
|
|
|
tr = Lang('zh-CN')
|
|
|
|
|
|
|
|
|
|
|
|
def test():
|
|
|
|
print(tr)
|
|
|
|
assert tr.language == 'zh-CN'
|