From 01b8d01285c388c22f74c2e9123997f848e50183 Mon Sep 17 00:00:00 2001 From: Redume Date: Sat, 17 May 2025 11:49:57 +0300 Subject: [PATCH] chore: made localization in the inline command to get the course of the command --- commands/currency.py | 106 ++++++++++++++++++++++++------------------- 1 file changed, 60 insertions(+), 46 deletions(-) diff --git a/commands/currency.py b/commands/currency.py index 0593dee..9907f39 100644 --- a/commands/currency.py +++ b/commands/currency.py @@ -3,13 +3,15 @@ import hashlib from aiogram import types, Router from aiogram.filters import Command -from bot import bot +from bot import bot, db from functions.convert import Converter from functions.create_chart import create_chart from utils.format_number import format_number from utils.inline_query import reply +from i18n.localization import I18n router = Router() +i18n = I18n() @router.inline_query() @@ -17,16 +19,28 @@ async def currency(query: types.InlineQuery) -> None: text = query.query.lower() args = text.split() result_id = hashlib.md5(text.encode()).hexdigest() - get_bot = await bot.get_me() + data = await db.fetch( + 'SELECT lang, chart, chart_period ' + 'FROM users WHERE user_id = ?', + query.from_user.id + ) + + lang = data.get('lang') + locale = i18n.get_locale(lang) + + currency_example = locale["currency_example"].format( + bot_username=get_bot.username + ) + if len(args) < 2: - return await reply(result_id, - [("2 or 3 arguments are required.", - f'@{get_bot.username} USD RUB \n' - f'@{get_bot.username} 12 USD RUB', - None, None)], - query) + await reply( + result_id, + [(locale["error_not_enough_args"], currency_example, None, None)], + query + ) + return conv = Converter() @@ -36,68 +50,68 @@ async def currency(query: types.InlineQuery) -> None: try: conv.amount = float(args[0].replace(',', '.')) if conv.amount < 0: - return await reply( + await reply( result_id, - [ - ("Negative amounts are not supported.", None, None) - ], - query - ) - + [(locale["error_negative_amount"], None, None)], + query + ) + return except ValueError: - return await reply( + await reply( result_id, - [ - ( - "Please enter a valid number for the amount.", - f'@{get_bot.username} USD RUB \n' - f'@{get_bot.username} 12 USD RUB', - None, None - ) - ], - query) - + [(locale["error_invalid_number"], currency_example, None, None)], + query + ) + return from_currency = args[1] conv_currency = args[2] elif len(args) == 2: from_currency = args[0] conv_currency = args[1] else: - return await reply( + await reply( result_id, - [ - ( - 'The source and target currency could not be determined.', - None, None - ) - ], - query + [(locale["error_unknown_currency"], None, None)], + query, ) + return conv.from_currency = from_currency.upper() conv.conv_currency = conv_currency.upper() + try: await conv.convert() except RuntimeError: - return await reply( + await reply( result_id, - [ - ( - 'The currency exchange rate could not be determined', - None, None - ) - ], + [(locale["error_currency_rate"], None, None)], query ) + return - chart = await create_chart(from_currency, conv_currency) + chart = None - message = f'{format_number(conv.amount)} {conv.from_currency} ' \ - f'= {conv.conv_amount} {conv.conv_currency}' + if bool(data.get('chart', 0)): + chart = await create_chart( + from_currency, + conv_currency, + data.get('chart_period', 'month') + ) + message = ( + f"{format_number(conv.amount)} {conv.from_currency} = " + f"{conv.conv_amount} {conv.conv_currency}" + ) results = [(message, None, None)] if chart: - results.insert(0, (f'{message}\n[Chart]({chart})', None, chart)) + results.insert( + 0, + ( + message, + None, + chart + ) + ) - await reply(result_id, results, query) \ No newline at end of file + await reply(result_id, results, query)