Coverage for app/backend/src/tests/test_i18n_locales.py: 100%

33 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-07-16 17:03 +0000

1import pytest 

2 

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 to_supported_locale, 

10) 

11 

12 

13@pytest.fixture(autouse=True) 

14def _(testconfig): 

15 pass 

16 

17 

18def test_translations_loaded(): 

19 """Test that translations are loaded from JSON files""" 

20 i18next = get_main_i18next() 

21 

22 # Should have at least English errors loaded 

23 en_translation = i18next.translations_by_locale.get("en") 

24 assert en_translation is not None 

25 assert en_translation.strings_by_key.get("errors.account_not_found") is not None 

26 

27 # Other languages should also exist 

28 assert len(i18next.translations_by_locale) > 1 

29 

30 

31def test_to_supported_locale(): 

32 # No-ops 

33 assert to_supported_locale("en") == "en" 

34 assert to_supported_locale("fr") == "fr" 

35 assert to_supported_locale("fr-CA") == "fr-CA" 

36 assert to_supported_locale("zh-Hans") == "zh-Hans" 

37 

38 # Bogus locales 

39 assert to_supported_locale("") == DEFAULT_LOCALE 

40 assert to_supported_locale("xx") == DEFAULT_LOCALE 

41 assert to_supported_locale("------------------") == DEFAULT_LOCALE 

42 

43 # Normalization 

44 assert to_supported_locale("FR-ca") == "fr-CA" 

45 assert to_supported_locale("en-UK") == "en" 

46 assert to_supported_locale("en-Shorthand") == "en" 

47 assert to_supported_locale("fr-CA-Shorthand") == "fr-CA" 

48 

49 

50def test_all_supported_locales_have_babel_locales(): 

51 for locale in get_supported_locales(): 

52 assert get_babel_locale(locale), f"Locale {locale} does not have a valid Babel locale" 

53 

54 

55def test_get_locale_chain(): 

56 """Test that fallbacks are correctly set up""" 

57 assert get_locale_chain("en") == ["en"] 

58 assert get_locale_chain("pl") == ["pl", "en"] 

59 assert get_locale_chain("xx") == ["xx", "en"] 

60 assert get_locale_chain("fr-CA") == ["fr-CA", "fr", "en"] 

61 assert get_locale_chain("pt") == ["pt", "pt-BR", "en"] 

62 assert get_locale_chain("pt-BR") == ["pt-BR", "pt", "en"]