| ⬅️ Back | 🏠 Docs Root |
The I18n Middleware is responsible for determining and managing the user’s preferred language (locale) throughout the bot’s lifecycle.
Located in: src/telegram_bot/middlewares/i18n_middleware.py
This class extends aiogram_i18n.managers.BaseManager to provide a custom strategy for locale resolution and persistence.
get_locale)When an update arrives, the manager determines the locale in the following order of priority:
locale key exists in the user’s FSM data. This is the highest priority as it represents an explicit user choice.language_code provided by the Telegram API. Currently supports ru and de.de (German).set_locale)When a user changes their language (e.g., via a settings menu), the set_locale method is called. It updates the user’s FSM data with the new locale key, ensuring the choice persists across sessions.
Unlike other middlewares, the I18nMiddleware is initialized and registered in src/telegram_bot/core/factory.py because it requires specific setup for the Fluent core and the FSM manager.
i18n_middleware = I18nMiddleware(
core=FluentRuntimeCore(path=locales_path),
manager=FSMContextI18nManager(),
default_locale="de",
)
i18n_middleware.setup(dp)
language_code for a seamless first-time experience.