mobiler-core 0.49.0

Mobiler runtime: the MobilerApp trait + Crux shell adapter over the fixed UI ABI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
//! Locale-aware number, currency, and date formatting — pure, synchronous, dependency-light.
//!
//! `view()` builds the `Widget` tree synchronously, but device capabilities/plugins are async, so
//! locale formatting can't be delegated to the platform's `NumberFormatter`/`Intl` at render time —
//! it has to run in the core. This module hand-rolls the conventions for the locales Mobiler apps
//! actually target (Swiss de/fr/it, the common Western locales, Ukrainian, Serbian, Polish, Spanish),
//! following the platform's CLDR conventions, instead of pulling in a full ICU
//! data bundle, keeping the wasm web shell small.
//!
//! ```
//! use mobiler_core::format::{format_currency, Currency, Locale};
//! assert_eq!(format_currency(1234.5, Currency::Chf, Locale::DeCh), "CHF 1'234.50");
//! assert_eq!(format_currency(1234.5, Currency::Eur, Locale::DeDe), "1.234,50 €");
//! ```

use serde::{Deserialize, Serialize};

/// A formatting locale — drives digit grouping, the decimal mark, currency placement, and the
/// date order + month names. Map a device language tag (e.g. from a `locale` plugin) to one with
/// [`Locale::from_tag`].
#[derive(Serialize, Deserialize, Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Locale {
    #[default]
    EnUs,
    EnGb,
    /// Swiss German — `1'234.50`, `31.12.2026`.
    DeCh,
    /// Swiss French — `1'234.50`, `31.12.2026`.
    FrCh,
    /// Swiss Italian — `1'234.50`, `31.12.2026`.
    ItCh,
    DeDe,
    FrFr,
    ItIt,
    /// Ukrainian — `1 234,50` (no-break space grouping), `31.12.2026`, `січень`.
    UkUa,
    /// Serbian, Latin script — `1.234,50`, `31.12.2026.`, `januar`.
    SrLatn,
    /// Serbian, Cyrillic script — `1.234,50`, `31.12.2026.`, `јануар`.
    SrCyrl,
    /// Polish — `1234,50` and `12 345,50` (no-break space grouping from five digits, as the platform does),
    /// `31.12.2026`, `styczeń`.
    PlPl,
    /// Spanish (Spain; every `es` tag maps here) — `1234,50` and `12.345,50` (grouping from five digits, as
    /// the platform does), `31/12/2026`, `enero`.
    EsEs,
}

/// A currency. Placement (symbol leading vs trailing) follows the [`Locale`].
#[derive(Serialize, Deserialize, Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Currency {
    Chf,
    #[default]
    Eur,
    Usd,
    Gbp,
    /// Ukrainian hryvnia — trails the amount (`1 234,50 ₴`).
    Uah,
    /// Serbian dinar — trails (`din.` in Latin, `дин.` in Cyrillic, else `RSD`).
    Rsd,
    /// Polish złoty — trails the amount in every locale (`1234,50 zł`).
    Pln,
}

/// A day of the week — the first column of a localized calendar ([`Locale::week_start`]).
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq)]
pub enum Weekday {
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
}

impl Weekday {
    /// 0 = Sunday … 6 = Saturday (the index `weekday_short` and the calendar layout use).
    #[must_use]
    pub const fn sun0(self) -> u8 {
        self as u8
    }
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum Lang {
    En,
    De,
    Fr,
    It,
    Uk,
    SrLatn,
    SrCyrl,
    Pl,
    Es,
}

impl Locale {
    /// `(group separator, decimal separator)`.
    const fn seps(self) -> (&'static str, &'static str) {
        match self {
            Locale::EnUs | Locale::EnGb => (",", "."),
            // Swiss: apostrophe grouping, period decimal (e.g. SSM's "CHF 80'000").
            Locale::DeCh | Locale::FrCh | Locale::ItCh => ("'", "."),
            Locale::DeDe | Locale::ItIt | Locale::SrLatn | Locale::SrCyrl | Locale::EsEs => (".", ","),
            // French: narrow no-break space grouping, comma decimal.
            Locale::FrFr => ("\u{202f}", ","),
            // Ukrainian and Polish: no-break space grouping, comma decimal.
            Locale::UkUa | Locale::PlPl => ("\u{a0}", ","),
        }
    }

    /// CLDR's minimum grouping digits: with 2, an integer part of four digits stays ungrouped
    /// (`1234`) and grouping starts at five (`12 345`), as Android, iOS and browsers print Polish and
    /// Spanish.
    const fn min_grouping(self) -> usize {
        match self {
            Locale::PlPl | Locale::EsEs => 2,
            _ => 1,
        }
    }

    const fn lang(self) -> Lang {
        match self {
            Locale::EnUs | Locale::EnGb => Lang::En,
            Locale::DeCh | Locale::DeDe => Lang::De,
            Locale::FrCh | Locale::FrFr => Lang::Fr,
            Locale::ItCh | Locale::ItIt => Lang::It,
            Locale::UkUa => Lang::Uk,
            Locale::SrLatn => Lang::SrLatn,
            Locale::SrCyrl => Lang::SrCyrl,
            Locale::PlPl => Lang::Pl,
            Locale::EsEs => Lang::Es,
        }
    }

    /// Best-effort map of a BCP-47 language tag (case-insensitive, e.g. `"de-CH"`, `"fr"`,
    /// `"en-US"`) to a supported [`Locale`]. Region wins when known; otherwise the language's
    /// most common locale is used. Returns `None` for unrecognized languages.
    #[must_use]
    pub fn from_tag(tag: &str) -> Option<Locale> {
        let t = tag.to_ascii_lowercase().replace('_', "-");
        let mut parts = t.split('-');
        let lang = parts.next().unwrap_or("");
        let region = parts.next().unwrap_or("");
        Some(match (lang, region) {
            ("en", "gb") => Locale::EnGb,
            ("en", _) => Locale::EnUs,
            ("de", "ch") => Locale::DeCh,
            ("de", _) => Locale::DeDe,
            ("fr", "ch") => Locale::FrCh,
            ("fr", _) => Locale::FrFr,
            ("it", "ch") => Locale::ItCh,
            ("it", _) => Locale::ItIt,
            ("uk", _) => Locale::UkUa,
            // Serbian: script wins; default to Cyrillic (the official script) when unspecified.
            ("sr", "latn") => Locale::SrLatn,
            ("sr", _) => Locale::SrCyrl,
            ("pl", _) => Locale::PlPl,
            // Every Spanish region gets Spain's conventions (the only Spanish locale).
            ("es", _) => Locale::EsEs,
            _ => return None,
        })
    }
}

/// Group an unsigned integer's digit string with `sep` every three digits from the right, unless it
/// is shorter than `3 + min_grouping` digits (CLDR minimum grouping digits).
fn group_digits(digits: &str, sep: &str, min_grouping: usize) -> String {
    let len = digits.len();
    if len < 3 + min_grouping {
        return digits.to_string();
    }
    let mut out = String::with_capacity(len + len / 3 * sep.len());
    for (i, ch) in digits.chars().enumerate() {
        if i > 0 && (len - i).is_multiple_of(3) {
            out.push_str(sep);
        }
        out.push(ch);
    }
    out
}

/// Format a floating-point number with a fixed number of decimal places, grouped per `locale`.
///
/// ```
/// use mobiler_core::format::{format_number, Locale};
/// assert_eq!(format_number(1234567.5, 2, Locale::DeCh), "1'234'567.50");
/// assert_eq!(format_number(1234567.5, 2, Locale::DeDe), "1.234.567,50");
/// ```
#[must_use]
pub fn format_number(value: f64, decimals: usize, locale: Locale) -> String {
    let (group, dec) = locale.seps();
    let s = format!("{:.*}", decimals, value.abs());
    let (int_part, frac_part) = match s.split_once('.') {
        Some((i, f)) => (i, Some(f)),
        None => (s.as_str(), None),
    };
    // Determine sign from the rounded result so -0.00 reads as "0.00".
    let negative = value < 0.0 && s.bytes().any(|b| b != b'0' && b != b'.');
    let mut out = String::new();
    if negative {
        out.push('-');
    }
    out.push_str(&group_digits(int_part, group, locale.min_grouping()));
    if let Some(f) = frac_part {
        out.push_str(dec);
        out.push_str(f);
    }
    out
}

/// Format an integer, grouped per `locale`.
///
/// ```
/// use mobiler_core::format::{format_int, Locale};
/// assert_eq!(format_int(80000, Locale::DeCh), "80'000");
/// assert_eq!(format_int(-1234, Locale::EnUs), "-1,234");
/// ```
#[must_use]
pub fn format_int(value: i64, locale: Locale) -> String {
    let (group, _) = locale.seps();
    let digits = value.unsigned_abs().to_string();
    let mut out = String::new();
    if value < 0 {
        out.push('-');
    }
    out.push_str(&group_digits(&digits, group, locale.min_grouping()));
    out
}

/// Format a monetary amount (always two fraction digits). Symbol placement follows `locale`:
/// CHF/USD/GBP lead; EUR trails in de-DE/fr-FR/it-IT/pl-PL/es-ES and leads elsewhere; UAH, RSD and
/// PLN trail.
///
/// ```
/// use mobiler_core::format::{format_currency, Currency, Locale};
/// assert_eq!(format_currency(1234.5, Currency::Chf, Locale::FrCh), "CHF 1'234.50");
/// assert_eq!(format_currency(1234.5, Currency::Usd, Locale::EnUs), "$1,234.50");
/// ```
#[must_use]
pub fn format_currency(value: f64, currency: Currency, locale: Locale) -> String {
    let num = format_number(value, 2, locale);
    match currency {
        Currency::Chf => format!("CHF {num}"),
        Currency::Usd => format!("${num}"),
        Currency::Gbp => format!("£{num}"),
        Currency::Eur => match locale {
            Locale::DeDe | Locale::FrFr | Locale::ItIt | Locale::PlPl | Locale::EsEs => format!("{num} €"),
            _ => format!("€{num}"),
        },
        Currency::Uah => format!("{num} ₴"),
        Currency::Rsd => match locale {
            Locale::SrLatn => format!("{num} din."),
            Locale::SrCyrl => format!("{num} дин."),
            _ => format!("{num} RSD"),
        },
        Currency::Pln => format!("{num} zł"),
    }
}

const MONTHS_EN: [&str; 12] = [
    "January", "February", "March", "April", "May", "June", "July", "August", "September",
    "October", "November", "December",
];
const MONTHS_DE: [&str; 12] = [
    "Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober",
    "November", "Dezember",
];
const MONTHS_FR: [&str; 12] = [
    "janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre",
    "octobre", "novembre", "décembre",
];
const MONTHS_IT: [&str; 12] = [
    "gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre",
    "ottobre", "novembre", "dicembre",
];
// Ukrainian standalone (nominative) month names.
const MONTHS_UK: [&str; 12] = [
    "січень", "лютий", "березень", "квітень", "травень", "червень", "липень", "серпень",
    "вересень", "жовтень", "листопад", "грудень",
];
const MONTHS_SR_LATN: [&str; 12] = [
    "januar", "februar", "mart", "april", "maj", "jun", "jul", "avgust", "septembar", "oktobar",
    "novembar", "decembar",
];
const MONTHS_SR_CYRL: [&str; 12] = [
    "јануар", "фебруар", "март", "април", "мај", "јун", "јул", "август", "септембар", "октобар",
    "новембар", "децембар",
];

// Polish standalone (nominative) month names.
const MONTHS_PL: [&str; 12] = [
    "styczeń", "luty", "marzec", "kwiecień", "maj", "czerwiec", "lipiec", "sierpień", "wrzesień",
    "październik", "listopad", "grudzień",
];
const MONTHS_ES: [&str; 12] = [
    "enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre",
    "octubre", "noviembre", "diciembre",
];

// Genitive month names for "day month year" dates, where Polish and Ukrainian decline the month
// ("31 grudnia", "5 травня"). Only `format_date_long` uses them.
const MONTHS_PL_GEN: [&str; 12] = [
    "stycznia", "lutego", "marca", "kwietnia", "maja", "czerwca", "lipca", "sierpnia", "września",
    "października", "listopada", "grudnia",
];
const MONTHS_UK_GEN: [&str; 12] = [
    "січня", "лютого", "березня", "квітня", "травня", "червня", "липня", "серпня", "вересня",
    "жовтня", "листопада", "грудня",
];

/// The month as it reads after a day number: genitive in Polish and Ukrainian, else [`month_name`].
fn month_in_date(month: u32, locale: Locale) -> &'static str {
    let idx = (month.clamp(1, 12) - 1) as usize;
    match locale.lang() {
        Lang::Pl => MONTHS_PL_GEN[idx],
        Lang::Uk => MONTHS_UK_GEN[idx],
        _ => month_name(month, locale),
    }
}

/// The localized full month name (`month` is 1–12, clamped).
#[must_use]
pub fn month_name(month: u32, locale: Locale) -> &'static str {
    let idx = (month.clamp(1, 12) - 1) as usize;
    match locale.lang() {
        Lang::En => MONTHS_EN[idx],
        Lang::De => MONTHS_DE[idx],
        Lang::Fr => MONTHS_FR[idx],
        Lang::It => MONTHS_IT[idx],
        Lang::Uk => MONTHS_UK[idx],
        Lang::SrLatn => MONTHS_SR_LATN[idx],
        Lang::SrCyrl => MONTHS_SR_CYRL[idx],
        Lang::Pl => MONTHS_PL[idx],
        Lang::Es => MONTHS_ES[idx],
    }
}

impl Locale {
    /// The first day of the week: Sunday for US English, Monday for every other supported locale.
    #[must_use]
    pub const fn week_start(self) -> Weekday {
        match self {
            Locale::EnUs => Weekday::Sunday,
            _ => Weekday::Monday,
        }
    }
}

// Narrow weekday labels for a calendar header, Sunday-first (index = `Weekday::sun0`).
const WEEKDAYS_EN: [&str; 7] = ["S", "M", "T", "W", "T", "F", "S"];
const WEEKDAYS_DE: [&str; 7] = ["S", "M", "D", "M", "D", "F", "S"];
const WEEKDAYS_FR: [&str; 7] = ["D", "L", "M", "M", "J", "V", "S"];
const WEEKDAYS_IT: [&str; 7] = ["D", "L", "M", "M", "G", "V", "S"];
// Ukrainian calendars use the two-letter forms (single letters are ambiguous).
const WEEKDAYS_UK: [&str; 7] = ["Нд", "Пн", "Вт", "Ср", "Чт", "Пт", "Сб"];
const WEEKDAYS_SR_LATN: [&str; 7] = ["N", "P", "U", "S", "Č", "P", "S"];
const WEEKDAYS_SR_CYRL: [&str; 7] = ["Н", "П", "У", "С", "Ч", "П", "С"];
const WEEKDAYS_PL: [&str; 7] = ["N", "P", "W", "Ś", "C", "P", "S"];
// Spanish uses X for miércoles so it differs from martes.
const WEEKDAYS_ES: [&str; 7] = ["D", "L", "M", "X", "J", "V", "S"];

/// The localized narrow weekday label for a calendar header. `sun0` is 0 = Sunday … 6 = Saturday
/// and wraps, so `weekday_short(start + i, …)` walks a week from any start day.
#[must_use]
pub fn weekday_short(sun0: u8, locale: Locale) -> &'static str {
    let idx = usize::from(sun0 % 7);
    match locale.lang() {
        Lang::En => WEEKDAYS_EN[idx],
        Lang::De => WEEKDAYS_DE[idx],
        Lang::Fr => WEEKDAYS_FR[idx],
        Lang::It => WEEKDAYS_IT[idx],
        Lang::Uk => WEEKDAYS_UK[idx],
        Lang::SrLatn => WEEKDAYS_SR_LATN[idx],
        Lang::SrCyrl => WEEKDAYS_SR_CYRL[idx],
        Lang::Pl => WEEKDAYS_PL[idx],
        Lang::Es => WEEKDAYS_ES[idx],
    }
}

/// A calendar title: the localized month name, capitalized, then the year (`"Septembar 2026"`;
/// Spanish puts "de" between them, as the platform does: `"Diciembre de 2026"`).
#[must_use]
pub fn month_year(year: u32, month: u32, locale: Locale) -> String {
    let name = month_name(month, locale);
    let mut chars = name.chars();
    let capitalized: String = chars.next().map(|c| c.to_uppercase().chain(chars).collect()).unwrap_or_default();
    match locale.lang() {
        Lang::Es => format!("{capitalized} de {year}"),
        _ => format!("{capitalized} {year}"),
    }
}

/// Numeric date in the locale's conventional order/separator.
///
/// ```
/// use mobiler_core::format::{format_date, Locale};
/// assert_eq!(format_date(2026, 1, 5, Locale::DeCh), "05.01.2026");
/// assert_eq!(format_date(2026, 1, 5, Locale::EnUs), "01/05/2026");
/// ```
#[must_use]
pub fn format_date(year: i32, month: u32, day: u32, locale: Locale) -> String {
    match locale {
        Locale::EnUs => format!("{month:02}/{day:02}/{year}"),
        Locale::EnGb | Locale::FrFr | Locale::ItIt | Locale::EsEs => format!("{day:02}/{month:02}/{year}"),
        Locale::DeCh | Locale::FrCh | Locale::ItCh | Locale::DeDe | Locale::UkUa | Locale::PlPl => {
            format!("{day:02}.{month:02}.{year}")
        }
        // Serbian uses a trailing dot: "31.12.2026."
        Locale::SrLatn | Locale::SrCyrl => format!("{day:02}.{month:02}.{year}."),
    }
}

/// Long date with the localized month name (e.g. `"5. Januar 2026"`, `"January 5, 2026"`). Polish and
/// Ukrainian use the genitive month (`"31 grudnia 2026"`, `"5 травня 2026 р."`), as the platform does.
///
/// ```
/// use mobiler_core::format::{format_date_long, Locale};
/// assert_eq!(format_date_long(2026, 1, 5, Locale::DeCh), "5. Januar 2026");
/// assert_eq!(format_date_long(2026, 1, 5, Locale::EnUs), "January 5, 2026");
/// ```
#[must_use]
pub fn format_date_long(year: i32, month: u32, day: u32, locale: Locale) -> String {
    let m = month_in_date(month, locale);
    match locale.lang() {
        Lang::En => match locale {
            Locale::EnUs => format!("{m} {day}, {year}"),
            _ => format!("{day} {m} {year}"),
        },
        // German + Serbian use the ordinal dot after the day; French/Italian/Polish/Ukrainian do not.
        Lang::De | Lang::SrLatn | Lang::SrCyrl => format!("{day}. {m} {year}"),
        Lang::Fr | Lang::It | Lang::Pl => format!("{day} {m} {year}"),
        // Ukrainian closes the year with "р." (рік), as the platform does.
        Lang::Uk => format!("{day} {m} {year} р."),
        Lang::Es => format!("{day} de {m} de {year}"),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn swiss_grouping_uses_apostrophe() {
        assert_eq!(format_int(80000, Locale::DeCh), "80'000");
        assert_eq!(format_int(1234567, Locale::FrCh), "1'234'567");
        assert_eq!(format_number(1234567.5, 2, Locale::ItCh), "1'234'567.50");
    }

    #[test]
    fn western_number_conventions() {
        assert_eq!(format_number(1234567.5, 2, Locale::EnUs), "1,234,567.50");
        assert_eq!(format_number(1234567.5, 2, Locale::DeDe), "1.234.567,50");
        assert_eq!(format_number(1234.5, 2, Locale::FrFr), "1\u{202f}234,50");
        assert_eq!(format_number(12.0, 0, Locale::EnUs), "12");
        assert_eq!(format_number(999.999, 2, Locale::EnUs), "1,000.00"); // rounds up + regroups
    }

    #[test]
    fn negatives_and_zero() {
        assert_eq!(format_int(-1234, Locale::EnUs), "-1,234");
        assert_eq!(format_number(-0.001, 2, Locale::EnUs), "0.00"); // rounds to zero → no sign
        assert_eq!(format_number(-12.5, 1, Locale::DeCh), "-12.5");
    }

    #[test]
    fn currency_placement() {
        assert_eq!(format_currency(1234.5, Currency::Chf, Locale::DeCh), "CHF 1'234.50");
        assert_eq!(format_currency(1234.5, Currency::Usd, Locale::EnUs), "$1,234.50");
        assert_eq!(format_currency(1234.5, Currency::Gbp, Locale::EnGb), "£1,234.50");
        assert_eq!(format_currency(1234.5, Currency::Eur, Locale::DeDe), "1.234,50 €");
        assert_eq!(format_currency(1234.5, Currency::Eur, Locale::EnUs), "€1,234.50");
    }

    #[test]
    fn dates() {
        assert_eq!(format_date(2026, 1, 5, Locale::DeCh), "05.01.2026");
        assert_eq!(format_date(2026, 1, 5, Locale::EnUs), "01/05/2026");
        assert_eq!(format_date(2026, 12, 31, Locale::ItIt), "31/12/2026");
        assert_eq!(format_date_long(2026, 1, 5, Locale::DeCh), "5. Januar 2026");
        assert_eq!(format_date_long(2026, 3, 5, Locale::FrCh), "5 mars 2026");
        assert_eq!(format_date_long(2026, 1, 5, Locale::EnUs), "January 5, 2026");
    }

    #[test]
    fn serbian() {
        // Latin + Cyrillic share number/date conventions (".", ",", trailing-dot date).
        assert_eq!(format_int(1234567, Locale::SrLatn), "1.234.567");
        assert_eq!(format_number(1234.5, 2, Locale::SrCyrl), "1.234,50");
        assert_eq!(format_date(2026, 12, 31, Locale::SrLatn), "31.12.2026.");
        assert_eq!(format_currency(1234.5, Currency::Rsd, Locale::SrLatn), "1.234,50 din.");
        assert_eq!(format_currency(1234.5, Currency::Rsd, Locale::SrCyrl), "1.234,50 дин.");
        assert_eq!(format_currency(1234.5, Currency::Rsd, Locale::EnUs), "1,234.50 RSD");
        // Month names differ by script.
        assert_eq!(format_date_long(2026, 1, 5, Locale::SrLatn), "5. januar 2026");
        assert_eq!(format_date_long(2026, 1, 5, Locale::SrCyrl), "5. јануар 2026");
    }

    #[test]
    fn ukrainian() {
        // No-break space grouping, comma decimal, trailing ₴.
        assert_eq!(format_int(1234567, Locale::UkUa), "1\u{a0}234\u{a0}567");
        assert_eq!(format_number(1234.5, 2, Locale::UkUa), "1\u{a0}234,50");
        assert_eq!(format_currency(1234.5, Currency::Uah, Locale::UkUa), "1\u{a0}234,50 ₴");
        // dd.MM.yyyy (no trailing dot), nominative month name, day-month-year long form.
        assert_eq!(format_date(2026, 12, 31, Locale::UkUa), "31.12.2026");
        assert_eq!(month_name(1, Locale::UkUa), "січень");
        assert_eq!(format_date_long(2026, 5, 5, Locale::UkUa), "5 травня 2026 р.");
        assert_eq!(format_date_long(2026, 11, 30, Locale::UkUa), "30 листопада 2026 р.");
        assert_eq!(month_name(5, Locale::UkUa), "травень");
        assert_eq!(month_year(2026, 5, Locale::UkUa), "Травень 2026");
    }

    #[test]
    fn polish() {
        assert_eq!(format_int(1234, Locale::PlPl), "1234");
        assert_eq!(format_int(12345, Locale::PlPl), "12\u{a0}345");
        assert_eq!(format_int(-1234, Locale::PlPl), "-1234");
        assert_eq!(format_int(-12345, Locale::PlPl), "-12\u{a0}345");
        assert_eq!(format_number(1234.5, 2, Locale::PlPl), "1234,50");
        assert_eq!(format_number(-1234.5, 2, Locale::PlPl), "-1234,50");
        assert_eq!(format_number(9999.999, 2, Locale::PlPl), "10\u{a0}000,00");
        assert_eq!(format_number(1234567.5, 2, Locale::PlPl), "1\u{a0}234\u{a0}567,50");
        assert_eq!(format_currency(1234.5, Currency::Eur, Locale::PlPl), "1234,50 €");
        assert_eq!(format_date(2026, 12, 31, Locale::PlPl), "31.12.2026");
        // Long dates take the genitive ("31 grudnia"); the month name alone stays nominative.
        assert_eq!(format_date_long(2026, 12, 31, Locale::PlPl), "31 grudnia 2026");
        assert_eq!(format_date_long(2026, 9, 1, Locale::PlPl), "1 września 2026");
        assert_eq!(month_name(12, Locale::PlPl), "grudzień");
        assert_eq!(month_name(10, Locale::PlPl), "październik");
        assert_eq!(month_year(2026, 12, Locale::PlPl), "Grudzień 2026");
        let pl: Vec<&str> = (0..7).map(|i| weekday_short(i, Locale::PlPl)).collect();
        assert_eq!(pl, ["N", "P", "W", "Ś", "C", "P", "S"]);
        assert_eq!(Locale::PlPl.week_start(), Weekday::Monday);
    }

    #[test]
    fn polish_zloty() {
        assert_eq!(format_currency(1234.5, Currency::Pln, Locale::PlPl), "1234,50 zł");
        assert_eq!(format_currency(12345.5, Currency::Pln, Locale::PlPl), "12\u{a0}345,50 zł");
        assert_eq!(format_currency(1234.5, Currency::Pln, Locale::EnUs), "1,234.50 zł");
    }

    #[test]
    fn spanish() {
        assert_eq!(format_int(1234, Locale::EsEs), "1234");
        assert_eq!(format_int(12345, Locale::EsEs), "12.345");
        assert_eq!(format_number(1234.5, 2, Locale::EsEs), "1234,50");
        assert_eq!(format_number(12345.5, 2, Locale::EsEs), "12.345,50");
        assert_eq!(format_currency(1234.5, Currency::Eur, Locale::EsEs), "1234,50 €");
        assert_eq!(format_date(2026, 1, 5, Locale::EsEs), "05/01/2026");
        assert_eq!(format_date_long(2026, 1, 5, Locale::EsEs), "5 de enero de 2026");
        assert_eq!(month_name(1, Locale::EsEs), "enero");
        assert_eq!(month_year(2026, 12, Locale::EsEs), "Diciembre de 2026");
        let es: Vec<&str> = (0..7).map(|i| weekday_short(i, Locale::EsEs)).collect();
        assert_eq!(es, ["D", "L", "M", "X", "J", "V", "S"]);
        assert_eq!(Locale::EsEs.week_start(), Weekday::Monday);
    }

    #[test]
    fn existing_locales_still_group_four_digits() {
        assert_eq!(format_int(1234, Locale::DeDe), "1.234");
        assert_eq!(format_int(1234, Locale::UkUa), "1\u{a0}234");
        assert_eq!(format_int(1234, Locale::EnUs), "1,234");
        assert_eq!(format_number(1234.5, 2, Locale::FrFr), "1\u{202f}234,50");
    }

    #[test]
    fn defaults() {
        assert_eq!(Locale::default(), Locale::EnUs);
        assert_eq!(Currency::default(), Currency::Eur);
    }

    #[test]
    fn tag_parsing() {
        assert_eq!(Locale::from_tag("de-CH"), Some(Locale::DeCh));
        assert_eq!(Locale::from_tag("uk"), Some(Locale::UkUa));
        assert_eq!(Locale::from_tag("uk-UA"), Some(Locale::UkUa));
        assert_eq!(Locale::from_tag("fr_FR"), Some(Locale::FrFr));
        assert_eq!(Locale::from_tag("EN-us"), Some(Locale::EnUs));
        assert_eq!(Locale::from_tag("it"), Some(Locale::ItIt));
        assert_eq!(Locale::from_tag("sr-Latn-RS"), Some(Locale::SrLatn));
        assert_eq!(Locale::from_tag("sr"), Some(Locale::SrCyrl));
        assert_eq!(Locale::from_tag("sr-RS"), Some(Locale::SrCyrl));
        assert_eq!(Locale::from_tag("pl"), Some(Locale::PlPl));
        assert_eq!(Locale::from_tag("pl_PL"), Some(Locale::PlPl));
        assert_eq!(Locale::from_tag("es"), Some(Locale::EsEs));
        assert_eq!(Locale::from_tag("ES"), Some(Locale::EsEs));
        assert_eq!(Locale::from_tag("es-MX"), Some(Locale::EsEs));
        assert_eq!(Locale::from_tag("es-419"), Some(Locale::EsEs));
        assert_eq!(Locale::from_tag("ja-JP"), None);
    }

    #[test]
    fn week_start_and_weekday_labels() {
        assert_eq!(Locale::EnUs.week_start(), Weekday::Sunday);
        assert_eq!(Locale::EnGb.week_start(), Weekday::Monday);
        assert_eq!(Locale::SrLatn.week_start(), Weekday::Monday);
        assert_eq!(Weekday::Tuesday.sun0(), 2);
        let sr: Vec<&str> = (1..8).map(|i| weekday_short(i, Locale::SrLatn)).collect();
        assert_eq!(sr, ["P", "U", "S", "Č", "P", "S", "N"], "Monday-first Serbian, wraps past Saturday");
        let en: Vec<&str> = (0..7).map(|i| weekday_short(i, Locale::EnUs)).collect();
        assert_eq!(en, ["S", "M", "T", "W", "T", "F", "S"]);
        assert_eq!(weekday_short(1, Locale::SrCyrl), "П");
        assert_eq!(weekday_short(1, Locale::UkUa), "Пн");
        assert_eq!(weekday_short(4, Locale::ItIt), "G");
    }

    #[test]
    fn month_year_capitalizes() {
        assert_eq!(month_year(2026, 6, Locale::EnUs), "June 2026");
        assert_eq!(month_year(2026, 9, Locale::SrLatn), "Septembar 2026");
        assert_eq!(month_year(2026, 9, Locale::SrCyrl), "Септембар 2026");
        assert_eq!(month_year(2026, 6, Locale::UkUa), "Червень 2026");
    }
}