diff --git a/classes/bot_locale.py b/classes/bot_locale.py deleted file mode 100644 index 2d45c0d..0000000 --- a/classes/bot_locale.py +++ /dev/null @@ -1,15 +0,0 @@ -import logging -from logging import Logger -from typing import override, Any - -from libbot.i18n.classes import BotLocale as LibBotLocale - -logger: Logger = logging.getLogger(__name__) - - -class BotLocale(LibBotLocale): - @override - def _(self, key: str, *args: str, locale: str | None = None) -> Any: - locale = None if locale is None else locale.split("-")[0] - - super()._(key, *args, locale=locale) diff --git a/classes/pycord_bot.py b/classes/pycord_bot.py index 5cf3a83..272cab7 100644 --- a/classes/pycord_bot.py +++ b/classes/pycord_bot.py @@ -1,4 +1,5 @@ -from pathlib import Path +import logging +from logging import Logger from typing import Any from aiohttp import ClientSession @@ -7,7 +8,8 @@ from libbot.cache.manager import create_cache_client from libbot.pycord.classes import PycordBot as LibPycordBot from classes import PycordUser -from classes.bot_locale import BotLocale + +logger: Logger = logging.getLogger(__name__) # from modules.tracking.dhl import update_tracks_dhl @@ -24,12 +26,9 @@ class PycordBot(LibPycordBot): if self.scheduler is None: return - self.bot_locale: BotLocale = BotLocale( - default_locale=self.config["locale"], - locales_root=(Path("locale")), - ) - - self._ = self.bot_locale._ + # This replacement exists because of the different + # i18n formats than provided by libbot + self._ = self._modified_string_getter # Scheduler job for DHL parcel tracking # self.scheduler.add_job( @@ -39,6 +38,15 @@ class PycordBot(LibPycordBot): # args=[self, self.client_session], # ) + def _modified_string_getter(self, key: str, *args: str, locale: str | None = None) -> Any: + """This method exists because of the different i18n formats than provided by libbot. + It splits "-" and takes the first part of the provided locale to make complex language codes + compatible with an easy libbot approach to i18n. + """ + return self.bot_locale._( + key, *args, locale=None if locale is None else locale.split("-")[0] + ) + def _set_cache_engine(self) -> None: if "cache" in self.config and self.config["cache"]["type"] is not None: self.cache = create_cache_client(self.config, self.config["cache"]["type"]) diff --git a/cogs/wallet.py b/cogs/wallet.py index fafe8fc..882a551 100644 --- a/cogs/wallet.py +++ b/cogs/wallet.py @@ -27,9 +27,14 @@ class WalletCog(commands.Cog): ctx.user.id if not user else user.id, ctx.guild_id ) - # TODO Replace with BotLocale calls await ctx.respond( - f"{'Your' if user is None else user.display_name + '\'s'} balance is `{wallet.balance}`" + self.client._("balance_own", "messages", "wallet", locale=ctx.locale).format( + balance=wallet.balance + ) + if user is None + else self.client._("balance_user", "messages", "wallet", locale=ctx.locale).format( + balance=wallet.balance, user=user.display_name + ) ) @command_group.command( @@ -50,7 +55,6 @@ class WalletCog(commands.Cog): try: await wallet.transfer(user.id, ctx.guild_id, amount) except WalletInsufficientFunds: - # TODO Replace with valid BotLocale calls await ctx.respond( self.client._( "transfer_insufficient_funds", "messages", "wallet", locale=ctx.locale @@ -59,7 +63,6 @@ class WalletCog(commands.Cog): return await ctx.respond( - # TODO Replace with valid BotLocale calls self.client._("transfer_success", "messages", "wallet", locale=ctx.locale).format( amount=amount, recipient=user.display_name ) diff --git a/locale/en.json b/locale/en.json index 3db0dce..1c966b1 100644 --- a/locale/en.json +++ b/locale/en.json @@ -1,7 +1,9 @@ { "messages": { "wallet": { - "transfer_success": "You have transferred `{amount}` to `{recipient}`.", + "balance_own": "Your balance is `{balance}`.", + "balance_user": "**{user}**'s balance is `{balance}`.", + "transfer_success": "You have transferred `{amount}` to **{recipient}**.", "transfer_insufficient_funds": "Insufficient funds. `{amount}` more is needed for this transaction." }, "welcome": { diff --git a/locale/uk.json b/locale/uk.json index 4b3db9b..24f5343 100644 --- a/locale/uk.json +++ b/locale/uk.json @@ -1,7 +1,9 @@ { "messages": { "wallet": { - "transfer_success": "Ви перевели `{amount}` на рахунок `{recipient}`.", + "balance_own": "Ваш баланс складає `{balance}`.", + "balance_user": "Баланс **{user}** складає `{balance}`.", + "transfer_success": "Ви перевели `{amount}` на рахунок **{recipient}**.", "transfer_insufficient_funds": "Недостатньо коштів. Потрібно ще `{amount}` для цієї транзакції." }, "welcome": {