Coverage for app/backend/src/tests/test_i18n_locales.py: 100%
44 statements
« prev ^ index » next coverage.py v7.16.0, created at 2026-08-30 21:18 +0000
« prev ^ index » next coverage.py v7.16.0, created at 2026-08-30 21:18 +0000
1import pytest
3from couchers.i18n.locales import (
4 DEFAULT_LOCALE,
5 get_babel_locale,
6 get_locale_chain,
7 get_main_i18next,
8 get_supported_locales,
9 is_locale_with_translations,
10 is_supported_locale,
11 to_supported_locale,
12)
15@pytest.fixture(autouse=True)
16def _(testconfig):
17 pass
20def test_translations_loaded():
21 """Test that translations are loaded from JSON files"""
22 i18next = get_main_i18next()
24 # Should have at least English errors loaded
25 en_translation = i18next.translations_by_locale.get("en")
26 assert en_translation is not None
27 assert en_translation.strings_by_key.get("errors.account_not_found") is not None
29 # Other languages should also exist
30 assert len(i18next.translations_by_locale) > 1
33def test_to_supported_locale():
34 # No-ops
35 assert to_supported_locale("en") == "en"
36 assert to_supported_locale("fr") == "fr"
37 assert to_supported_locale("fr-CA") == "fr" # We don't have a separate fr-CA translation anymore
38 assert to_supported_locale("zh-Hans") == "zh-Hans"
40 # Supported locale with no translations
41 assert is_supported_locale("en-US") and not is_locale_with_translations("en-US")
42 assert to_supported_locale("en-US") == "en-US"
44 # Bogus locales
45 assert to_supported_locale("") == DEFAULT_LOCALE
46 assert to_supported_locale("xx") == DEFAULT_LOCALE
47 assert to_supported_locale("------------------") == DEFAULT_LOCALE
49 # Normalization
50 assert to_supported_locale("PT-br") == "pt-BR"
51 assert to_supported_locale("en-UK") == "en"
52 assert to_supported_locale("en-Shorthand") == "en"
53 assert to_supported_locale("pt-BR-Shorthand") == "pt-BR"
56def test_all_supported_locales_have_babel_locales():
57 for locale in get_supported_locales():
58 babel_locale = get_babel_locale(locale)
59 assert babel_locale, f"Locale {locale} does not have a valid Babel locale"
61 # Ensure minimal support for the i18n operations we care about
62 assert babel_locale.date_formats
63 assert babel_locale.datetime_formats
64 assert babel_locale.datetime_skeletons
65 assert babel_locale.list_patterns
66 assert babel_locale.languages
67 assert babel_locale.time_zones
68 assert babel_locale.zone_formats
69 assert babel_locale.territories
72def test_get_locale_chain():
73 """Test that fallbacks are correctly set up"""
74 assert get_locale_chain("en") == ["en"]
75 assert get_locale_chain("en-US") == ["en-US", "en"]
76 assert get_locale_chain("pl") == ["pl", "en"]
77 assert get_locale_chain("xx") == ["xx", "en"]
78 assert get_locale_chain("pt") == ["pt", "pt-BR", "en"]
79 assert get_locale_chain("pt-BR") == ["pt-BR", "pt", "en"]