Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions dodo.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,13 @@ def generate_mo():
"""

def check_mo():
# The sample translations below include ja/ko/zh text, which a Windows console
# (cp1252) cannot print. Force UTF-8 so validation output is readable everywhere.
# doit replaces sys.stdout with its own writer while a task runs, so reconfigure
# the underlying console stream as well.
for stream in (sys.stdout, sys.__stdout__):
if hasattr(stream, "reconfigure"):
stream.reconfigure(encoding="utf-8", errors="replace")
print("\n****** Validate all generated .mo files")
for current_locale in LOCALES:
LANG_DIR = os.path.join("tabcmd", "locales")
Expand Down
6 changes: 6 additions & 0 deletions tabcmd/execution/localize.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ def define_locale_dir(logger):
def _load_language(current_locale, domain, logger):
locale_dir = define_locale_dir(logger)
language: gettext.NullTranslations = gettext.translation(domain, locale_dir, languages=[current_locale])
if current_locale != "en":
# Messages not yet translated fall back to English instead of showing the raw key.
try:
language.add_fallback(gettext.translation(domain, locale_dir, languages=["en"]))
except Exception as e:
logger.debug("Could not load English fallback catalog: {}".format(e))
language.install() # I believe this is the expensive call
_ = language.gettext
return _
Expand Down
13 changes: 13 additions & 0 deletions tests/test_localize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import logging
import unittest

from tabcmd.execution import localize


class LocalizeFallbackTests(unittest.TestCase):
def test_untranslated_key_falls_back_to_english(self):
key = "session.errors.password_file_not_found"
translate = localize._load_language("de", "tabcmd", logging.getLogger("test"))
text = translate(key)
assert text != key, text
assert "{0}" in text, text
Loading