Skip to content

Feat: added i18n example (with dynamic translation) #1655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: dev-3.x
Choose a base branch
from

Conversation

mrbuslov
Copy link

Description

This PR introduces an example of dynamic language switching using the aiogram i18n utilities. By default, aiogram handlers store the language context at startup, preventing runtime changes. This PR provides a workaround by leveraging i18n.context() and i18n.use_locale(new_lang) inside the /language command handler, allowing messages to be dynamically translated during execution.

Additionally, a BotI18nMiddleware class has been implemented to manage locale retrieval dynamically, enabling further customization (e.g., fetching the locale from a database).

Fixes # (issue)

Type of change

  • New feature (non-breaking change which adds functionality)
  • Documentation (code examples or any documentation update)

How Has This Been Tested?

This feature has been manually tested by sending /start and /language <new_lang> commands in a bot instance. The output messages correctly reflect the language change in real-time.

  • Tested with different language codes (en, ru, uk) to ensure proper translation.
  • Verified that the middleware correctly sets the default locale.

Test Configuration:

  • Operating System: Linux
  • Python version: 3.11.6

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@github-actions github-actions bot added the 3.x Issue or PR for stable 3.x version label Mar 16, 2025
Copy link

github-actions bot commented Mar 16, 2025

✔️ Changelog found.

Thank you for adding a description of the changes

@mrbuslov
Copy link
Author

Changelog is added

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.x Issue or PR for stable 3.x version
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant