Enhance localization for user-facing messages across payment and subscription services
- Updated various services to utilize the user's database language for all user-facing messages, improving the localization experience. - Refactored message retrieval logic in payment processing, subscription management, and notification handling to ensure consistency in language usage. - Added comments to clarify the purpose of language settings in relevant sections of the code.
This commit is contained in:
@@ -198,6 +198,7 @@ async def process_successful_payment(session: AsyncSession, bot: Bot,
|
|||||||
applied_referee_bonus_days_from_referral = referral_bonus_info.get(
|
applied_referee_bonus_days_from_referral = referral_bonus_info.get(
|
||||||
"referee_bonus_applied_days")
|
"referee_bonus_applied_days")
|
||||||
|
|
||||||
|
# Use user's DB language for all user-facing messages
|
||||||
user_lang = db_user.language_code if db_user and db_user.language_code else settings.DEFAULT_LANGUAGE
|
user_lang = db_user.language_code if db_user and db_user.language_code else settings.DEFAULT_LANGUAGE
|
||||||
_ = lambda key, **kwargs: i18n.gettext(user_lang, key, **kwargs)
|
_ = lambda key, **kwargs: i18n.gettext(user_lang, key, **kwargs)
|
||||||
|
|
||||||
@@ -488,6 +489,7 @@ async def yookassa_webhook_route(request: web.Request):
|
|||||||
await session.rollback()
|
await session.rollback()
|
||||||
# Notify user about successful binding with Back button
|
# Notify user about successful binding with Back button
|
||||||
try:
|
try:
|
||||||
|
# Use user's DB language for bind success notification
|
||||||
i18n_lang = settings.DEFAULT_LANGUAGE
|
i18n_lang = settings.DEFAULT_LANGUAGE
|
||||||
from db.dal import user_dal
|
from db.dal import user_dal
|
||||||
db_user = await user_dal.get_user_by_id(session, user_id)
|
db_user = await user_dal.get_user_by_id(session, user_id)
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import logging
|
|||||||
from aiogram import Router, F, types, Bot
|
from aiogram import Router, F, types, Bot
|
||||||
from aiogram.filters import Command
|
from aiogram.filters import Command
|
||||||
from aiogram.types import InlineKeyboardButton, InlineKeyboardMarkup, LabeledPrice
|
from aiogram.types import InlineKeyboardButton, InlineKeyboardMarkup, LabeledPrice
|
||||||
from typing import Optional, Dict, Any, Union
|
from typing import Optional, Dict, Any, Union, List
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
from sqlalchemy.future import select
|
from sqlalchemy.future import select
|
||||||
@@ -774,14 +774,14 @@ async def payment_method_history(callback: types.CallbackQuery, settings: Settin
|
|||||||
async def payment_methods_list(callback: types.CallbackQuery, settings: Settings, i18n_data: dict, session: AsyncSession):
|
async def payment_methods_list(callback: types.CallbackQuery, settings: Settings, i18n_data: dict, session: AsyncSession):
|
||||||
current_lang = i18n_data.get("current_language", settings.DEFAULT_LANGUAGE)
|
current_lang = i18n_data.get("current_language", settings.DEFAULT_LANGUAGE)
|
||||||
i18n: Optional[JsonI18n] = i18n_data.get("i18n_instance")
|
i18n: Optional[JsonI18n] = i18n_data.get("i18n_instance")
|
||||||
_ = lambda key, **kwargs: i18n.gettext(current_lang, key, **kwargs) if i18n else key
|
get_text = lambda key, **kwargs: i18n.gettext(current_lang, key, **kwargs) if i18n else key
|
||||||
|
|
||||||
# For now we only support single saved YK method; format as list API-ready
|
# For now we only support single saved YK method; format as list API-ready
|
||||||
from db.dal.user_billing_dal import list_user_payment_methods
|
from db.dal.user_billing_dal import list_user_payment_methods
|
||||||
cards: List[tuple] = []
|
cards: List[tuple] = []
|
||||||
methods = await list_user_payment_methods(session, callback.from_user.id)
|
methods = await list_user_payment_methods(session, callback.from_user.id)
|
||||||
for m in methods:
|
for m in methods:
|
||||||
title = _("payment_method_card_title", network=m.card_network or "Card", last4=m.card_last4 or "????")
|
title = get_text("payment_method_card_title", network=m.card_network or "Card", last4=m.card_last4 or "????")
|
||||||
cards.append((str(m.method_id), title if not m.is_default else f"⭐ {title}"))
|
cards.append((str(m.method_id), title if not m.is_default else f"⭐ {title}"))
|
||||||
|
|
||||||
# Parse page
|
# Parse page
|
||||||
@@ -791,9 +791,9 @@ async def payment_methods_list(callback: types.CallbackQuery, settings: Settings
|
|||||||
except Exception:
|
except Exception:
|
||||||
page = 0
|
page = 0
|
||||||
|
|
||||||
text = _("payment_methods_title")
|
text = get_text("payment_methods_title")
|
||||||
if not cards:
|
if not cards:
|
||||||
text += "\n\n" + _("payment_method_none")
|
text += "\n\n" + get_text("payment_method_none")
|
||||||
await callback.message.edit_text(text, reply_markup=get_payment_methods_list_keyboard(cards, page, current_lang, i18n))
|
await callback.message.edit_text(text, reply_markup=get_payment_methods_list_keyboard(cards, page, current_lang, i18n))
|
||||||
try:
|
try:
|
||||||
await callback.answer()
|
await callback.answer()
|
||||||
|
|||||||
@@ -155,6 +155,7 @@ class CryptoPayService:
|
|||||||
return
|
return
|
||||||
|
|
||||||
db_user = await user_dal.get_user_by_id(session, user_id)
|
db_user = await user_dal.get_user_by_id(session, user_id)
|
||||||
|
# Use DB language for user-facing messages
|
||||||
lang = db_user.language_code if db_user and db_user.language_code else settings.DEFAULT_LANGUAGE
|
lang = db_user.language_code if db_user and db_user.language_code else settings.DEFAULT_LANGUAGE
|
||||||
_ = lambda k, **kw: i18n.gettext(lang, k, **kw)
|
_ = lambda k, **kw: i18n.gettext(lang, k, **kw)
|
||||||
|
|
||||||
|
|||||||
@@ -109,8 +109,9 @@ class StarsService:
|
|||||||
if not final_end:
|
if not final_end:
|
||||||
final_end = activation_details["end_date"]
|
final_end = activation_details["end_date"]
|
||||||
|
|
||||||
current_lang = i18n_data.get("current_language",
|
# Always use user's language from DB for user-facing messages
|
||||||
self.settings.DEFAULT_LANGUAGE)
|
db_user = await user_dal.get_user_by_id(session, message.from_user.id)
|
||||||
|
current_lang = db_user.language_code if db_user and db_user.language_code else self.settings.DEFAULT_LANGUAGE
|
||||||
i18n: JsonI18n = i18n_data.get("i18n_instance")
|
i18n: JsonI18n = i18n_data.get("i18n_instance")
|
||||||
_ = lambda k, **kw: i18n.gettext(current_lang, k, **kw) if i18n else k
|
_ = lambda k, **kw: i18n.gettext(current_lang, k, **kw) if i18n else k
|
||||||
|
|
||||||
|
|||||||
@@ -209,6 +209,7 @@ class TributeService:
|
|||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
# Use user's DB language in success messages prepared above
|
||||||
await bot.send_message(
|
await bot.send_message(
|
||||||
int(user_id),
|
int(user_id),
|
||||||
success_msg,
|
success_msg,
|
||||||
|
|||||||
Reference in New Issue
Block a user