phrasebook 0.1.0

Fluent localization for Rust web frontends: message catalogs per language, arguments and plurals, a remembered language choice in the browser.
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};

use tracing::subscriber::with_default;
use tracing::{Event, Level, Metadata, Subscriber, span};

use crate::{Args, Catalog, Lang, Phrasebook, Phrases, Source, Value, args, check};

const SV_SE: Lang = Lang::new("sv-SE");
const DE_DE: Lang = Lang::new("de-DE");

static APP: Catalog = Catalog::new(
    "app",
    &[
        Source::new(
            Lang::EN_US,
            r#"
hello = Hello
greeting = Hello, { $name }!
only-english = Only in English
items = { $count ->
    [0] No items
    [one] One item
   *[other] { $count } items
}
nested = { $kind ->
    [a] { $count ->
        [one] One A
       *[other] Many A
    }
   *[other] Other
}
code = Code { $code }
mixed = { $pts } of { $limit } points
lib-sign-in = Log in
"#,
        ),
        Source::new(
            SV_SE,
            r#"
hello = Hej
greeting = Hej, { $name }!
items = { $count ->
    [0] Inga saker
    [one] En sak
   *[other] { $count } saker
}
"#,
        ),
        // Not declared by the phrasebooks below, so ignored.
        Source::new(DE_DE, "hello = Hallo\n"),
    ],
);

static LIB: Catalog = Catalog::new(
    "lib",
    &[
        Source::new(
            Lang::EN_US,
            "lib-sign-in = Sign in\nlib-delete = Delete { $name }\n",
        ),
        Source::new(SV_SE, "lib-sign-in = Logga in\n"),
    ],
)
.with_prefix("lib-");

fn book() -> Phrasebook {
    Phrasebook::builder()
        .language(Lang::EN_US, "English")
        .language(SV_SE, "Svenska")
        .catalog(&APP)
        .catalog(&LIB)
        .build()
}

/// Counts the warnings logged while it is the default subscriber.
#[derive(Clone, Default)]
struct Warnings(Arc<AtomicUsize>);

impl Warnings {
    fn count(&self) -> usize {
        self.0.load(Ordering::SeqCst)
    }
}

impl Subscriber for Warnings {
    fn enabled(&self, _: &Metadata<'_>) -> bool {
        true
    }

    fn new_span(&self, _: &span::Attributes<'_>) -> span::Id {
        span::Id::from_u64(1)
    }

    fn record(&self, _: &span::Id, _: &span::Record<'_>) {}

    fn record_follows_from(&self, _: &span::Id, _: &span::Id) {}

    fn event(&self, event: &Event<'_>) {
        if *event.metadata().level() == Level::WARN {
            self.0.fetch_add(1, Ordering::SeqCst);
        }
    }

    fn enter(&self, _: &span::Id) {}

    fn exit(&self, _: &span::Id) {}
}

#[test]
fn lookup() {
    let book = book();
    let en = book.phrases(Lang::EN_US);
    let sv = book.phrases(SV_SE);

    assert_eq!(&*en.text("hello"), "Hello");
    assert_eq!(&*sv.text("hello"), "Hej");
    assert_eq!(en.msg("greeting", &args!(name = "Ada")), "Hello, Ada!");
    assert_eq!(sv.msg("greeting", &args!(name = "Ada")), "Hej, Ada!");
    assert_eq!(en.try_text("hello").as_deref(), Some("Hello"));
}

#[test]
fn text_is_cached() {
    let book = book();
    let en = book.phrases(Lang::EN_US);
    let a = en.text("hello");
    let b = en.text("hello");
    assert!(std::rc::Rc::ptr_eq(&a, &b));
}

#[test]
fn no_isolation_by_default() {
    let book = book();
    let en = book.phrases(Lang::EN_US);
    assert_eq!(en.msg("greeting", &args!(name = "Ada")), "Hello, Ada!");

    let isolating = Phrasebook::builder()
        .language(Lang::EN_US, "English")
        .catalog(&APP)
        .use_isolating(true)
        .build();

    assert_eq!(
        isolating
            .phrases(Lang::EN_US)
            .msg("greeting", &args!(name = "Ada")),
        "Hello, \u{2068}Ada\u{2069}!"
    );
}

#[test]
fn arguments() {
    let book = book();
    let en = book.phrases(Lang::EN_US);
    let name = String::from("Ada");

    assert_eq!(en.msg("greeting", &args!(name = &name)), "Hello, Ada!");
    assert_eq!(
        en.msg("greeting", &args!(name = name.clone())),
        "Hello, Ada!"
    );
    assert_eq!(
        en.msg("greeting", &Args::new().with("name", "Ada")),
        "Hello, Ada!"
    );
    assert_eq!(en.msg("greeting", &args([("name", "Ada")])), "Hello, Ada!");

    let mut a = Args::new();
    a.set("name", std::rc::Rc::<str>::from("Ada"));
    assert_eq!(en.msg("greeting", &a), "Hello, Ada!");

    assert_eq!(
        en.msg("mixed", &args!(pts = 1.5, limit = 10u32)),
        "1.5 of 10 points"
    );
    assert_eq!(
        en.msg("mixed", &args!("pts" = 2, "limit" = "10",)),
        "2 of 10 points"
    );

    let collected: Args<'_> = [("name", Value::from("Ada"))].into_iter().collect();
    assert_eq!(en.msg("greeting", &collected), "Hello, Ada!");
}

#[test]
fn plurals_with_numbers_and_digit_strings() {
    let book = book();
    let en = book.phrases(Lang::EN_US);
    let sv = book.phrases(SV_SE);

    for (count, expected) in [(0, "No items"), (1, "One item"), (2, "2 items")] {
        assert_eq!(en.msg("items", &args!(count = count)), expected);
        let digits = count.to_string();
        assert_eq!(en.msg("items", &args!(count = &digits)), expected);
    }

    assert_eq!(sv.msg("items", &args!(count = "1")), "En sak");
    assert_eq!(sv.msg("items", &args!(count = 5)), "5 saker");

    // At any depth.
    assert_eq!(en.msg("nested", &args!(kind = "a", count = "1")), "One A");
    assert_eq!(en.msg("nested", &args!(kind = "a", count = 7)), "Many A");

    // Not a number: the default variant.
    assert_eq!(en.msg("items", &args!(count = "many")), "many items");
}

#[test]
fn unselected_digits_stay_as_written() {
    let book = book();
    let en = book.phrases(Lang::EN_US);
    assert_eq!(en.msg("code", &args!(code = "007")), "Code 007");
    // A selected variable is formatted as the number it spells.
    assert_eq!(en.msg("items", &args!(count = "02")), "2 items");
}

#[test]
fn fallback_language() {
    let book = book();
    let sv = book.phrases(SV_SE);

    assert_eq!(&*sv.text("only-english"), "Only in English");
    assert_eq!(
        sv.try_text("only-english").as_deref(),
        Some("Only in English")
    );

    // An explicit fallback.
    let book = Phrasebook::builder()
        .language(Lang::EN_US, "English")
        .language(SV_SE, "Svenska")
        .fallback(SV_SE)
        .catalog(&APP)
        .build();

    assert_eq!(book.fallback(), SV_SE);
    assert_eq!(book.phrases(DE_DE).lang(), SV_SE);
    assert_eq!(&*book.phrases(Lang::EN_US).text("hello"), "Hello");
}

#[test]
fn undeclared_language_uses_the_fallback() {
    let book = book();
    let de = book.phrases(DE_DE);
    assert_eq!(de.lang(), Lang::EN_US);
    assert_eq!(&*de.text("hello"), "Hello");
    assert_eq!(book.phrases(SV_SE).with_lang(DE_DE).lang(), Lang::EN_US);
    assert!(!book.supports(DE_DE));
}

#[test]
fn missing_ids() {
    let book = book();
    let sv = book.phrases(SV_SE);

    assert_eq!(&*sv.text("no-such-id"), "no-such-id");
    assert_eq!(sv.msg("no-such-id", &args!(count = 1)), "no-such-id");
    assert_eq!(sv.try_text("no-such-id"), None);
    assert_eq!(sv.try_msg("no-such-id", &Args::new()), None);
}

#[test]
fn misses_are_logged_once() {
    let warnings = Warnings::default();

    with_default(warnings.clone(), || {
        let book = book();
        let sv = book.phrases(SV_SE);

        for _ in 0..3 {
            sv.text("no-such-id");
            sv.msg("no-such-id", &Args::new());
        }

        assert_eq!(warnings.count(), 1);

        for _ in 0..3 {
            sv.msg("only-english", &Args::new());
            sv.text("only-english");
        }

        assert_eq!(warnings.count(), 2);

        // Another language is logged on its own.
        book.phrases(Lang::EN_US).text("no-such-id");
        assert_eq!(warnings.count(), 3);

        // A missing argument: a format error, logged once per id.
        let en = book.phrases(Lang::EN_US);

        for _ in 0..3 {
            assert_eq!(en.msg("greeting", &Args::new()), "Hello, {$name}!");
        }

        assert_eq!(warnings.count(), 4);
    });
}

#[test]
fn library_catalog_fallback() {
    static APP_ONLY: Catalog = Catalog::new("app", &[Source::new(Lang::EN_US, "hello = Hello\n")]);

    let warnings = Warnings::default();

    with_default(warnings.clone(), || {
        // The app did not add the library's catalog.
        let book = Phrasebook::builder()
            .language(Lang::EN_US, "English")
            .language(SV_SE, "Svenska")
            .catalog(&APP_ONLY)
            .build();

        let en = book.phrases(Lang::EN_US).with_backstop(&LIB);
        assert_eq!(&*en.text("lib-sign-in"), "Sign in");
        assert_eq!(en.msg("lib-delete", &args!(name = "x")), "Delete x");
        assert_eq!(&*en.text("hello"), "Hello");

        // The library's own Swedish, and its default language after that.
        let sv = en.with_lang(SV_SE);
        assert_eq!(&*sv.text("lib-sign-in"), "Logga in");
        assert_eq!(sv.msg("lib-delete", &args!(name = "x")), "Delete x");

        // Found in the backstop: not a warning.
        assert_eq!(warnings.count(), 0);

        // Missing from the backstop too: the id.
        assert_eq!(&*sv.text("lib-nope"), "lib-nope");
        assert_eq!(sv.try_text("lib-nope"), None);
        assert_eq!(warnings.count(), 1);
    });

    // Without any phrasebook: the catalog alone.
    let lib = Phrases::from_catalog(&LIB);
    assert_eq!(lib.lang(), Lang::EN_US);
    assert_eq!(&*lib.text("lib-sign-in"), "Sign in");
    assert_eq!(&*lib.with_lang(SV_SE).text("lib-sign-in"), "Logga in");
    assert!(lib == Phrases::from_catalog(&LIB));

    // An app that added the catalog overrides it.
    let book = book();
    let en = book.phrases(Lang::EN_US).with_backstop(&LIB);
    assert_eq!(&*en.text("lib-sign-in"), "Log in");
    assert_eq!(en.msg("lib-delete", &args!(name = "x")), "Delete x");
}

#[test]
fn empty() {
    let empty = Phrases::empty();
    assert_eq!(&*empty.text("hello"), "hello");
    assert_eq!(empty.msg("hello", &args!(n = 1)), "hello");
    assert!(empty.languages().is_empty());
    assert!(empty == Phrases::empty());
    assert!(empty.with_backstop(&LIB) == Phrases::empty().with_backstop(&LIB));
    assert!(empty != Phrases::empty().with_backstop(&LIB));
    assert_eq!(&*empty.with_backstop(&LIB).text("lib-sign-in"), "Sign in");
}

#[test]
fn duplicate_id_precedence() {
    static FIRST: Catalog = Catalog::new("first", &[Source::new(Lang::EN_US, "a = First\n")]);
    static SECOND: Catalog = Catalog::new(
        "second",
        &[Source::new(Lang::EN_US, "a = Second\nb = Only second\n")],
    );
    static TWICE: Catalog =
        Catalog::new("twice", &[Source::new(Lang::EN_US, "c = One\nc = Two\n")]);

    let warnings = Warnings::default();

    with_default(warnings.clone(), || {
        let book = Phrasebook::builder()
            .language(Lang::EN_US, "English")
            .catalog(&FIRST)
            .catalog(&SECOND)
            .catalog(&TWICE)
            .build();

        let en = book.phrases(Lang::EN_US);
        assert_eq!(&*en.text("a"), "First");
        assert_eq!(&*en.text("b"), "Only second");
        assert_eq!(&*en.text("c"), "One");
        // `a` across catalogs and `c` within one.
        assert_eq!(warnings.count(), 2);
    });

    // An app overriding a library message inside its prefix is deliberate.
    let warnings = Warnings::default();

    with_default(warnings.clone(), || {
        let book = book();
        assert_eq!(&*book.phrases(Lang::EN_US).text("lib-sign-in"), "Log in");
        assert_eq!(warnings.count(), 0);
    });
}

#[test]
fn equality() {
    let book = book();
    let other = self::book();

    assert!(book.phrases(SV_SE) == book.phrases(SV_SE));
    assert!(book.phrases(SV_SE) != book.phrases(Lang::EN_US));
    assert!(book.phrases(SV_SE) != other.phrases(SV_SE));
    assert!(book.phrases(SV_SE).with_lang(Lang::EN_US) == book.phrases(Lang::EN_US));
    assert!(book == book.clone());
    assert!(book != other);
}

#[test]
fn languages_and_negotiation() {
    let book = book();
    let names = book
        .languages()
        .iter()
        .map(|l| (l.lang, l.name))
        .collect::<Vec<_>>();
    assert_eq!(names, [(Lang::EN_US, "English"), (SV_SE, "Svenska")]);
    assert_eq!(book.phrases(SV_SE).languages(), book.languages());
    assert_eq!(book.fallback(), Lang::EN_US);

    assert_eq!(book.negotiate(["sv-SE"]), SV_SE);
    assert_eq!(book.negotiate(["sv"]), SV_SE);
    assert_eq!(book.negotiate(["de", "sv"]), SV_SE);
    assert_eq!(book.negotiate(["en-GB", "sv"]), Lang::EN_US);
    assert_eq!(book.negotiate(["*", "de"]), Lang::EN_US);
    assert_eq!(book.negotiate([]), Lang::EN_US);
}

#[test]
fn bundles_parse_lazily() {
    // Swedish has a syntax error, logged only once Swedish is used.
    static BROKEN: Catalog = Catalog::new(
        "broken",
        &[
            Source::new(Lang::EN_US, "a = A\n"),
            Source::new(SV_SE, "a = A\n!junk\nc = C\n"),
        ],
    );

    let errors = Arc::new(AtomicUsize::new(0));

    #[derive(Clone)]
    struct Errors(Arc<AtomicUsize>);

    impl Subscriber for Errors {
        fn enabled(&self, _: &Metadata<'_>) -> bool {
            true
        }

        fn new_span(&self, _: &span::Attributes<'_>) -> span::Id {
            span::Id::from_u64(1)
        }

        fn record(&self, _: &span::Id, _: &span::Record<'_>) {}

        fn record_follows_from(&self, _: &span::Id, _: &span::Id) {}

        fn event(&self, event: &Event<'_>) {
            if *event.metadata().level() == Level::ERROR {
                self.0.fetch_add(1, Ordering::SeqCst);
            }
        }

        fn enter(&self, _: &span::Id) {}

        fn exit(&self, _: &span::Id) {}
    }

    with_default(Errors(errors.clone()), || {
        let book = Phrasebook::builder()
            .language(Lang::EN_US, "English")
            .language(SV_SE, "Svenska")
            .catalog(&BROKEN)
            .build();

        assert_eq!(&*book.phrases(Lang::EN_US).text("a"), "A");
        assert_eq!(errors.load(Ordering::SeqCst), 0);

        // What parsed is kept.
        let sv = book.phrases(SV_SE);
        assert_eq!(&*sv.text("a"), "A");
        assert_eq!(&*sv.text("c"), "C");
        assert_eq!(errors.load(Ordering::SeqCst), 1);
    });
}

#[test]
fn check_catalog() {
    check::catalog(&APP).unwrap();
    check::catalog(&LIB).unwrap();

    static BAD: Catalog = Catalog::new(
        "bad",
        &[
            Source::new(
                Lang::EN_US,
                "bad-a = A\nbad-a = Again\nb = B\nbad-c = { $x } { missing-ref }\n!junk\n-bad-term = T\n-term = T\n",
            ),
            Source::new(Lang::EN_US, "bad-a = A\n"),
        ],
    )
    .with_prefix("bad-");

    let problems = check::catalog(&BAD).unwrap_err();
    let messages = problems.iter().map(|p| p.message()).collect::<Vec<_>>();

    assert_eq!(
        messages,
        [
            "syntax error on line 5: Expected one of \"a-zA-Z\"",
            "`bad-a` is defined twice",
            "`b` does not start with the catalog's prefix `bad-`",
            "`-term` does not start with the catalog's prefix `bad-`",
            "`bad-c` does not format: Resolver error: Unknown message: missing-ref",
            "a second source for this language, which is ignored",
        ],
        "{problems}"
    );

    assert!(problems.iter().all(|p| p.catalog() == "bad"));
    assert!(problems.iter().all(|p| p.lang() == Lang::EN_US));
}

#[test]
fn check_parity_and_ids() {
    assert_eq!(check::ids(&APP, SV_SE), ["hello", "greeting", "items"]);
    assert!(check::ids(&APP, Lang::new("fr")).is_empty());

    let problems = check::parity(&APP, SV_SE).unwrap_err();
    let messages = problems.iter().map(|p| p.message()).collect::<Vec<_>>();
    assert_eq!(
        messages,
        [
            "`only-english` is missing (defined in en-US)",
            "`nested` is missing (defined in en-US)",
            "`code` is missing (defined in en-US)",
            "`mixed` is missing (defined in en-US)",
            "`lib-sign-in` is missing (defined in en-US)",
        ]
    );

    let problems = check::parity(&APP, DE_DE).unwrap_err();
    assert_eq!(
        problems.iter().map(|p| p.message()).collect::<Vec<_>>(),
        [
            "`greeting` is missing (defined in en-US)",
            "`only-english` is missing (defined in en-US)",
            "`items` is missing (defined in en-US)",
            "`nested` is missing (defined in en-US)",
            "`code` is missing (defined in en-US)",
            "`mixed` is missing (defined in en-US)",
            "`lib-sign-in` is missing (defined in en-US)",
        ]
    );

    static EXTRA: Catalog = Catalog::new(
        "extra",
        &[
            Source::new(Lang::EN_US, "a = A\n"),
            Source::new(SV_SE, "a = A\nb = B\n"),
        ],
    );

    let problems = check::parity(&EXTRA, SV_SE).unwrap_err();
    assert_eq!(
        problems.to_string(),
        "extra sv-SE: `b` is not defined in en-US"
    );

    assert!(check::parity(&LIB, Lang::new("fr")).is_err());
}

#[test]
fn check_parity_with_libraries() {
    // The app translates one of the library's messages, misspells another,
    // and has a stray id of its own.
    static TRANSLATING: Catalog = Catalog::new(
        "app",
        &[
            Source::new(Lang::EN_US, "a = A\nb = B\n"),
            Source::new(
                SV_SE,
                "a = A\nlib-sign-in = Logga in\nlib-sign-up = Registrera\nc = C\n",
            ),
        ],
    );

    // Without libraries, parity reports every id English lacks, and
    // parity_with with none says the same.
    let problems = check::parity(&TRANSLATING, SV_SE).unwrap_err();
    assert_eq!(
        problems.to_string(),
        "\
app sv-SE: `b` is missing (defined in en-US)
app sv-SE: `lib-sign-in` is not defined in en-US
app sv-SE: `lib-sign-up` is not defined in en-US
app sv-SE: `c` is not defined in en-US"
    );
    assert_eq!(
        check::parity_with(&TRANSLATING, SV_SE, &[]).unwrap_err(),
        problems
    );

    // With the library, its id is accepted; an unknown id under its prefix
    // and the stray id are still reported, and the library's other ids are
    // not required.
    let problems = check::parity_with(&TRANSLATING, SV_SE, &[&LIB]).unwrap_err();
    assert_eq!(
        problems.to_string(),
        "\
app sv-SE: `b` is missing (defined in en-US)
app sv-SE: `lib-sign-up` is defined neither in en-US nor in any given library
app sv-SE: `c` is defined neither in en-US nor in any given library"
    );

    // Only a library's default language counts: an id the library defines
    // in Swedish alone is not accepted.
    static PARTIAL: Catalog = Catalog::new(
        "lib",
        &[
            Source::new(Lang::EN_US, "lib-sign-in = Sign in\n"),
            Source::new(SV_SE, "lib-sign-in = Logga in\nlib-sign-up = Registrera\n"),
        ],
    )
    .with_prefix("lib-");

    let problems = check::parity_with(&TRANSLATING, SV_SE, &[&PARTIAL]).unwrap_err();
    assert_eq!(
        problems,
        check::parity_with(&TRANSLATING, SV_SE, &[&LIB]).unwrap_err()
    );

    // Missing ids are reported as before.
    let problems = check::parity_with(&APP, SV_SE, &[&LIB]).unwrap_err();
    assert_eq!(problems, check::parity(&APP, SV_SE).unwrap_err());
}

#[cfg(feature = "koine01")]
#[test]
fn koine01_warns_about_unknown_languages() {
    let warnings = Warnings::default();

    with_default(warnings.clone(), || {
        let _ = Phrasebook::builder()
            .language(Lang::EN_US, "English")
            .language(Lang::new("sv"), "Svenska")
            .language(Lang::new("fil-PH"), "Filipino")
            .build();
    });

    assert_eq!(warnings.count(), 0);

    with_default(warnings.clone(), || {
        let _ = Phrasebook::builder()
            .language(Lang::EN_US, "English")
            .language(Lang::new("xx"), "Unknown")
            .language(Lang::new("sv-JP"), "Svenska")
            .language(Lang::new("en-ZZ"), "English")
            .build();
    });

    assert_eq!(warnings.count(), 3);
}

#[cfg(feature = "koine01")]
#[test]
fn koine01_language_named_by_koine() {
    let warnings = Warnings::default();

    let book = with_default(warnings.clone(), || {
        Phrasebook::builder()
            .language_named_by_koine(SV_SE)
            .language_named_by_koine(Lang::new("fil"))
            .language_named_by_koine(Lang::new("xx-YY"))
            // Already declared: keeps its name.
            .language_named_by_koine(SV_SE)
            .build()
    });

    let names = book
        .languages()
        .iter()
        .map(|l| (l.lang, l.name))
        .collect::<Vec<_>>();

    assert_eq!(
        names,
        [
            (SV_SE, "Swedish"),
            (Lang::new("fil"), "Filipino"),
            (Lang::new("xx-YY"), "xx-YY"),
        ]
    );
    assert_eq!(book.fallback(), SV_SE);
    assert_eq!(warnings.count(), 1);
}