commit 80673c53a72dd310d14ac5c5c10fc10c96f8275d Author: hiro hiro@torproject.org Date: Wed May 22 19:22:51 2019 +0200
Fix load locales --- gettor/utils/strings.py | 12 +++++++----- tests/test_locales.py | 4 ++++ 2 files changed, 11 insertions(+), 5 deletions(-)
diff --git a/gettor/utils/strings.py b/gettor/utils/strings.py index 6b3a1d7..1538fe4 100644 --- a/gettor/utils/strings.py +++ b/gettor/utils/strings.py @@ -92,22 +92,24 @@ def get_locales(): locales = json.load(f) return locales
-def load_strings(current_locale='en'): +def load_strings(current_locale): """ Loads translated strings and fallback to English if the translation does not exist. """ global strings, translations
+ # Load all translations translations = {} available_locales = get_locales()
- for locale in available_locales: + if current_locale not in available_locales: + current_locale = "en"
- filename = get_resource_path("{}.json".format(locale), '../share/locale') - with open(filename, encoding='utf-8') as f: - translations[locale] = json.load(f) + filename = get_resource_path("{}.json".format(current_locale), '../share/locale') + with open(filename, encoding='utf-8') as f: + translations[current_locale] = json.load(f)
strings = {} for s in translations[current_locale]: diff --git a/tests/test_locales.py b/tests/test_locales.py index cc43485..39cdac5 100644 --- a/tests/test_locales.py +++ b/tests/test_locales.py @@ -25,6 +25,10 @@ class EmailServiceTests(unittest.TestCase): conftests.strings.load_strings("en") self.assertEqual(conftests.strings._("smtp_mirrors_subject"), "[GetTor] Mirrors")
+ def test_load_default_strings(self): + conftests.strings.load_strings(None) + self.assertEqual(conftests.strings._("smtp_mirrors_subject"), "[GetTor] Mirrors") + def test_load_es_strings(self): conftests.strings.load_strings("es") self.assertEqual(conftests.strings._("smtp_help_subject"), "[GetTor] Ayuda")