diff --git a/galaxy_backend/tg/bonuses.py b/galaxy_backend/tg/bonuses.py index 8b32497..998d340 100644 --- a/galaxy_backend/tg/bonuses.py +++ b/galaxy_backend/tg/bonuses.py @@ -4,7 +4,6 @@ import core.config import tg.hello - def is_bonus_callback(query: aiogram.types.CallbackQuery) -> bool: return query.data == 'bonus' @@ -17,7 +16,6 @@ def is_join_channel_callback(query: aiogram.types.CallbackQuery) -> bool: def is_check_subscription_callback(query: aiogram.types.CallbackQuery) -> bool: return query.data == 'check_subscription' - def get_task_markup(tr: Lang): join_channel_button = aiogram.types.InlineKeyboardButton( text=tr.join_our_channel, @@ -35,7 +33,6 @@ def get_task_markup(tr: Lang): inline_keyboard=[[join_channel_button], [invite_friend_button], [main_back_button]] ) - def get_join_markup(tr: Lang): channel_link = f'https://t.me/{core.config.env.channel_username}' join_button = aiogram.types.InlineKeyboardButton( @@ -54,7 +51,6 @@ def get_join_markup(tr: Lang): inline_keyboard=[[join_button], [channel_check_button], [main_back_button]] ) - @all.dp.callback_query(is_bonus_callback) async def on_bonus_button_press(callback_query: aiogram.types.CallbackQuery): assert isinstance(callback_query.message, aiogram.types.Message) @@ -67,7 +63,6 @@ async def on_bonus_button_press(callback_query: aiogram.types.CallbackQuery): ) await callback_query.answer() - @all.dp.callback_query(is_join_channel_callback) async def on_join_channel_button_press(callback_query: aiogram.types.CallbackQuery): assert isinstance(callback_query.message, aiogram.types.Message) @@ -80,7 +75,6 @@ async def on_join_channel_button_press(callback_query: aiogram.types.CallbackQue ) await callback_query.answer() - @all.dp.callback_query(is_main_back_callback) async def on_back_button_press(callback_query: aiogram.types.CallbackQuery): assert isinstance(callback_query.message, aiogram.types.Message) @@ -104,7 +98,6 @@ async def on_back_button_press(callback_query: aiogram.types.CallbackQuery): ) await callback_query.answer() - @all.dp.callback_query(is_check_subscription_callback) async def on_check_subscription(callback_query: aiogram.types.CallbackQuery): assert isinstance(callback_query.message, aiogram.types.Message) @@ -118,7 +111,7 @@ async def on_check_subscription(callback_query: aiogram.types.CallbackQuery): callback_query.from_user.id, ) except aiogram.exceptions.TelegramAPIError: - await callback_query.answer('not subscribed') + await callback_query.answer(tr.not_subscribed) return main_back_button = aiogram.types.InlineKeyboardButton( text=tr.back, @@ -134,8 +127,7 @@ async def on_check_subscription(callback_query: aiogram.types.CallbackQuery): ) else: await callback_query.message.edit_text( - text='not subscribed', + text=tr.not_subscribed, reply_markup=get_join_markup(tr) ) await callback_query.answer() - diff --git a/galaxy_backend/tg/parse_langs.py b/galaxy_backend/tg/parse_langs.py index fbf08e9..79487a4 100644 --- a/galaxy_backend/tg/parse_langs.py +++ b/galaxy_backend/tg/parse_langs.py @@ -1,22 +1,13 @@ from pathlib import Path -import core.common import json - +import core.common +from typing import Optional class Lang: - def __init__( - self, - path: Path, - ) -> None: - parsed_json = json.loads( - path.read_text(encoding='utf-8') - ) + def __init__(self, path: Path, default_lang: Optional['Lang'] = None) -> None: + parsed_json = json.loads(path.read_text(encoding='utf-8')) for key in self.__annotations__.keys(): - if key in parsed_json: - val = parsed_json[key] - else: - assert langs.en - val = getattr(langs.en, key) + val = parsed_json.get(key, getattr(default_lang, key) if default_lang else None) setattr(self, key, val) start_game: str @@ -29,7 +20,6 @@ def __init__( coins_for_tasks: str click_to_join: str thanks_for_joining: str - not_joined_yet: str invite_friend_button: str clans_invalid_id: str clans_already_participate: str @@ -38,20 +28,16 @@ def __init__( clan_join_request: str clan_join_confirmed: str clan_join_denied: str + not_subscribed: str +def load_langs(path: Path) -> dict: + langs = {} + langs['en'] = Lang(path / 'en.json') + langs['ru'] = Lang(path / 'ru.json', default_lang=langs['en']) + langs['uk'] = Lang(path / 'uk.json', default_lang=langs['en']) + return langs -class langs: - path = core.common.path.langs - en: Lang = Lang(path / 'en.json') - ru: Lang = Lang(path / 'ru.json') - uk: Lang = Lang(path / 'uk.json') - - -def get_tr( - lang_code: str, -) -> Lang: - return langs.__dict__.get( - lang_code, - langs.en, - ) +langs = load_langs(core.common.path.langs) +def get_tr(lang_code: str) -> Lang: + return langs.get(lang_code, langs['en'])