Coverage for app/backend/src/tests/test_i18n_locales.py: 100%
35 statements
« prev ^ index » next coverage.py v7.14.1, created at 2026-05-31 14:08 +0000
« prev ^ index » next coverage.py v7.14.1, created at 2026-05-31 14:08 +0000
1import pytest
3from couchers.i18n.locales import (
4 DEFAULT_LOCALE,
5 get_babel_locale,
6 get_main_i18next,
7 get_supported_locales,
8 to_supported_locale,
9)
12@pytest.fixture(autouse=True)
13def _(testconfig):
14 pass
17def test_translations_loaded():
18 """Test that translations are loaded from JSON files"""
19 i18next = get_main_i18next()
21 # Should have at least English errors loaded
22 en_translation = i18next.translations_by_locale.get("en")
23 assert en_translation is not None
24 assert en_translation.strings_by_key.get("errors.account_not_found") is not None
26 # Other languages should also exist
27 assert len(i18next.translations_by_locale) > 1
30def test_to_supported_locale():
31 # No-ops
32 assert to_supported_locale("en") == "en"
33 assert to_supported_locale("fr") == "fr"
34 assert to_supported_locale("fr-CA") == "fr-CA"
35 assert to_supported_locale("zh-Hans") == "zh-Hans"
37 # Bogus locales
38 assert to_supported_locale("") == DEFAULT_LOCALE
39 assert to_supported_locale("xx") == DEFAULT_LOCALE
40 assert to_supported_locale("------------------") == DEFAULT_LOCALE
42 # Normalization
43 assert to_supported_locale("FR-ca") == "fr-CA"
44 assert to_supported_locale("en-UK") == "en"
45 assert to_supported_locale("en-Shorthand") == "en"
46 assert to_supported_locale("fr-CA-Shorthand") == "fr-CA"
49def test_all_supported_locales_have_babel_locales():
50 for locale in get_supported_locales():
51 assert get_babel_locale(locale), f"Locale {locale} does not have a valid Babel locale"
54def test_fallback_chain():
55 """Test that fallbacks are correctly set up"""
56 i18next = get_main_i18next()
58 # Example: fr-CA should fallback to fr, which should fallback to en
59 fr_CA = i18next.translations_by_locale["fr-CA"]
60 fr = i18next.translations_by_locale["fr"]
61 en = i18next.translations_by_locale["en"]
63 assert fr_CA.fallbacks == [fr, en]
64 assert fr.fallbacks == [en]
65 assert en.fallbacks == []
67 assert i18next.default_translation == en