WIP: Modified i18n usage
This commit is contained in:
parent
fcb09303ec
commit
c6f971b39e
@ -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)
|
@ -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"])
|
||||
|
@ -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
|
||||
)
|
||||
|
@ -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": {
|
||||
|
@ -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": {
|
||||
|
Loading…
x
Reference in New Issue
Block a user