Coverage for app/backend/src/tests/test_localize.py: 100%
60 statements
« prev ^ index » next coverage.py v7.18.0, created at 2026-12-03 18:26 +0000
« prev ^ index » next coverage.py v7.18.0, created at 2026-12-03 18:26 +0000
1from datetime import UTC, date, datetime, time
2from decimal import Decimal
3from zoneinfo import ZoneInfo
5import babel
7from couchers.i18n.localize import (
8 localize_currency,
9 localize_date,
10 localize_datetime,
11 localize_list,
12 localize_time,
13 localize_timezone,
14 try_localize_language_name_from_iso639,
15 try_localize_region_name_from_iso3166,
16)
18babel_en = babel.Locale.parse("en")
19babel_es = babel.Locale.parse("es")
20babel_fr = babel.Locale.parse("fr")
21babel_zh = babel.Locale.parse("zh")
24def test_localize_language_name() -> None:
25 assert try_localize_language_name_from_iso639("en", [babel_en]) == "English"
26 assert try_localize_language_name_from_iso639("es", [babel_en]) == "Spanish"
28 assert try_localize_language_name_from_iso639("en", [babel_es]) == "inglés"
29 assert try_localize_language_name_from_iso639("es", [babel_es]) == "español"
31 assert try_localize_language_name_from_iso639("en", [babel_es], standalone=True) == "Inglés" # Sentence case
32 assert try_localize_language_name_from_iso639("eng", [babel_es]) == "inglés" # ISO639-3 code
33 assert try_localize_language_name_from_iso639("xx", [babel_en]) is None
36def test_localize_region_name() -> None:
37 assert try_localize_region_name_from_iso3166("US", [babel_en]) == "United States"
38 assert try_localize_region_name_from_iso3166("DE", [babel_en]) == "Germany"
40 assert try_localize_region_name_from_iso3166("US", [babel_es]) == "Estados Unidos"
41 assert try_localize_region_name_from_iso3166("DE", [babel_es]) == "Alemania"
43 assert try_localize_region_name_from_iso3166("USA", [babel_en]) == "United States" # alpha3 code
45 assert try_localize_region_name_from_iso3166("xx", [babel_en]) is None
48def test_localize_currency() -> None:
49 assert localize_currency(Decimal("25.50"), "USD", babel_en) == "$25.50"
50 assert localize_currency(Decimal("25.50"), "EUR", babel_en) == "€25.50"
51 assert localize_currency(Decimal("25.50"), "USD", babel_fr) == "25,50\xa0$US"
52 assert localize_currency(Decimal("1234.5"), "EUR", babel_es) == "1.234,50\xa0€"
54 # Whole amounts drop their decimals unless asked to keep them
55 assert localize_currency(25, "USD", babel_en) == "$25"
56 assert localize_currency(Decimal("25.00"), "USD", babel_en) == "$25"
57 assert localize_currency(25, "USD", babel_en, keep_trailing_zeros=True) == "$25.00"
60def test_localize_date() -> None:
61 assert localize_date(date(2000, 1, 2), babel_en) == "January 2, 2000"
62 assert localize_date(date(2000, 1, 2), babel_es, abbrev=True) == "2 ene 2000"
63 assert localize_date(date(2000, 1, 2), babel_fr, with_day_of_week=True) == "dimanche 2 janvier 2000"
64 assert localize_date(date(2000, 1, 2), babel_zh, with_year=False) == "1月2日"
65 assert localize_date(date(2000, 1, 2), babel_en, with_year=False, with_day_of_week=True) == "Sunday, January 2"
68def test_localize_time() -> None:
69 assert localize_time(time(14, 5), babel_en) == "2:05 PM"
70 assert localize_time(time(14, 5), babel_fr) == "14:05"
71 assert localize_time(time(14, 5), babel_es, with_seconds=True) == "14:05:00"
74def test_localize_datetime() -> None:
75 value = datetime(2000, 1, 2, 14, 5, tzinfo=UTC)
76 assert localize_datetime(value, babel_en) == "January 2, 2000, 2:05 PM"
77 assert localize_datetime(value, babel_fr) == "2 janvier 2000, 14:05"
79 value_non_utc = datetime(2000, 1, 2, 14, 5, tzinfo=ZoneInfo("Etc/GMT-2"))
80 assert localize_datetime(value_non_utc, babel_en) == "January 2, 2000, 2:05 PM"
82 assert localize_datetime(value, babel_en, abbrev=True) == "Jan 2, 2000, 2:05 PM"
83 assert localize_datetime(value, babel_en, with_year=False) == "January 2, 2:05 PM"
84 assert localize_datetime(value, babel_en, with_day_of_week=True) == "Sunday, January 2, 2000, 2:05 PM"
85 assert localize_datetime(value, babel_en, with_seconds=True) == "January 2, 2000, 2:05:00 PM"
88def test_localize_timezone() -> None:
89 assert localize_timezone(ZoneInfo("Europe/London"), [babel_en]) == "United Kingdom Time"
90 assert localize_timezone(ZoneInfo("Europe/London"), [babel_es]) == "hora de Reino Unido"
93def test_localize_list() -> None:
94 abc = ["a", "b", "c"]
95 assert localize_list(abc, [babel_en]) == "a, b, and c"
96 assert localize_list(abc, [babel_es]) == "a, b y c"
97 assert localize_list(abc, [babel_zh]) == "a、b和c"