2019-09-08 21:05:43 +00:00
|
|
|
import gettext
|
2020-01-15 21:56:54 +00:00
|
|
|
import json
|
2019-09-08 21:05:43 +00:00
|
|
|
|
2019-09-29 21:01:49 +00:00
|
|
|
from discord.ext import commands
|
|
|
|
|
2019-09-08 21:05:43 +00:00
|
|
|
|
2019-09-12 13:43:57 +00:00
|
|
|
class Texts:
|
2019-09-29 21:01:49 +00:00
|
|
|
def __init__(self, base: str = 'base', ctx: commands.Context = None):
|
|
|
|
self.locale = self.get_locale(ctx)
|
2019-09-29 16:31:01 +00:00
|
|
|
self.base = base
|
2019-09-12 13:43:57 +00:00
|
|
|
|
|
|
|
def get(self, text: str) -> str:
|
2020-01-12 20:43:05 +00:00
|
|
|
texts = gettext.translation(self.base, localedir='utils/locales',
|
2019-09-29 16:31:01 +00:00
|
|
|
languages=[self.locale])
|
|
|
|
texts.install()
|
|
|
|
return texts.gettext(text)
|
|
|
|
|
|
|
|
def set(self, lang: str):
|
|
|
|
self.locale = lang
|
2019-09-29 21:01:49 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2020-02-04 17:47:11 +00:00
|
|
|
def get_locale(ctx: commands.Context):
|
|
|
|
lang = 'fr'
|
2019-09-29 21:01:49 +00:00
|
|
|
if ctx is not None:
|
2020-02-04 17:47:11 +00: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
|