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

40 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-15 14:59 +0000

1from couchers.i18n.locales import ( 

2 DEFAULT_LOCALE, 

3 get_babel_locale, 

4 get_locale_chain, 

5 get_main_i18next, 

6 get_supported_locales, 

7 is_locale_with_translations, 

8 is_supported_locale, 

9 to_supported_locale, 

10) 

11 

12 

13def test_translations_loaded(): 

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

15 i18next = get_main_i18next() 

16 

17 # Should have at least English errors loaded 

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

19 assert en_translation is not None 

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

21 

22 # Other languages should also exist 

23 assert len(i18next.translations_by_locale) > 1 

24 

25 

26def test_to_supported_locale(): 

27 # No-ops 

28 assert to_supported_locale("en") == "en" 

29 assert to_supported_locale("fr") == "fr" 

30 assert to_supported_locale("fr-CA") == "fr" # We don't have a separate fr-CA translation anymore 

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

32 

33 # Supported locale with no translations 

34 assert is_supported_locale("en-US") and not is_locale_with_translations("en-US") 

35 assert to_supported_locale("en-US") == "en-US" 

36 

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 

41 

42 # Normalization 

43 assert to_supported_locale("PT-br") == "pt-BR" 

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

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

46 assert to_supported_locale("pt-BR-Shorthand") == "pt-BR" 

47 

48 

49def test_all_supported_locales_have_babel_locales(): 

50 for locale in get_supported_locales(): 

51 babel_locale = get_babel_locale(locale) 

52 assert babel_locale, f"Locale {locale} does not have a valid Babel locale" 

53 

54 # Ensure minimal support for the i18n operations we care about 

55 assert babel_locale.date_formats 

56 assert babel_locale.datetime_formats 

57 assert babel_locale.datetime_skeletons 

58 assert babel_locale.list_patterns 

59 assert babel_locale.languages 

60 assert babel_locale.time_zones 

61 assert babel_locale.zone_formats 

62 assert babel_locale.territories 

63 

64 

65def test_get_locale_chain(): 

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

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

68 assert get_locale_chain("en-US") == ["en-US", "en"] 

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

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

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

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