2019-09-08 23:05:43 +02:00
|
|
|
import gettext
|
2020-01-15 22:56:54 +01:00
|
|
|
import json
|
2019-09-08 23:05:43 +02:00
|
|
|
|
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:
|
2020-01-12 21:43:05 +01:00
|
|
|
texts = gettext.translation(self.base, localedir='utils/locales',
|
2019-09-29 18:31:01 +02:00
|
|
|
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
|
2020-02-04 18:47:11 +01:00
|
|
|
def get_locale(ctx: commands.Context):
|
|
|
|
lang = 'fr'
|
2019-09-29 23:01:49 +02:00
|
|
|
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
|