2019-09-08 23:05:43 +02:00
|
|
|
import gettext
|
2019-12-16 18:12:10 +01:00
|
|
|
from .config import Config
|
2019-10-06 23:49:00 +02:00
|
|
|
from cogs.utils.database import Database
|
2019-09-08 23:05:43 +02:00
|
|
|
|
2019-09-29 23:01:49 +02:00
|
|
|
from .models.lang import Lang
|
|
|
|
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-09-29 23:01:49 +02:00
|
|
|
.query(Lang.value)\
|
|
|
|
.filter(Lang.key == str(ctx.guild.id))
|
|
|
|
if current.count() > 0:
|
|
|
|
return current.one()[0]
|
|
|
|
|
2019-10-06 23:49:00 +02:00
|
|
|
default = database.session\
|
2019-09-29 23:01:49 +02:00
|
|
|
.query(Lang.value)\
|
|
|
|
.filter(Lang.key == 'default')\
|
|
|
|
.one()[0]
|
|
|
|
return default
|