tuxbot-bot/cogs/utils/lang.py

39 lines
1.1 KiB
Python
Raw Normal View History

import gettext
2019-12-16 18:12:10 +01:00
from .config import Config
from cogs.utils.database import Database
from .models.lang import Lang
from discord.ext import commands
class Texts:
def __init__(self, base: str = 'base', ctx: commands.Context = None):
self.locale = self.get_locale(ctx)
self.base = base
def get(self, text: str) -> str:
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
@staticmethod
def get_locale(ctx):
2019-12-16 18:12:10 +01:00
database = Database(Config("./configs/config.cfg"))
if ctx is not None:
current = database.session\
.query(Lang.value)\
.filter(Lang.key == str(ctx.guild.id))
if current.count() > 0:
return current.one()[0]
default = database.session\
.query(Lang.value)\
.filter(Lang.key == 'default')\
.one()[0]
return default