In order to make you bot translatable you have to add a minimal number of hooks to your Python code.
These hooks are called translation strings.
The aiogram translation utils is build on top of `GNU gettext Python module <https://docs.python.org/3/library/gettext.html>`_
and `Babel library <http://babel.pocoo.org/en/latest/>`_.
Installation
============
Babel is required to make simple way to extract translation strings from your code
Can be installed from pip directly:
..code-block:: bash
pip install Babel
or as `aiogram` extra dependency:
..code-block:: bash
pip install aiogram[i18n]
Make messages translatable
==========================
In order to gettext need to know what the strings should be translated you will need to write translation strings.
For example:
..code-block:: python
:emphasize-lines:6-8
from aiogram import html
from aiogram.utils.i18n import gettext as _
async def my_handler(message: Message) -> None:
await message.answer(
_("Hello, {name}!").format(
name=html.quote(message.from_user.full_name)
)
)
..danger::
f-strings can't be used as translations string because any dynamic variables should be added to message after getting translated message
Also if you want to use translated string in keyword- or magic- filters you will need to use lazy gettext calls:
..code-block:: python
:emphasize-lines:4
from aiogram import F
from aiogram.utils.i18n import lazy_gettext as __
@router.message(F.text.lower() == __("My menu entry"))
...
..danger::
Lazy gettext calls should always be used when the current language is not know at the moment
..danger::
Lazy gettext can't be used as value for API methods or any Telegram Object (like :class:`aiogram.types.inline_keyboard_button.InlineKeyboardButton` or etc.)
Configuring engine
==================
After you messages is already done to use gettext your bot should know how to detect user language