Skip to content

Cleanup and fix loading of translations. #3836

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 1 commit into
base: master
Choose a base branch
from

Conversation

htgoebel
Copy link
Contributor

Pull Request Checklist

  • I’ve read the guidelines for contributing.
  • I updated AUTHORS.txt and CHANGES.txt (if the change is non-trivial) and documentation (if applicable).
  • I tested my changes.

Description

Closes #3835

The new code clearly distinguishes between loading default translations from the "base" theme and loading (additional) translations from the (inherited) themes.

Also the former method put the directories onto sys.path and imported the translation files as modules. This not only keeps the whole module in memory (which might be neglectable nowadays) but also requires extra code to reload the module for each theme. The new method loads the file and executes it using exec().

As a side-effect, this also fixed the bug that translations provided by the "base" theme got lost if another theme provided additional message strings. See #3835.

Output from the tests shown in #3835:

===== Without translation file in theme ---
Posted:              translated to   Veröffentlicht:
My new message       string missing

===== With translation file in theme ---
Posted:              translated to   Veröffentlicht:
My new message       translated to   Meine neue Meldung

===== With translation file containing no strings in theme ---
Posted:              translated to   Veröffentlicht:
My new message       string missing

@htgoebel htgoebel force-pushed the rework-translation-load branch from 6d6850e to 8dfbadd Compare March 23, 2025 20:24
The new code clearly distinguishes between loading default
translations from the "base" theme and loading (additional)
translations from the (inherited) themes.

Also the former method put the directories onto sys.path and imported
the translation files as modules. This not only keeps the whole module
in memory (which might be neglectable nowadays) but also requires
extra code to reload the module for each theme.  The new method loads
the file and executes it using exec().

As a side-effect, this also fixed the bug that translations provided
by the "base" theme got lost if another theme provided additional
message strings.
msg_folder = Path(get_theme_path(theme_name)) / 'messages'
# load English default from this theme
english = load_msgs('en', msg_folder)
messages[lang].update(english) # FIXME: This will overwrite translated strings
Copy link
Member

Choose a reason for hiding this comment

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

That’s not acceptable, please fix that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do. This is just what the former code did, AFAUI.


if not all(found.values()):
print(lang, last_exception)
Copy link
Member

Choose a reason for hiding this comment

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

Leftover debug print.

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.

Translations in themes remove messages provided by Nikola
2 participants