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

30 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-19 14:14 +0000

1import pytest 

2from babel import Locale, UnknownLocaleError 

3 

4from couchers.i18n.localize import get_main_i18next 

5 

6 

7@pytest.fixture(autouse=True) 

8def _(testconfig): 

9 pass 

10 

11 

12def test_translations_loaded(): 

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

14 i18next = get_main_i18next() 

15 

16 # Should have at least English errors loaded 

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

18 assert en_translation is not None 

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

20 

21 # Other languages should also exist 

22 assert len(i18next.translations_by_locale) > 1 

23 

24 

25def test_fallback_chain(): 

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

27 i18next = get_main_i18next() 

28 

29 # Example: fr-CA should fallback to fr, which should fallback to en 

30 fr_CA = i18next.translations_by_locale["fr-CA"] 

31 fr = i18next.translations_by_locale["fr"] 

32 en = i18next.translations_by_locale["en"] 

33 

34 assert fr_CA.fallbacks == [fr, en] 

35 assert fr.fallbacks == [en] 

36 assert en.fallbacks == [] 

37 

38 assert i18next.default_translation == en 

39 

40 

41def test_babel_locales(): 

42 """Documents which locales are not supported/recognized by the Babel library's CLDR.""" 

43 unknown_locales: set[str] = set() 

44 i18next = get_main_i18next() 

45 for locale in i18next.translations_by_locale.keys(): 

46 try: 

47 Locale(locale) 

48 except UnknownLocaleError: 

49 unknown_locales.add(locale) 

50 assert unknown_locales == {"en_CORP", "es-419", "fr-CA", "nb-NO", "pt-BR", "zh-Hans", "zh-Hant"}