Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from bundle
Use when building or fixing aiogram 3 bots that need aiogram_i18n with Fluent/FTL, topic-aware reply handling, Telegram quote and forward-origin context, and pr
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/i18n-with-aiogram-i18n.md
1# aiogram_i18n with Fluent/FTL23This reference covers runtime usage of `aiogram_i18n` for aiogram 3.45Primary sources:6- Context7 aiogram_i18n library: `/aiogram/i18n`7- Context7 aiogram 3 library: `/aiogram/aiogram/v3.25.0`8- GitHub: https://github.com/aiogram/i18n9- PyPI: https://pypi.org/project/aiogram_i18n/1011Use Context7 first for exact examples and current API shape, then fall back to GitHub or PyPI when you need package prose or details that are not covered in snippets.1213## Installation1415Base package:16```bash17pip install aiogram_i18n18```1920For Fluent Runtime:21```bash22pip install fluent.runtime23```2425For Fluent Compile Core:26```bash27pip install fluent_compiler28```2930## Core setup3132The documented basic runtime setup uses `I18nMiddleware` with `FluentRuntimeCore`.3334Minimal shape:35```python36from aiogram import Bot, Dispatcher, Router37from aiogram_i18n import I18nContext, I18nMiddleware38from aiogram_i18n.cores.fluent_runtime_core import FluentRuntimeCore3940router = Router()4142i18n_middleware = I18nMiddleware(43core=FluentRuntimeCore(44path="locales/{locale}/LC_MESSAGES"45)46)4748dp = Dispatcher()49dp.include_router(router)50i18n_middleware.setup(dispatcher=dp)51```5253This registration pattern is shown in the project README/PyPI description.5455## Locale file layout5657For Fluent Runtime, the documented path shape is:58```text59locales/60en/61LC_MESSAGES/62messages.ftl63uk/64LC_MESSAGES/65messages.ftl66```6768The exact FTL file name is project-defined, but the locale directory structure should match the configured path template.6970## Handler usage7172Handlers can receive `I18nContext` directly and render strings through:73- `i18n.get("key", arg=value)`74- `i18n.key(arg=value)`7576Example:77```python78@router.message(...)79async def handler(message: Message, i18n: I18nContext) -> None:80await message.answer(i18n.get("hello", user=message.from_user.full_name))81```8283## Lazy translations8485The documented package also exposes:86- `LazyProxy`87- `LazyFilter`8889Useful cases:90- localized keyboard button labels91- matching localized button text in filters9293Important package note from the docs:94- if you use `LazyProxy` inside mutable Telegram objects, import those mutable objects from `aiogram_i18n.types`9596## Locale persistence pattern9798`aiogram_i18n` is the runtime i18n layer. Your project still needs to decide how locale is stored.99100A universal pattern is:1011. resolve target locale from callback data, command args, profile, or Telegram user language1022. persist locale in your own storage layer1033. call `await i18n.set_locale(...)`1044. re-render the current screen in the new locale105106Use `scripts/i18n_callback_query.py` for the handler shape.107108## Extraction and generation workflow109110`aiogram_i18n` itself is the runtime layer. Extraction and locale-file generation usually come from a separate tool.111112One concrete FTL-oriented tool is `FTL-Extract`:113- PyPI: https://pypi.org/project/FTL-Extract/114- command: `ftl_extract`115116Example usage from the published tool docs:117```bash118pip install FTL-Extract119ftl_extract project_path/code_path project_path/locales120```121122The same tool also documents multi-locale extraction, for example:123```bash124ftl_extract project_path/code_path project_path/locales -l en -l uk -l pl125```126127And custom i18n key discovery, for example:128```bash129ftl_extract project_path/code_path project_path/locales -k i18n -k LazyProxy -k L130```131132Important boundary:133- this command comes from `FTL-Extract`, not from `aiogram_i18n`134- if a repository uses another extractor or a wrapper command, document that repository-specific command separately135- do not present a project wrapper as universal unless it is actually part of the chosen toolchain136137## Minimal FTL example138139```ftl140hello = Hello, { $user }!141language-select-prompt = Choose your language.142language-changed-success = Language updated.143```144145## Practical rules146147- keep keys stable and semantic148- re-render UI after locale changes149- avoid hard-coded English strings in callbacks150- treat locale storage and locale rendering as separate concerns151