diff --git a/chstrings/__init__.py b/chstrings/__init__.py index e5c762d..11818c8 100644 --- a/chstrings/__init__.py +++ b/chstrings/__init__.py @@ -159,11 +159,16 @@ def _load_strings_for_lang_tag(lang_tag): return json.load(json_fp) def get_localized_strings(config, lang_tag): - strings = _load_strings_for_lang_tag(config.fallback_lang_tag) + localized_strings = {} try: - strings.update(_load_strings_for_lang_tag(lang_tag)) + localized_strings = _load_strings_for_lang_tag(lang_tag) except: - pass + return localized_strings + + # Complete the strings with the fallback in case of incomplete + # translations, but only if there is a translation at all. + strings = _load_strings_for_lang_tag(config.fallback_lang_tag) + strings.update(localized_strings) strings = _preprocess_variables(config, strings) _partition_js_strings(strings) return strings diff --git a/chstrings/chstrings_test.py b/chstrings/chstrings_test.py index c234c19..1fcd8aa 100644 --- a/chstrings/chstrings_test.py +++ b/chstrings/chstrings_test.py @@ -43,6 +43,15 @@ def _load_strings_side_effect(lang_tag): self.assertEqual(fallback_strings['instructions_goal'], strings['instructions_goal']) + def test_missing_lang_tag_has_no_fallback(self): + # We must only apply the fallback strings if there is an incomplete + # strings file (test_fallback_lang_tag exercises that behavior). + # If the file does not exist at all, we must return {} rather than just + # the fallback strings. + self.assertEqual( + chstrings.get_localized_strings(cfg, 'fake'), {}) + + if __name__ == '__main__': for lc in config.LANG_CODES_TO_LANG_NAMES: cfg = config.get_localized_config(lc, api = False) diff --git a/handlers/common.py b/handlers/common.py index df8cbb7..e03de10 100644 --- a/handlers/common.py +++ b/handlers/common.py @@ -56,7 +56,7 @@ def load_strings_for_request(lang_code, cfg, accept_language_hdr): header_locales = accept_language_hdr.split(',') for l in header_locales: lang_tag = l.split(';', 1)[0] # drop weight, if any - lang = l.split('-', 1)[0] # en-GB -> en + lang = l.split('-', 1)[0] # en-GB -> en lang_tag_matches_config = ( lang == lang_code or