-
Notifications
You must be signed in to change notification settings - Fork 464
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
htgoebel
wants to merge
1
commit into
getnikola:master
Choose a base branch
from
htgoebel:rework-translation-load
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,7 @@ | |
from collections import defaultdict, OrderedDict | ||
from html import unescape as html_unescape | ||
from importlib import reload as _reload | ||
from pathlib import Path | ||
from unicodedata import normalize as unicodenormalize | ||
from urllib.parse import quote as urlquote | ||
from urllib.parse import unquote as urlunquote | ||
|
@@ -710,41 +711,48 @@ def load_messages(themes, translations, default_lang, themes_dirs): | |
and "younger" themes have priority. | ||
""" | ||
messages = Functionary(dict, default_lang) | ||
oldpath = list(sys.path) | ||
found = {lang: False for lang in translations.keys()} | ||
found = {lang: False for lang in translations} | ||
completion_status = found.copy() | ||
last_exception = None | ||
completion_status = {lang: False for lang in translations.keys()} | ||
|
||
def load_msgs(lang, folder): | ||
globals = {} | ||
msg_file = folder / f'messages_{lang}.py' | ||
try: | ||
pysrc = msg_file.read_bytes() | ||
exec(pysrc, globals) | ||
except Exception as ex: | ||
last_exception = ex # noqa: F841 | ||
return globals.get('MESSAGES', {}) | ||
|
||
# load default translations provided by "base" theme | ||
default_folder = Path(get_theme_path_real('base', themes_dirs)) / 'messages' | ||
english = load_msgs('en', default_folder) | ||
for lang in translations: | ||
messages[lang].update(english) | ||
messages[lang].update(load_msgs(lang, default_folder)) | ||
|
||
# load translations for each theme | ||
for theme_name in themes[::-1]: | ||
msg_folder = os.path.join(get_theme_path(theme_name), 'messages') | ||
default_folder = os.path.join(get_theme_path_real('base', themes_dirs), 'messages') | ||
sys.path.insert(0, default_folder) | ||
sys.path.insert(0, msg_folder) | ||
|
||
english = __import__('messages_en') | ||
# If we don't do the reload, the module is cached | ||
_reload(english) | ||
for lang in translations.keys(): | ||
try: | ||
translation = __import__('messages_' + lang) | ||
# If we don't do the reload, the module is cached | ||
_reload(translation) | ||
found[lang] = True | ||
if sorted(translation.MESSAGES.keys()) != sorted(english.MESSAGES.keys()): | ||
completion_status[lang] = completion_status[lang] or False | ||
else: | ||
completion_status[lang] = True | ||
|
||
messages[lang].update(english.MESSAGES) | ||
for k, v in translation.MESSAGES.items(): | ||
if v: | ||
messages[lang][k] = v | ||
del translation | ||
except ImportError as orig: | ||
last_exception = orig | ||
del english | ||
sys.path = oldpath | ||
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 | ||
for lang in translations: | ||
translation = load_msgs(lang, msg_folder) | ||
if not translation: | ||
continue | ||
found[lang] = True | ||
for k, v in translation.items(): | ||
if v: | ||
messages[lang][k] = v | ||
if set(translation) != set(english): | ||
completion_status[lang] = completion_status[lang] or False | ||
else: | ||
completion_status[lang] = True | ||
|
||
if not all(found.values()): | ||
print(lang, last_exception) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leftover debug print. |
||
raise LanguageNotFoundError(lang, last_exception) | ||
for lang, status in completion_status.items(): | ||
if not status and lang not in INCOMPLETE_LANGUAGES_WARNED: | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.