[feat]: New translation system

This commit is contained in:
Sweetbread
2023-05-20 21:19:32 +03:00
parent 1f10c788e4
commit 6a99350ac5
3 changed files with 217 additions and 151 deletions
+1 -1
View File
@@ -244,7 +244,7 @@ class Economic(commands.Cog, name="Экономика"):
logger.debug(m["guild_stat"][str(guild.id)]["exp"])
stat_gr = app_commands.Group(name="statistic", description="Some statistic")
stat_gr = app_commands.Group(name="statistics", description="Some statistics")
@stat_gr.command(description="View balance and level")
async def rank(self, inter: discord.Interaction, user: discord.Member = None):
+47 -12
View File
@@ -1,4 +1,5 @@
import discord
from bot import db
from discord import app_commands
from typing import Optional
from loguru import logger
@@ -10,21 +11,55 @@ import json
class MeowTranslator(app_commands.Translator):
async def load(self):
with open('translations.json', 'r') as f:
self.translations = json.loads(f.read())
data = json.loads(f.read())
self.db = db.translation
await self.db.delete_many({})
for locale in data.keys():
logger.debug(locale)
for type_ in data[locale].keys():
logger.debug('\t'+type_)
for key in data[locale][type_].keys():
logger.debug('\t\t'+key)
translate = data[locale][type_][key]
if type(translate) is dict:
for tr in translate.keys():
logger.debug('\t\t\t'+tr)
await self.db.insert_one({'locale': locale, 'type': int(type_), 'context': key, 'string': tr, 'translate': translate[tr]})
else:
logger.debug('\t\t '+translate)
await self.db.insert_one({'locale': locale, 'type': int(type_), 'string': key, 'translate': translate})
async def unload(self): pass
async def translate(self, string: app_commands.locale_str, locale: discord.Locale, context: app_commands.TranslationContext) -> Optional[str]:
logger.debug(f"{locale}\t{string.message}")
if str(locale) == "uk": locale = "ru" # TODO: make translation for Ukranian
if str(locale) not in self.translations.keys(): return
if context.location is trans_context.other:
if f"{context.data}.{string.message}" in self.translations[str(locale)].keys():
return self.translations[str(locale)][f"{context.data}.{string.message}"]
elif context.data in self.translations[str(locale)].keys():
return self.translations[str(locale)][context.data]
else: return
if string.message not in self.translations[str(locale)].keys(): return
return self.translations[str(locale)][string.message]
if context.data is str:
search_dict = {'locale': str(locale), 'type': context.location.value, 'context': context.data, 'string': string.message}
else:
search_dict = {'locale': str(locale), 'type': context.location.value, 'string': string.message}
data = await self.db.find_one(search_dict)
if data:
return data['translate']
if str(locale) == 'en-US': return
if str(locale) == 'uk': search_dict['locale'] = 'ru'
else: search_dict['locale'] = 'en-US'
if (data := await self.db.find_one(search_dict)):
return data['translate']
return
# if str(locale) == "uk": locale = "ru" # TODO: make translation for Ukranian
# if str(locale) not in self.translations.keys(): return
# if context.location is trans_context.other:
# if f"{context.data}.{string.message}" in self.translations[str(locale)].keys():
# return self.translations[str(locale)][f"{context.data}.{string.message}"]
# elif context.data in self.translations[str(locale)].keys():
# return self.translations[str(locale)][context.data]
# else: return
# if string.message not in self.translations[str(locale)].keys(): return
# return self.translations[str(locale)][string.message]
async def setup(bot):