P
P
pin4uck2021-04-24 20:36:11
Python
pin4uck, 2021-04-24 20:36:11

What is the best and correct way to use gettext?

Hello. I'm learning python and now I want to understand gettext. I created a telegram bot and I'm improving it little by little. I figured out how pygettext works a little (I tried to translate using a simple file as an example). But I don’t know how to better or correctly translate the messages that the bot displays after entering commands. That is, it is better to do so that, for example, when entering the 'start' command, the bot displays the message "select a language" - and then all messages would be translated into the desired language? Or which is better? I work in windows (pycharm). I'm a beginner so don't judge too harshly. I will be glad to any information, any examples.
Link to my bot
github.com/Pin4uck/Bot_Testik

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
shurshur, 2021-04-25
@shurshur

gettext in its simplest form assumes the choice of one language, which is then used throughout the life cycle of the application.
You either need to switch the language to the language of a particular user in all the right places (which will lead to frequent switching and is utterly incompatible with asynchronous code), or use gettext in class-based API mode.
Related question How to make a python application multilingual?

I
Ilya Evseev, 2021-04-25
@IlyaEvseev

Google search:
"telegram bot get user language"
Find:
https://stackoverflow.com/a/63790087/2743554

Each Update from Telegram contains User information (in Message, in InlineQuery or some other). This language_code is based on which language is set as default on user's device or set for Telegram app. You can trust it.
But some users may want to change a language just for your bot. For that you can add your custom "Settings" to your bot menu, with all languages ​​you support.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question