Skip to content

Fix i18n locale detection for Telegram region codes (pt-br -> pt_BR)#1776

Open
mahisafa82 wants to merge 2 commits into
aiogram:dev-3.xfrom
mahisafa82:dev-3.x
Open

Fix i18n locale detection for Telegram region codes (pt-br -> pt_BR)#1776
mahisafa82 wants to merge 2 commits into
aiogram:dev-3.xfrom
mahisafa82:dev-3.x

Conversation

@mahisafa82

@mahisafa82 mahisafa82 commented Feb 25, 2026

Copy link
Copy Markdown

Resolve user locale by checking the Telegram language code as-is, then its Babel-normalized form, then the base language. This fixes lowercase regional codes such as pt-br failing to match available translations like pt_BR. Add tests covering region-code variants and fallback behavior, and update i18n documentation plus changelog notes.

Description

This PR improves locale detection in SimpleI18nMiddleware by resolving the user locale in this order: Telegram language code as received, Babel-normalized locale, then base language. This fixes cases where Telegram returns lowercase regional codes such as pt-br and translations exist under pt_BR, which previously caused fallback to the default locale.

The change also adds test coverage for region-code variants and fallback behavior, updates the i18n documentation to explain this resolution flow, and includes a changelog note.

Fixes #1755

Type of change

  • Documentation (typos, code examples or any documentation update)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

  • Added and updated unit tests for locale resolution behavior, including:
    • Direct match for Telegram locale code
    • Babel-normalized match for region codes
    • Preference when both raw and normalized locale keys exist
    • Base-language fallback for regional codes
  • Verified behavior for examples such as pt-br to pt_BR and en-US to en

Test Configuration:

  • Operating System: Linux 6.19.3-2-cachyos
  • Python version: 3.13.11

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

Resolve user locale by checking the Telegram language code as-is, then
its Babel-normalized form, then the base language. This fixes lowercase
regional codes such as pt-br failing to match available translations like
pt_BR. Add tests covering region-code variants and fallback behavior, and
update i18n documentation plus changelog notes.
@github-actions github-actions Bot added the 3.x Issue or PR for stable 3.x version label Feb 25, 2026
@github-actions

Copy link
Copy Markdown

✔️ Changelog found.

Thank you for adding a description of the changes

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.

Support for lowercase language codes (e.g., pt-br) in i18n middleware

1 participant