diff --git a/src/boot.py b/src/boot.py index f7ed98c2..b1322116 100644 --- a/src/boot.py +++ b/src/boot.py @@ -60,21 +60,20 @@ def login(ctx_login): login_start_from = None while True: - login = krux.pages.login.Login(ctx_login) - if not login.run(login_start_from): + login_page = krux.pages.login.Login(ctx_login) + if not login_page.run(login_start_from): break if ctx_login.wallet is not None: # Have a loaded wallet - del login + del login_page break # Login closed due to change of locale at Settings login_start_from = ( krux.pages.login.Login.SETTINGS_MENU_INDEX ) # will start Login again from Settings index - del login - + del login_page # Unimport login del krux.pages.login diff --git a/src/krux/krux_settings.py b/src/krux/krux_settings.py index cd205364..3c3888d2 100644 --- a/src/krux/krux_settings.py +++ b/src/krux/krux_settings.py @@ -30,8 +30,8 @@ MAIN_TXT, TEST_TXT, ) -from .translations import translation_index from .key import SCRIPT_LONG_NAMES +from krux.translations import translation_index BAUDRATES = [1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200] @@ -54,16 +54,21 @@ THERMAL_ADAFRUIT_TXT = "thermal/adafruit" TRANSLATION_LOOKUP_TABLE = "translation_dict" +DEFAULT_LOCALE = "en-US" -def translations(locale): +def _get_locale_module(locale): + """Get the name of the module based on the locale""" + return "krux.translations_" + locale.replace("-", "_") + + +def _translations(locale): """Returns the translations map for the given locale""" - if locale == "en-US": + if locale == DEFAULT_LOCALE: return None - locale = locale.replace("-", "_") translations_module = __import__( - "krux.translations_" + locale, None, None, TRANSLATION_LOOKUP_TABLE + _get_locale_module(locale), None, None, TRANSLATION_LOOKUP_TABLE ) return getattr(translations_module, TRANSLATION_LOOKUP_TABLE) @@ -72,7 +77,7 @@ def translations(locale): def t(slug): """Translates a slug according to the current locale""" slug_id = binascii.crc32(slug.encode("utf-8")) - lookup = translations(Settings().i18n.locale) + lookup = _translations(Settings().i18n.locale) if not lookup or slug_id not in lookup: return slug return lookup[slug_id] @@ -101,9 +106,8 @@ class I18nSettings(SettingsNamespace): """I18n-specific settings""" namespace = "settings.i18n" - DEFAULT_LOCALE = "en-US" locale = CategorySetting( - "locale", DEFAULT_LOCALE, translation_index + [DEFAULT_LOCALE] + "locale", DEFAULT_LOCALE, list(translation_index) + [DEFAULT_LOCALE] ) def label(self, attr): @@ -406,6 +410,14 @@ class Settings(SettingsNamespace): namespace = "settings" + # Make sure only one instance is created (Singleton) + _instance = None + + def __new__(cls): + if cls._instance is None: + cls._instance = super().__new__(cls) + return cls._instance + def __init__(self): self.wallet = DefaultWallet() self.security = SecuritySettings() diff --git a/tests/test_i18n.py b/tests/test_i18n.py index 23becbf8..3197bfd5 100644 --- a/tests/test_i18n.py +++ b/tests/test_i18n.py @@ -13,7 +13,7 @@ def tdata(mocker): def test_translations(mocker, m5stickv, tdata): import binascii - from krux.krux_settings import translations + from krux.krux_settings import _translations cases = [ (tdata[0], None), @@ -22,7 +22,7 @@ def test_translations(mocker, m5stickv, tdata): for case in cases: mocker.patch("krux.krux_settings.translation_index", list(case[0].keys())[0]) mocker.patch("krux.translations_es_MX.translation_dict", case[1]) - lookup = translations(list(case[0].keys())[0]) + lookup = _translations(list(case[0].keys())[0]) assert lookup == case[1]