Skip to content

refactor translation key generation in Jinja2Core#42

Open
loRes228 wants to merge 5 commits intoaiogram:devfrom
loRes228:dev
Open

refactor translation key generation in Jinja2Core#42
loRes228 wants to merge 5 commits intoaiogram:devfrom
loRes228:dev

Conversation

@loRes228
Copy link

Keys are now generated based on the relative file path, not just the stem file.
This allows you to support a nested template structure, e.g:

locales/en/
  main/
    hello.j2
    buttons/
      settings.j2
      profile.j2

They can be accessed like this:

i18n.main.hello()
i18n.main.buttons.settings()

@JrooTJunior JrooTJunior requested a review from Copilot April 19, 2025 12:24
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the translation key generation in the Jinja2Core module to support a nested template structure by generating keys based on the relative file paths.

  • Introduces a new parameter for a key separator.
  • Constructs keys by joining parts of the file path relative to the locale folder.

@JrooTJunior JrooTJunior requested a review from Copilot April 23, 2025 11:56
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors how translation keys are generated in Jinja2Core to support nested template structures.

  • Introduces a key_separator parameter (defaulting to "-") in the initializer
  • Replaces the use of file_path.stem with a joined key based on the relative file path parts
  • Updates key generation in translations to support nested directories
Comments suppressed due to low confidence (2)

aiogram_i18n/cores/jinja2_core.py:25

  • The default key_separator '-' may result in keys like 'main-buttons-settings' which do not directly match the dot notation (e.g., i18n.main.buttons.settings()) suggested in the PR description. Consider using '.' as the default delimiter if attribute-style access is intended.
key_separator: str = "-",

aiogram_i18n/cores/jinja2_core.py:56

  • Consider adding tests for nested file paths to ensure that the new key generation works correctly in various directory structures.
relative_file_path = file_path.relative_to(self.path / locale)

@loRes228
Copy link
Author

@andrew000

@andrew000
Copy link
Contributor

@andrew000

I'll check it soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants