3 horizontal lines, burger
3 horizontal lines, burger
3 horizontal lines, burger
3 horizontal lines, burger

3 horizontal lines, burger
Remove all
LOADING ...

Content



    How to Localize a Telegram Bot using aiogram and Babel

    Clock
    14.01.2025
    /
    Clock
    01.10.2026
    /
    Clock
    2 minutes
    An eye
    4445
    Hearts
    0
    Connected dots
    0
    Connected dots
    0
    Connected dots
    0

    Introduction

    In this article, I’ll show you how I create multilingual Telegram bots using `aiogram` and the `Babel` package.
    I’ll keep the code to an absolute minimum - just the essentials - so if you came here looking for extensive code examples, I apologize.
    That said, every bot I build is translated into at least English, so you can check out any of them for a closer look. `TextifyPic_bot` is the simplest one to start with, while `NotebookOnMarginalia_bot` is a good choice for a more advanced example.

    Translations

    Locating the necessary strings

    Each string needs to be wrapped in the underscore alias. To do this, we import the corresponding module for the bot, and initialize some global variables :
    from aiogram.utils.i18n import gettext as _ i18n = I18n(path="locales", domain="messages") i18n_handler = SimpleI18nMiddleware(i18n) i18n_handler.setup(bot_dispatcher)

    Extracting strings

    Once all the necessary strings have been selected, you can begin extracting them to the locales directory. All strings and their associated data will be saved in the messages.pot file.
    pybabel extract --input-dirs=. -o locales/messages.pot

    Locale initialization

    You need to create exactly as many locales as the number of languages ​​you intend to use. In my case, there are two, and they are created like this:
    pybabel init -i locales/messages.pot -d locales -D messages -l en; pybabel init -i locales/messages.pot -d locales -D messages -l ru;

    Performing the translation

    Once the required locales have been initialized, you can proceed to the actual translation. This means opening the generated .po files and entering the translations.
    You do not need to translate the language in which you write the original texts.

    Compiling translations

    Now, let's compile the translations. We will need to generate special .mo files.
    pybabel compile -d locales -D messages
    Now, when a user with a different language setting visits the bot, the correct strings will be displayed. However, if you need to fix or update translations, a slightly different process is used.

    Updating translations

    First, we extract the changes from all the translation files.
    pybabel extract --input-dirs=. -o locales/messages.pot
    Afterwards we update all locales
    pybabel update -d locales -D messages -i locales/messages.pot
    We update/fix the translations. And finally, we compile the translations:
    pybabel compile -d locales -D messages
    That’s it - the translations have been updated.

    Conclusion

    Creating translations for Telegram bots isn't exactly simple and can be quite a headache for someone doing it for the first time.
    The main thing is to grasp the initialization process:
    1. Wrap the necessary strings.
    2. Extract all wrapped strings into a single master file.
    3. Initialize the required locales (languages).
    4. Translate.
    5. Compile.
    To update the translations:
    1. Extract all wrapped strings into a single master file.
    2. Update the existing locales.
    3. Translate.
    4. Compile.
    In my opinion, the most difficult step is likely the translation itself; the rest is just a matter of procedure.

    Do not forget to share, like and leave a comment :)

    Comments

    (0)

    captcha
    Send
    LOADING ...
    It's empty now. Be the first (o゚v゚)ノ

    Other

    Similar articles


    How to get a telegram bot token and initial setup for telegram bot

    Clock
    14.01.2025
    /
    Clock
    12.09.2026
    An eye
    9825
    Hearts
    0
    Connected dots
    0
    Connected dots
    0
    Connected dots
    0
    In this article you will see how I get the telegram bot token (via bot father), where I store the token and how I set it up

    Full localization for a Django website

    Clock
    06.02.2025
    /
    Clock
    12.09.2026
    An eye
    5188
    Hearts
    0
    Connected dots
    0
    Connected dots
    0
    Connected dots
    0
    I'll show you how to completely make localization a Django website, describe the full process: setting up, extracting, collecting, and compiling translations. Covering all three translation scenarios, including files such …

    Fast implementation of a RESTful API for a Django website

    Clock
    24.02.2025
    /
    Clock
    12.09.2026
    An eye
    1360
    Hearts
    1
    Connected dots
    0
    Connected dots
    0
    Connected dots
    0
    Implementing a REST API for a Django website in short, clear steps with minimal code required. Also included is a demonstration of backend interaction via the client side of the …