def locale(bot, update, args): chat = update.effective_chat if len(args) > 0: locale = args[0].lower() if locale in list_locales: if locale in ('en', 'ru', 'ua', 'es', 'tr', 'id', 'it'): switch_to_locale(chat.id, locale) update.message.reply_text( tld(chat.id, 'Switched to {} successfully!').format( list_locales[locale])) else: update.message.reply_text("{} is not supported yet!".format( list_locales[locale])) else: update.message.reply_text( "Is that even a valid language code? Use an internationally accepted ISO code!" ) else: LANGUAGE = prev_locale(chat.id) if LANGUAGE: locale = LANGUAGE.locale_name native_lang = list_locales[locale] update.message.reply_text( "Current locale for this chat is: *{}*".format(native_lang), parse_mode=ParseMode.MARKDOWN) else: update.message.reply_text( "Current locale for this chat is: *English*", parse_mode=ParseMode.MARKDOWN)
def locale_button(bot, update): chat = update.effective_chat user = update.effective_user # type: Optional[User] query = update.callback_query lang_match = re.findall(r"en|ru|ua|es|tr|id", query.data) if lang_match: if lang_match[0]: switch_to_locale(chat.id, lang_match[0]) query.answer(text="Language changed!") else: query.answer(text="Error!", show_alert=True) try: LANGUAGE = prev_locale(chat.id) locale = LANGUAGE.locale_name curr_lang = list_locales[locale] except: curr_lang = "English" if curr_lang == "Indonesian": text = "*Select language* \n" text += "User language : `Sinhala`" else: text = "*Select language* \n" text += "User language : `{}`".format(curr_lang) conn = connected(bot, update, chat, user.id, need_admin=False) if not conn == False: try: chatlng = prev_locale(conn).locale_name chatlng = list_locales[chatlng] if chatlng == "Indonesian": text += "\nConnected chat language : `Sinhala`" else: text += "\nConnected chat language : `{}`".format(chatlng) except: chatlng = "English" text += "*\n\nSelect new user language:*" query.message.reply_text( text, parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup([[ InlineKeyboardButton("English 🇺🇸", callback_data="set_lang_en") ]] + [[ InlineKeyboardButton("Russian 🇷🇺", callback_data="set_lang_ru"), InlineKeyboardButton("Ukrainian 🇺🇦", callback_data="set_lang_ua") ]] + [[ InlineKeyboardButton("Spanish 🇪🇸", callback_data="set_lang_es"), InlineKeyboardButton("Turkish 🇹🇷", callback_data="set_lang_tr") ]] + [[ InlineKeyboardButton("Sinhala 🇱🇰", callback_data="set_lang_id") ]] + [[InlineKeyboardButton("⬅️ Back", callback_data="bot_start")]])) print(lang_match) query.message.delete() bot.answer_callback_query(query.id)
def locale(bot, update, args): chat = update.effective_chat if len(args) > 0: locale = args[0].lower() if locale in list_locales: if locale in ('en', 'id'): switch_to_locale(chat.id, locale) update.message.reply_text( tld(chat.id, 'language_switch_success').format( list_locales[locale])) else: update.message.reply_text( tld(chat.id, "language_not_supported").format(list_locales[locale])) else: update.message.reply_text(tld(chat.id, "language_code_not_valid")) else: LANGUAGE = prev_locale(chat.id) if LANGUAGE: locale = LANGUAGE.locale_name native_lang = list_locales[locale] update.message.reply_text(tld( chat.id, "language_current_locale").format(native_lang), parse_mode=ParseMode.MARKDOWN) else: update.message.reply_text(tld( chat.id, "language_current_locale").format("English"), parse_mode=ParseMode.MARKDOWN)
def locale_button(bot, update): chat = update.effective_chat user = update.effective_user # type: Optional[User] query = update.callback_query lang_match = re.findall(r"en|id", query.data) if lang_match: if lang_match[0]: switch_to_locale(chat.id, lang_match[0]) query.answer(text=tld(chat.id, 'language_switch_success').format( list_locales[lang_match[0]])) else: query.answer(text="Error!", show_alert=True) try: LANGUAGE = prev_locale(chat.id) locale = LANGUAGE.locale_name curr_lang = list_locales[locale] except: curr_lang = "English" text = tld(chat.id, "language_select_language") text += tld(chat.id, "language_user_language").format(curr_lang) conn = connected(bot, update, chat, user.id, need_admin=False) if conn: try: chatlng = prev_locale(conn).locale_name chatlng = list_locales[chatlng] text += tld(chat.id, "language_chat_language").format(chatlng) except: chatlng = "English" text += tld(chat.id, "language_sel_user_lang") query.message.reply_text( text, parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup([[ InlineKeyboardButton("English 🇺🇸", callback_data="set_lang_en") ]] + [[ InlineKeyboardButton("Indonesian 🇮🇩", callback_data="set_lang_id") ]] + [[ InlineKeyboardButton(f"⬅️ {tld(chat.id, 'btn_go_back')}", callback_data="bot_start") ]])) print(lang_match) query.message.delete() bot.answer_callback_query(query.id)