tuxbot-bot/utils/functions/lang.py

35 lines
871 B
Python
Raw Permalink Normal View History

import gettext
import json
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:
2020-01-12 21:43:05 +01:00
texts = gettext.translation(self.base, localedir='utils/locales',
languages=[self.locale])
texts.install()
return texts.gettext(text)
def set(self, lang: str):
self.locale = lang
@staticmethod
2020-02-04 18:47:11 +01:00
def get_locale(ctx: commands.Context):
lang = 'fr'
if ctx is not None:
2020-02-04 18:47:11 +01:00
try:
with open(f'./configs/guilds/{ctx.guild.id}.json', 'r') as f:
data = json.load(f)
lang = data['lang']
except FileNotFoundError:
pass
return lang