From 195af0c850dfa3476731c9e301afbe7f1956d218 Mon Sep 17 00:00:00 2001 From: Bernd Hahnebach Date: Wed, 8 Dec 2021 15:19:36 +0100 Subject: [PATCH] bimtester: do not break bimtester if translation files are missing, fixes #1910 --- src/ifcbimtester/bimtester/lang.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/ifcbimtester/bimtester/lang.py b/src/ifcbimtester/bimtester/lang.py index 60caf519cc..3a8ca0d393 100644 --- a/src/ifcbimtester/bimtester/lang.py +++ b/src/ifcbimtester/bimtester/lang.py @@ -8,8 +8,21 @@ def _(message): return message -def switch_locale(locale_dir, locale_id="en"): +def switch_locale(locale_dir, locale_id): global translation - newlang = gettext.translation("messages", localedir=locale_dir, languages=[locale_id]) - newlang.install() - translation = newlang.gettext + domain = "messages" + # https://docs.python.org/3/library/gettext.html + mo_file = gettext.find(domain, localedir=locale_dir, languages=[locale_id]) + if mo_file is not None: + print( + "Locale will be switched to language '{}'. Translation file found {}" + .format(locale_id, mo_file) + ) + newlang = gettext.translation(domain, localedir=locale_dir, languages=[locale_id]) + newlang.install() + translation = newlang.gettext + else: + print( + "Locale can not be switched to '{}'. Translation file (*.mo) not found in {}" + .format(locale_id, locale_dir) + )