Skip to content

Commit dd1ddd0

Browse files
committedMar 18, 2018
Restore ability to override messages partially
The fix in 064d63f broke language overrides (changing only some messages in custom themes and letting Nikola fill in the rest from `base`). This commit makes both work. Signed-off-by: Chris Warrick <kwpolska@gmail.com>
1 parent a4e358e commit dd1ddd0

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed
 

‎nikola/utils.py

+29-23
Original file line numberDiff line numberDiff line change
@@ -700,35 +700,41 @@ def load_messages(themes, translations, default_lang, themes_dirs):
700700
"""
701701
messages = Functionary(dict, default_lang)
702702
oldpath = list(sys.path)
703+
found = {lang: False for lang in translations.keys()}
704+
last_exception = None
703705
for theme_name in themes[::-1]:
704706
msg_folder = os.path.join(get_theme_path(theme_name), 'messages')
705707
default_folder = os.path.join(get_theme_path_real('base', themes_dirs), 'messages')
706708
sys.path.insert(0, default_folder)
707709
sys.path.insert(0, msg_folder)
708710

709-
english = __import__('messages_en')
710-
# If we don't do the reload, the module is cached
711-
_reload(english)
712-
for lang in list(translations.keys()):
713-
try:
714-
translation = __import__('messages_' + lang)
715-
# If we don't do the reload, the module is cached
716-
_reload(translation)
717-
if sorted(translation.MESSAGES.keys()) !=\
718-
sorted(english.MESSAGES.keys()) and \
719-
lang not in language_incomplete_warned:
720-
language_incomplete_warned.append(lang)
721-
LOGGER.warn("Incomplete translation for language "
722-
"'{0}'.".format(lang))
723-
messages[lang].update(english.MESSAGES)
724-
for k, v in translation.MESSAGES.items():
725-
if v:
726-
messages[lang][k] = v
727-
del(translation)
728-
except ImportError as orig:
729-
raise LanguageNotFoundError(lang, orig)
730-
del(english)
731-
sys.path = oldpath
711+
english = __import__('messages_en')
712+
# If we don't do the reload, the module is cached
713+
_reload(english)
714+
for lang in translations.keys():
715+
try:
716+
translation = __import__('messages_' + lang)
717+
# If we don't do the reload, the module is cached
718+
_reload(translation)
719+
found[lang] = True
720+
if sorted(translation.MESSAGES.keys()) !=\
721+
sorted(english.MESSAGES.keys()) and \
722+
lang not in language_incomplete_warned:
723+
language_incomplete_warned.append(lang)
724+
LOGGER.warn("Incomplete translation for language "
725+
"'{0}'.".format(lang))
726+
messages[lang].update(english.MESSAGES)
727+
for k, v in translation.MESSAGES.items():
728+
if v:
729+
messages[lang][k] = v
730+
del(translation)
731+
except ImportError as orig:
732+
last_exception = orig
733+
del(english)
734+
sys.path = oldpath
735+
if not all(found.values()):
736+
raise LanguageNotFoundError(lang, last_exception)
737+
732738
return messages
733739

734740

0 commit comments

Comments
 (0)
Please sign in to comment.