Skip to content

Commit

Permalink
[+] translation support for the 'not subscribed' message
Browse files Browse the repository at this point in the history
  • Loading branch information
imneokitty committed Jun 21, 2024
1 parent 6a1f79c commit d4635e7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 39 deletions.
12 changes: 2 additions & 10 deletions galaxy_backend/tg/bonuses.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import core.config
import tg.hello


def is_bonus_callback(query: aiogram.types.CallbackQuery) -> bool:
return query.data == 'bonus'

Expand All @@ -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,
Expand All @@ -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(
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -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()

44 changes: 15 additions & 29 deletions galaxy_backend/tg/parse_langs.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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'])

0 comments on commit d4635e7

Please sign in to comment.