feat(i18n): start skeleton class for translations
This commit is contained in:
parent
dbf7f3ce8e
commit
bf6d961658
7 changed files with 109 additions and 3 deletions
|
@ -161,7 +161,7 @@ async def shutdown_handler(tux: Tux, signal_type, exit_code=None) -> NoReturn:
|
||||||
await asyncio.gather(*pending, return_exceptions=True)
|
await asyncio.gather(*pending, return_exceptions=True)
|
||||||
|
|
||||||
|
|
||||||
async def run_bot(tux: Tux, cli_flags: Namespace, loop) -> None:
|
async def run_bot(tux: Tux, cli_flags: Namespace) -> None:
|
||||||
"""This run the bot.
|
"""This run the bot.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
|
@ -238,7 +238,7 @@ def main() -> NoReturn:
|
||||||
dm_help=None
|
dm_help=None
|
||||||
)
|
)
|
||||||
|
|
||||||
loop.run_until_complete(run_bot(tux, cli_flags, loop))
|
loop.run_until_complete(run_bot(tux, cli_flags))
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print(Fore.RED
|
print(Fore.RED
|
||||||
+ "Please use <prefix>quit instead of Ctrl+C to Shutdown!"
|
+ "Please use <prefix>quit instead of Ctrl+C to Shutdown!"
|
||||||
|
|
6
tuxbot/cogs/anti_raid/__init__.py
Normal file
6
tuxbot/cogs/anti_raid/__init__.py
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
from .anti_raid import Warnings
|
||||||
|
from ...core.bot import Tux
|
||||||
|
|
||||||
|
|
||||||
|
def setup(bot: Tux):
|
||||||
|
bot.add_cog(Warnings(bot))
|
24
tuxbot/cogs/anti_raid/anti_raid.py
Normal file
24
tuxbot/cogs/anti_raid/anti_raid.py
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from discord.ext import commands
|
||||||
|
|
||||||
|
from tuxbot.core import checks
|
||||||
|
from tuxbot.core.bot import Tux
|
||||||
|
from tuxbot.core.i18n import Translator
|
||||||
|
|
||||||
|
log = logging.getLogger("tuxbot.cogs.anti_raid")
|
||||||
|
T_ = Translator("AntiRaid", __file__)
|
||||||
|
|
||||||
|
|
||||||
|
class AntiRaid(commands.Cog, name="AntiRaid"):
|
||||||
|
def __init__(self, bot: Tux):
|
||||||
|
self.bot = bot
|
||||||
|
|
||||||
|
@commands.group(
|
||||||
|
name='anti_raid',
|
||||||
|
alias=['anti-raid', 'raid_protect', 'raid-protect', 'no_raid', 'no-raid']
|
||||||
|
)
|
||||||
|
@commands.guild_only()
|
||||||
|
@checks.is_admin()
|
||||||
|
async def _warn(self, ctx: commands.Context):
|
||||||
|
pass
|
|
@ -101,6 +101,7 @@ class Tux(commands.AutoShardedBot):
|
||||||
f"Language: {self.config('core').get('locale')}",
|
f"Language: {self.config('core').get('locale')}",
|
||||||
f"Tuxbot Version: {__version__}",
|
f"Tuxbot Version: {__version__}",
|
||||||
f"Discord.py Version: {discord.__version__}",
|
f"Discord.py Version: {discord.__version__}",
|
||||||
|
"Python Version: " + sys.version.replace('\n', ''),
|
||||||
f"Shards: {self.shard_count}",
|
f"Shards: {self.shard_count}",
|
||||||
f"Servers: {len(self.guilds)}",
|
f"Servers: {len(self.guilds)}",
|
||||||
f"Users: {len(self.users)}"
|
f"Users: {len(self.users)}"
|
||||||
|
|
70
tuxbot/core/i18n.py
Normal file
70
tuxbot/core/i18n.py
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Callable, Union, Dict
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"get_locale",
|
||||||
|
"set_locale",
|
||||||
|
"reload_locales",
|
||||||
|
"Translator",
|
||||||
|
]
|
||||||
|
|
||||||
|
_translators = []
|
||||||
|
_current_locale = "en-US"
|
||||||
|
|
||||||
|
|
||||||
|
def get_locale() -> str:
|
||||||
|
return _current_locale
|
||||||
|
|
||||||
|
|
||||||
|
def set_locale(locale: str) -> None:
|
||||||
|
global _current_locale
|
||||||
|
_current_locale = locale
|
||||||
|
reload_locales()
|
||||||
|
|
||||||
|
|
||||||
|
def reload_locales() -> None:
|
||||||
|
for translator in _translators:
|
||||||
|
translator.load_translations()
|
||||||
|
|
||||||
|
|
||||||
|
class Translator(Callable[[str], str]):
|
||||||
|
"""Class to load locales at init."""
|
||||||
|
|
||||||
|
def __init__(self, name: str, file_location: Union[str, Path, os.PathLike]):
|
||||||
|
"""Initializes the Translator object.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
name : str
|
||||||
|
The cog name.
|
||||||
|
file_location:str|Path|os.PathLike
|
||||||
|
File path for the required extension.
|
||||||
|
|
||||||
|
"""
|
||||||
|
self.cog_folder = Path(file_location).resolve().parent
|
||||||
|
self.cog_name = name
|
||||||
|
self.translations = {}
|
||||||
|
|
||||||
|
_translators.append(self)
|
||||||
|
|
||||||
|
self.load_translations()
|
||||||
|
|
||||||
|
def __call__(self, untranslated: str) -> str:
|
||||||
|
try:
|
||||||
|
return self.translations[untranslated]
|
||||||
|
except KeyError:
|
||||||
|
return untranslated
|
||||||
|
|
||||||
|
def load_translations(self):
|
||||||
|
"""Loads the current translations.
|
||||||
|
|
||||||
|
"""
|
||||||
|
self.translations = {}
|
||||||
|
locale_path = self.cog_folder / "locales" / f"{get_locale()}.po"
|
||||||
|
|
||||||
|
...
|
||||||
|
|
||||||
|
def _add_translation(self, untranslated, translated):
|
||||||
|
if translated:
|
||||||
|
self.translations[untranslated] = translated
|
|
@ -6,7 +6,8 @@ from discord.ext import commands, flags
|
||||||
|
|
||||||
class ContextPlus(commands.Context):
|
class ContextPlus(commands.Context):
|
||||||
async def send(self, content=None, *args, **kwargs):
|
async def send(self, content=None, *args, **kwargs):
|
||||||
if (hasattr(self.command, 'deletable') and self.command.deletable) \
|
if (hasattr(self.command, 'deletable')
|
||||||
|
and self.command.deletable) \
|
||||||
and kwargs.pop('deletable', True):
|
and kwargs.pop('deletable', True):
|
||||||
message = await super().send(content, *args, **kwargs)
|
message = await super().send(content, *args, **kwargs)
|
||||||
await message.add_reaction('🗑')
|
await message.add_reaction('🗑')
|
||||||
|
|
|
@ -42,5 +42,9 @@ def init_logging(level: int, location: pathlib.Path) -> None:
|
||||||
|
|
||||||
stdout_handler = logging.StreamHandler(sys.stdout)
|
stdout_handler = logging.StreamHandler(sys.stdout)
|
||||||
stdout_handler.setFormatter(formatter)
|
stdout_handler.setFormatter(formatter)
|
||||||
|
|
||||||
|
dpy_handler.setFormatter(formatter)
|
||||||
|
base_handler.setFormatter(formatter)
|
||||||
|
|
||||||
dpy_logger.addHandler(dpy_handler)
|
dpy_logger.addHandler(dpy_handler)
|
||||||
base_logger.addHandler(base_handler)
|
base_logger.addHandler(base_handler)
|
||||||
|
|
Loading…
Reference in a new issue