Function pluralize

Source
pub fn pluralize<C, S, P>(
    count: C,
    singular: S,
    plural: P,
) -> Result<Pluralize<S, P>, C::Error>
where C: PluralizeCount,
Expand description

For a value of ±1 by default an empty string "" is returned, otherwise "s".

§Examples

§With default arguments

/// ```jinja
/// I have {{dogs}} dog{{dogs|pluralize}} and {{cats}} cat{{cats|pluralize}}.
/// ```
#[derive(Template)]
#[template(ext = "html", in_doc = true)]
struct Pets {
    dogs: i8,
    cats: i8,
}

assert_eq!(
    Pets { dogs: 0, cats: 0 }.to_string(),
    "I have 0 dogs and 0 cats."
);
assert_eq!(
    Pets { dogs: 1, cats: 1 }.to_string(),
    "I have 1 dog and 1 cat."
);
assert_eq!(
    Pets { dogs: -1, cats: 99 }.to_string(),
    "I have -1 dog and 99 cats."
);

§Overriding the singular case

/// ```jinja
/// I have {{dogs}} dog{{ dogs|pluralize("go") }}.
/// ```
#[derive(Template)]
#[template(ext = "html", in_doc = true)]
struct Dog {
    dogs: i8,
}

assert_eq!(
    Dog { dogs: 0 }.to_string(),
    "I have 0 dogs."
);
assert_eq!(
    Dog { dogs: 1 }.to_string(),
    "I have 1 doggo."
);

§Overriding singular and plural cases

/// ```jinja
/// I have {{mice}} {{ mice|pluralize("mouse", "mice") }}.
/// ```
#[derive(Template)]
#[template(ext = "html", in_doc = true)]
struct Mice {
    mice: i8,
}

assert_eq!(
    Mice { mice: 42 }.to_string(),
    "I have 42 mice."
);
assert_eq!(
    Mice { mice: 1 }.to_string(),
    "I have 1 mouse."
);

§Arguments get escaped

/// ```jinja
/// You are number {{ number|pluralize("<b>ONE</b>", number) }}!
/// ```
#[derive(Template)]
#[template(ext = "html", in_doc = true)]
struct Number {
    number: usize
}

assert_eq!(
    Number { number: 1 }.to_string(),
    "You are number &#60;b&#62;ONE&#60;/b&#62;!",
);
assert_eq!(
    Number { number: 9000 }.to_string(),
    "You are number 9000!",
);