2019-09-08 23:05:43 +02:00
|
|
|
import gettext
|
2019-12-16 18:12:10 +01:00
|
|
|
from .config import Config
|
2020-01-04 02:46:12 +01:00
|
|
|
from utils import Database
|
2019-09-08 23:05:43 +02:00
|
|
|
|
2019-12-30 00:48:11 +01:00
|
|
|
from .models.lang import LangModel
|
2019-09-29 23:01:49 +02:00
|
|
|
from discord.ext import commands
|
|
|
|
|
2019-09-08 23:05:43 +02:00
|
|
|
|
2019-09-12 15:43:57 +02:00
|
|
|
class Texts:
|
2019-09-29 23:01:49 +02:00
|
|
|
def __init__(self, base: str = 'base', ctx: commands.Context = None):
|
|
|
|
self.locale = self.get_locale(ctx)
|
2019-09-29 18:31:01 +02:00
|
|
|
self.base = base
|
2019-09-12 15:43:57 +02:00
|
|
|
|
|
|
|
def get(self, text: str) -> str:
|
2019-09-29 18:31:01 +02:00
|
|
|
texts = gettext.translation(self.base, localedir='extras/locales',
|
|
|
|
languages=[self.locale])
|
|
|
|
texts.install()
|
|
|
|
return texts.gettext(text)
|
|
|
|
|
|
|
|
def set(self, lang: str):
|
|
|
|
self.locale = lang
|
2019-09-29 23:01:49 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_locale(ctx):
|
2019-12-16 18:12:10 +01:00
|
|
|
database = Database(Config("./configs/config.cfg"))
|
2019-09-29 23:01:49 +02:00
|
|
|
|
|
|
|
if ctx is not None:
|
2019-10-06 23:49:00 +02:00
|
|
|
current = database.session\
|
2019-12-30 00:48:11 +01:00
|
|
|
.query(LangModel.value)\
|
|
|
|
.filter(LangModel.key == str(ctx.guild.id))
|
2019-09-29 23:01:49 +02:00
|
|
|
if current.count() > 0:
|
|
|
|
return current.one()[0]
|
|
|
|
|
2019-10-06 23:49:00 +02:00
|
|
|
default = database.session\
|
2019-12-30 00:48:11 +01:00
|
|
|
.query(LangModel.value)\
|
|
|
|
.filter(LangModel.key == 'default')\
|
2019-09-29 23:01:49 +02:00
|
|
|
.one()[0]
|
|
|
|
return default
|