Files
Marcos Paulo 83412fde83 feat(i18n): wave 5 i18n
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-13 20:01:23 -03:00

105 lines
4.5 KiB
Rust

//! Integration tests: lookup, fallbacks, placeholders and plural forms.
use notescraft_i18n::{Catalog, I18n, PluralRule};
/// Parses or fails the test with the error text.
fn parse(locale: &str, source: &str) -> Catalog {
match Catalog::from_toml(locale, source) {
Ok(cat) => cat,
Err(e) => {
assert_eq!(e.to_string(), format!("expected a catalog, got: {e:?}"));
Catalog::default()
}
}
}
#[test]
fn fallback_order_is_active_then_en_then_key() {
let fr = parse("fr", "buttons.save = \"Enregistrer\"\nonly.fr = \"Ici\"\n");
let mut i18n = I18n::new().with_locale(fr);
assert_eq!(i18n.t("buttons.save"), "Enregistrer", "active wins");
assert_eq!(i18n.t("menus.file"), "File", "base en is the fallback");
assert_eq!(i18n.t("only.fr"), "Ici", "key unknown to base still resolves");
}
#[test]
fn missing_keys_return_key_and_are_recorded_once() {
let mut i18n = I18n::new();
assert_eq!(i18n.t("screen.unknown.thing"), "screen.unknown.thing");
assert_eq!(i18n.t("screen.unknown.thing"), "screen.unknown.thing");
assert_eq!(i18n.t("other.missing"), "other.missing");
assert_eq!(i18n.t("buttons.save"), "Save");
assert_eq!(i18n.missing_keys(), &["screen.unknown.thing".to_owned(), "other.missing".to_owned()], "each distinct key recorded once, in request order");
}
#[test]
fn placeholder_substitution_through_lookup() {
let mut i18n = I18n::new();
assert_eq!(i18n.t_args("assistant.turns", &[("count", "3")]), "You have 3 messages in this conversation.");
let cat = parse("xx", "greet = \"Aloha {name}, aloha {name}!\"\n");
let mut i18n = I18n::new().with_locale(cat);
assert_eq!(i18n.t_args("greet", &[("name", "Kim")]), "Aloha Kim, aloha Kim!");
assert_eq!(i18n.t_args("greet", &[]), "Aloha {name}, aloha {name}!", "unknown placeholders stay");
assert_eq!(i18n.t_args("greet", &[("name", ""), ("other", "x")]), "Aloha , aloha !", "values are inserted verbatim");
}
#[test]
fn placeholder_values_are_not_reexpanded() {
let cat = parse("xx", "chain = \"{a}\"\n");
let mut i18n = I18n::new().with_locale(cat);
let out = i18n.t_args("chain", &[("a", "{b}"), ("b", "boom")]);
assert_eq!(out, "{b}", "injection through a value is not expanded");
}
#[test]
fn english_plural_uses_zero_one_other() {
let cat = parse("en", "test.count.zero = \"No notes\"\ntest.count.one = \"1 note\"\ntest.count.other = \"{n} notes\"\n");
let mut i18n = I18n::new().with_locale(cat).with_rule(PluralRule::English);
assert_eq!(i18n.plural("test.count", 0), "No notes");
assert_eq!(i18n.plural("test.count", 1), "1 note");
assert_eq!(i18n.plural("test.count", 2), "{n} notes");
}
#[test]
fn zero_form_is_optional() {
let cat = parse("en", "test.count.one = \"1 note\"\ntest.count.other = \"{n} notes\"\n");
let mut i18n = I18n::new().with_locale(cat).with_rule(PluralRule::English);
assert_eq!(i18n.plural("test.count", 0), "{n} notes", "no zero form: fall through to other");
}
#[test]
fn one_other_rule_skips_special_zero() {
let cat = parse("de", "files.selected.one = \"1 Datei\"\nfiles.selected.other = \"{n} Dateien\"\n");
let mut i18n = I18n::new().with_locale(cat).with_rule(PluralRule::OneOther);
assert_eq!(i18n.plural("files.selected", 1), "1 Datei");
assert_eq!(i18n.plural("files.selected", 5), "{n} Dateien");
}
#[test]
fn none_rule_always_uses_other() {
let cat = parse("ja", "files.selected.other = \"{n} 件\"\nfiles.selected.one = \"1件\"\n");
let mut i18n = I18n::new().with_locale(cat).with_rule(PluralRule::None);
assert_eq!(i18n.plural("files.selected", 0), "{n} 件");
assert_eq!(i18n.plural("files.selected", 1), "{n} 件");
}
#[test]
fn plural_falls_back_across_locales_and_records_missing() {
let cat = parse("xx", "unrelated = \"x\"\n");
let mut i18n = I18n::new().with_locale(cat);
assert_eq!(i18n.plural("notes.count", 0), "No notes", "resolved from base en");
let err_key = "void.count";
assert_eq!(i18n.plural(err_key, 3), "void.count.other");
assert_eq!(i18n.missing_keys(), &["void.count.other".to_owned()], "only the final failing form is recorded, once");
}
#[test]
fn plural_missing_form_falls_to_other_inside_catalog() {
let cat = parse("en", "test.count.one = \"1 note\"\n");
let mut i18n = I18n::new().with_locale(cat);
assert_eq!(i18n.plural("test.count", 1), "1 note");
assert_eq!(i18n.plural("test.count", 5), "test.count.other");
assert_eq!(i18n.plural("test.count", 5), "test.count.other");
assert_eq!(i18n.missing_keys(), &["test.count.other".to_owned()]);
}