Archived
1
0
Fork 0

Rescheduled i18n to v4.1

Issue: #42
This commit is contained in:
Dianliang233 2021-08-11 21:08:13 +08:00
parent f5e62e84d2
commit 9713352f7c
No known key found for this signature in database
GPG key ID: 6C56F399D872F19C
4 changed files with 0 additions and 79 deletions

View file

@ -1,53 +0,0 @@
import json
import os
from core.elements import MessageSession
from database import BotDBUtil
class BotI18n:
storage = {}
languages = []
uselang = 'zh_cn'
session = {}
def __init__(self, msg: MessageSession):
BotI18n.session = msg
files = os.listdir(os.path.abspath('./core/i18n/lang/'))
for lang in files:
obj = json.load(open('./core/i18n/lang/' + lang, 'r'))
name = lang.split('.')[0]
BotI18n.languages.append(name)
BotI18n.storage[name] = obj
for module in BotDBUtil.Module(msg).enable_modules_list:
if module.startswith('_lang_'):
BotI18n.uselang = module.strip('_lang_')
break
else:
continue
@classmethod
def get_string(self, key: str):
strings = BotI18n.storage[BotI18n.uselang]
if key in strings:
return strings[key]
else:
fallbacks = strings['__metadata__']['fallback']
for fallback in fallbacks:
fallbacked = False
fallback_strings = BotI18n.storage[fallback]
if key in fallback_strings:
fallbacked = True
return fallback_strings[key]
else:
continue
if not fallbacked:
return key
@classmethod
def set_language(self, target):
module_session = BotDBUtil.Module(BotI18n.session)
BotI18n.uselang = target
module_session.disable(f'_lang_{BotI18n.uselang}')
module_session.enable(f'_lang_{target}')

View file

@ -1,3 +0,0 @@
{
"en": "1"
}

View file

@ -1,3 +0,0 @@
{
"en": "0"
}

View file

@ -1,20 +0,0 @@
from core.elements import MessageSession
from core.i18n import BotI18n
from core.loader.decorator import command
@command('lang', ('i18n', 'language', 'languages'), (
'~lang {获取当前语言}',
'~lang set <target> {设置群组使用语言}',
'~lang get <string> {获取指定字符串}'),
need_admin=True, is_base_function=True
)
async def lang(msg: MessageSession):
i18n = BotI18n(msg)
if msg.parsed_msg is None:
await msg.sendMessage(i18n.uselang)
elif msg.parsed_msg['set']:
i18n.set_language(msg.parsed_msg['<target>'])
await msg.sendMessage('设置成功。')
elif msg.parsed_msg['get']:
await msg.sendMessage(i18n.get_string(msg.parsed_msg['<string>']))