I
I
insendiofx2017-08-11 19:04:39
Python
insendiofx, 2017-08-11 19:04:39

Python Telebot, How to implement a multilingual bot?

Hello, I am writing a telegram bot in Python using telebot .
I would like to add to it several languages ​​​​in addition to Russian. I tried to think about the logic of the bot myself, but so far I have not succeeded.
Created a configuration file cons.py :

token = "427778629:-oWX9DXZgTX5TXi0I8e0N3s4"
lang = 'ru'
start_msg = 'Здравствуйте, уважаемый клиент!\nВас приветствует бот компании!\nПожалуйста, выберите из меню то что вас интересует.'
order_menu = 'Заказать такси'
info_menu = 'Информация'
exit_menu = 'Покинуть бота'

And main.py :
...
import telebot
import cons
...
bot = telebot.TeleBot(cons.token)
@bot.message_handler(commands=['start'])
def request_contact(message):
    keyboard = types.ReplyKeyboardMarkup(row_width=1, resize_keyboard=True)
    button_contact = types.KeyboardButton(text="START", request_contact=True)
    keyboard.add(button_contact)
    msg = bot.send_message(message.chat.id, "Ув.пользовател, для начало работы с ботом нажмите на кнопку 'START'  ", reply_markup=keyboard)
    bot.register_next_step_handler(msg, start)

def start(message):
    #Переводы
    if cons.lang == 'ru':
      start_msg = 'Здравствуйте, уважаемый клиент!\nВас приветствует бот компании!\nПожалуйста, выберите из меню то что вас интересует.'
      order_menu = 'Заказать такси'
      info_menu = 'Информация'
      exit_menu = 'Покинуть бота'
    elif cons.lang == 'en':
      start_msg = 'Grettings.'
      order_menu = 'Order Taxi'
      info_menu = 'Information'
      exit_menu = 'Leave bot'

  # Главное меню
    start_markup = types.ReplyKeyboardMarkup(True, False)
    start_markup.row(' '+cons.order_menu+' ', )
    start_markup.row('ℹ️ '+cons.info_menu+' ℹ️')
    start_markup.row('English', 'Русский язык')
    start_markup.row(' '+cons.exit_menu+' ')
    bot.send_message(message.chat.id, cons.start_msg, reply_markup=start_markup)

@bot.message_handler(content_types=["text"])
def main(message): 
    if message.text == 'English':
        cons.lang = 'en'
        msg = bot.send_message(message.chat.id, 'Your language is -'+cons.lang+' -', reply_markup=start_markup)
        bot.register_next_step_handler(msg, start)
    elif message.text == 'Русский язык':
        cons.lang = 'ru'
        msg = bot.send_message(message.chat.id, 'Вы выбрали русский язык-'+cons.lang+' -', reply_markup=start_markup)
        bot.register_next_step_handler(msg, start)

In my case, this code works, well, how it works :D I have to press the button on "English" 2 times and the language of the bot changes, but not only for one user as it should, and for everyone else it also changes ...
I hope for your help, thank you in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artyom Belousov, 2017-08-13
@insendiofx

Modules designed to solve the problems of internationalization of the gettext type are designed to be executed locally by the user. Those. in your case, it will be 1 language - 1 bot.
Therefore, I would do this:
0) Create a dictionary of type

languages = {
    'russian': {
       'Hello': 'Привет',
    },
}

1) First, ask the user for his language, then enter it into the database, and give him data like , where language is the language value from the database for this chat languages[language]["Hello"]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question