Локализация таблицы (ru/en): колонка О→Оч/Pts, детект языка клиента Telegram
All checks were successful
CI/CD Pipeline / build-and-deploy (push) Successful in 5s
All checks were successful
CI/CD Pipeline / build-and-deploy (push) Successful in 5s
This commit is contained in:
parent
22a0e27763
commit
cfd410d5fe
1 changed files with 43 additions and 11 deletions
|
|
@ -32,6 +32,35 @@ START_MSG = (
|
|||
'Работает для швейцарских турниров\. Движок: FIDE Dutch System \(bbpPairings\)\.'
|
||||
)
|
||||
|
||||
LOCALE = {
|
||||
'ru': {
|
||||
'player': 'Игрок',
|
||||
'rating': 'Рейт',
|
||||
'points': 'Оч',
|
||||
'tb1': 'Доп1',
|
||||
'title': '📋 *Тур {rnd}* — пары',
|
||||
'bye': '— BYE —',
|
||||
'footer_bbp': '\n_FIDE 2025 \(bbpPairings\)_',
|
||||
'footer_fallback': '\n_Упрощённый Swiss \(bbpPairings недоступен\)_',
|
||||
},
|
||||
'en': {
|
||||
'player': 'Player',
|
||||
'rating': 'Rat',
|
||||
'points': 'Pts',
|
||||
'tb1': 'TB1',
|
||||
'title': '📋 *Round {rnd}* — pairings',
|
||||
'bye': '— BYE —',
|
||||
'footer_bbp': '\n_FIDE 2025 \(bbpPairings\)_',
|
||||
'footer_fallback': '\n_Simplified Swiss \(bbpPairings unavailable\)_',
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def _get_locale(lang_code: str) -> dict:
|
||||
if lang_code and lang_code.startswith('ru'):
|
||||
return LOCALE['ru']
|
||||
return LOCALE['en']
|
||||
|
||||
|
||||
def _md_escape(text: str) -> str:
|
||||
escape_chars = r'_*[]()~`>#+-=|{}.!'
|
||||
|
|
@ -74,32 +103,34 @@ def _pad_col(s: str, width: int) -> str:
|
|||
return s + ' ' * (width - cur)
|
||||
|
||||
|
||||
def format_pairings(pairings_data: dict, tournament_name: str) -> str:
|
||||
def format_pairings(pairings_data: dict, tournament_name: str, lang: str = '') -> str:
|
||||
locale = _get_locale(lang)
|
||||
pairings = pairings_data['pairings']
|
||||
rnd = pairings_data['round']
|
||||
|
||||
w_board, w_name, w_rating, w_pts, w_tb1 = 2, 17, 4, 4, 5
|
||||
border = '+'.join('-' * w for w in [w_board, w_name, w_rating, w_pts, w_tb1])
|
||||
|
||||
header = (f'{_pad_col("#", w_board)}|{_pad_col("Игрок", w_name)}|'
|
||||
f'{_pad_col("Рейт", w_rating)}|{_pad_col("О", w_pts)}|'
|
||||
f'{_pad_col("Доп1", w_tb1)}')
|
||||
header = (f'{_pad_col("#", w_board)}|{_pad_col(locale["player"], w_name)}|'
|
||||
f'{_pad_col(locale["rating"], w_rating)}|{_pad_col(locale["points"], w_pts)}|'
|
||||
f'{_pad_col(locale["tb1"], w_tb1)}')
|
||||
table_header = f'```\n{header}\n{border}\n'
|
||||
|
||||
source = pairings_data.get('source', 'algorithm')
|
||||
footer = '\n_FIDE 2025 \(bbpPairings\)_' if source == 'bbp_pairings_fide_2025' else \
|
||||
'\n_Упрощённый Swiss \(bbpPairings недоступен\)_' if source == 'swiss_algorithm_simplified' else ''
|
||||
footer = locale['footer_bbp'] if source == 'bbp_pairings_fide_2025' else \
|
||||
locale['footer_fallback'] if source == 'swiss_algorithm_simplified' else ''
|
||||
|
||||
return {
|
||||
'title': f'📋 *Тур {rnd}* — пары',
|
||||
'rows': _build_table_rows(pairings, w_board, w_name, w_rating, w_pts, w_tb1, border),
|
||||
'title': locale['title'].format(rnd=rnd),
|
||||
'rows': _build_table_rows(pairings, w_board, w_name, w_rating, w_pts, w_tb1, border,
|
||||
locale['bye']),
|
||||
'table_header': table_header,
|
||||
'footer': footer,
|
||||
}
|
||||
|
||||
|
||||
def _build_table_rows(pairings: list, w_board: int, w_name: int, w_rating: int,
|
||||
w_pts: int, w_tb1: int, border: str) -> list:
|
||||
w_pts: int, w_tb1: int, border: str, bye_label: str = '— BYE —') -> list:
|
||||
def _rating_str(p):
|
||||
return str(p.rating) if p.rating else ''
|
||||
|
||||
|
|
@ -123,7 +154,7 @@ def _build_table_rows(pairings: list, w_board: int, w_name: int, w_rating: int,
|
|||
)
|
||||
rows.append(
|
||||
f'{_pad_col("", w_board)}|'
|
||||
f'{_pad_col("— BYE —", w_name)}|'
|
||||
f'{_pad_col(bye_label, w_name)}|'
|
||||
f'{_pad_col("", w_rating)}|'
|
||||
f'{_pad_col("", w_pts)}|'
|
||||
f'{_pad_col("", w_tb1)}'
|
||||
|
|
@ -365,7 +396,8 @@ async def handle_url(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|||
record_request(user.id, url, tournament.get('name', ''), False, str(e))
|
||||
return
|
||||
|
||||
fmt = format_pairings(result, tournament['name'])
|
||||
lang_code = user.language_code or ''
|
||||
fmt = format_pairings(result, tournament['name'], lang_code)
|
||||
chunks = _render_chunks(fmt)
|
||||
|
||||
try:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue