WIP: Modified i18n usage

This commit is contained in:
Profitroll 2025-02-24 21:36:01 +01:00
parent fcb09303ec
commit c6f971b39e
Signed by: profitroll
GPG Key ID: FA35CAB49DACD3B2
5 changed files with 29 additions and 29 deletions

View File

@ -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)

View File

@ -1,4 +1,5 @@
from pathlib import Path import logging
from logging import Logger
from typing import Any from typing import Any
from aiohttp import ClientSession 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 libbot.pycord.classes import PycordBot as LibPycordBot
from classes import PycordUser from classes import PycordUser
from classes.bot_locale import BotLocale
logger: Logger = logging.getLogger(__name__)
# from modules.tracking.dhl import update_tracks_dhl # from modules.tracking.dhl import update_tracks_dhl
@ -24,12 +26,9 @@ class PycordBot(LibPycordBot):
if self.scheduler is None: if self.scheduler is None:
return return
self.bot_locale: BotLocale = BotLocale( # This replacement exists because of the different
default_locale=self.config["locale"], # i18n formats than provided by libbot
locales_root=(Path("locale")), self._ = self._modified_string_getter
)
self._ = self.bot_locale._
# Scheduler job for DHL parcel tracking # Scheduler job for DHL parcel tracking
# self.scheduler.add_job( # self.scheduler.add_job(
@ -39,6 +38,15 @@ class PycordBot(LibPycordBot):
# args=[self, self.client_session], # 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: def _set_cache_engine(self) -> None:
if "cache" in self.config and self.config["cache"]["type"] is not 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"]) self.cache = create_cache_client(self.config, self.config["cache"]["type"])

View File

@ -27,9 +27,14 @@ class WalletCog(commands.Cog):
ctx.user.id if not user else user.id, ctx.guild_id ctx.user.id if not user else user.id, ctx.guild_id
) )
# TODO Replace with BotLocale calls
await ctx.respond( 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( @command_group.command(
@ -50,7 +55,6 @@ class WalletCog(commands.Cog):
try: try:
await wallet.transfer(user.id, ctx.guild_id, amount) await wallet.transfer(user.id, ctx.guild_id, amount)
except WalletInsufficientFunds: except WalletInsufficientFunds:
# TODO Replace with valid BotLocale calls
await ctx.respond( await ctx.respond(
self.client._( self.client._(
"transfer_insufficient_funds", "messages", "wallet", locale=ctx.locale "transfer_insufficient_funds", "messages", "wallet", locale=ctx.locale
@ -59,7 +63,6 @@ class WalletCog(commands.Cog):
return return
await ctx.respond( await ctx.respond(
# TODO Replace with valid BotLocale calls
self.client._("transfer_success", "messages", "wallet", locale=ctx.locale).format( self.client._("transfer_success", "messages", "wallet", locale=ctx.locale).format(
amount=amount, recipient=user.display_name amount=amount, recipient=user.display_name
) )

View File

@ -1,7 +1,9 @@
{ {
"messages": { "messages": {
"wallet": { "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." "transfer_insufficient_funds": "Insufficient funds. `{amount}` more is needed for this transaction."
}, },
"welcome": { "welcome": {

View File

@ -1,7 +1,9 @@
{ {
"messages": { "messages": {
"wallet": { "wallet": {
"transfer_success": "Ви перевели `{amount}` на рахунок `{recipient}`.", "balance_own": "Ваш баланс складає `{balance}`.",
"balance_user": "Баланс **{user}** складає `{balance}`.",
"transfer_success": "Ви перевели `{amount}` на рахунок **{recipient}**.",
"transfer_insufficient_funds": "Недостатньо коштів. Потрібно ще `{amount}` для цієї транзакції." "transfer_insufficient_funds": "Недостатньо коштів. Потрібно ще `{amount}` для цієї транзакції."
}, },
"welcome": { "welcome": {