Our bot example will consist of 2 *.py files, main.pyand config.py. The first will contain the entry point and the corresponding handlers, and the second will contain the settings for translations. The code for the example was taken from the Bot-example for connecting the Inline mode.
Necessary code and setup
As I promised, we do translations. Firstly, create a locales directory in the project folder. Then add the following imports to the main.py file:
In the config.py file, we will set up a directory for storing translations and middleware, respectively.
Marking strings for translation
After we have connected the relevant modules and set up the environment, we need to mark which strings we are going to translate. In main.py I created an alias.
Each line will need to be wrapped around the alias of gettext. The end result will look something like this:
Extracting strings
Finally, you can start extracting strings to the locales folder. All strings and their data will be saved in the messages.pot file.
Initializing locales and translated languages
You will need to make exactly as many locales as languages you are going to use. In my case, there are 2, and they are made like this:
Translate
The first is English, the second is Russian. In the first case, after generation, you will need to write a translation. You will need to find the messages.po file in locales\en\LC_MESSAGES and write translations. In the second case, this is the original language. No need to do translations.
Compiling translations
Now let's compile the translations
Updating translations
The translations are ready. But what if you wanted to make changes to the translations or added another text? In this case, you need to update the translations. And to do this, you will have to perform slightly different steps than when initializing in the first steps.
It is important to remember that we initialize only once. Then, unless you want to lose all the translations, you will need to update them in a special way. First, extract the changes from all files with translation strings.
Afterward, we update all locales
And finally compile the translations.
That's it, the translations have been updated and will be shown depending on the user's Telegram client settings.
Conclusion
Translations for bots in Telegram are not so easy and can cause quite a headache for those who do it for the first time. The most difficult step, in my opinion, is finding and wrapping the lines you need for translation. Because not all lines can be wrapped, but if you follow the basic rules, everything should work out. ( ̄︶ ̄)↗
In this article you will see how I start creating bots for telegram, where I store the token and how I set it up. Including how to enable inline mode …
In this article, I will show how you can add localization and translations to a Django website(i18n). We will translate Python, JS code, as well as templates and Django-models. Plus, …
This article describes the process of setting up and adding a REST framework to a site written in Django. It is added in order to build an API for access …
Used termins
Django template ⟶ This is a text document marked up with a special syntax for inserting new code.
Django model ⟶ Is a database manager used in the Django framework. Implemented via classes and inheritance in python.
Python dictionary ⟶ Composite data type, and structure which has one more name, associative array. The python dictionary has keys and associating values.
Python programming language ⟶ It is interpreted, build upon object-oriented approach, with dynamic semantics, and yes it s high-level programming language. Actively using for rapid development, scripting and gluing part of existing apps.
Javascript ⟶ Is a high-level, interpreted programming language that is commonly used for web development. It is an essential part of web applications, enabling interactive features and dynamic content on websites.
Related questions
How do I extract content from dynamic web pages?
A dynamic website would update the data frequently. For example, there are always new posts on Twitter. To scrape from such a website, it is the same process as scraping other websites, but it would allow the scraper to access the website with some frequency to get the continuously updated data.
What is the best way to combine fonts?
Overloading the website with different fonts is never a good idea, but it doesn’t hurt to combine just a few simple fonts on each page. The general rule is to incorporate two fonts that look quite similar with one noticeable distinction.