Struct icu_plurals::PluralRules[][src]

pub struct PluralRules { /* fields omitted */ }
Expand description

A struct which provides an ability to retrieve an appropriate Plural Category for a given number.

Examples

use icu::locid::macros::langid;
use icu::plurals::{PluralRules, PluralRuleType, PluralCategory};
use icu_provider::inv::InvariantDataProvider;

let lid = langid!("en");

let dp = InvariantDataProvider;

let pr = PluralRules::try_new(lid, &dp, PluralRuleType::Cardinal)
    .expect("Failed to construct a PluralRules struct.");

assert_eq!(pr.select(5_usize), PluralCategory::Other);

Implementations

Constructs a new PluralRules for a given locale, type and data provider.

This constructor will fail if the Data Provider does not have the data.

Examples

use icu::locid::macros::langid;
use icu::plurals::{PluralRules, PluralRuleType};
use icu_provider::inv::InvariantDataProvider;

let lid = langid!("en");

let dp = InvariantDataProvider;

let _ = PluralRules::try_new(lid, &dp, PluralRuleType::Cardinal);

Returns the Plural Category appropriate for the given number.

Examples

use icu::locid::macros::langid;
use icu::plurals::{PluralRules, PluralRuleType, PluralCategory};
use icu_provider::inv::InvariantDataProvider;

let lid = langid!("en");

let dp = InvariantDataProvider;

let pr = PluralRules::try_new(lid, &dp, PluralRuleType::Cardinal)
    .expect("Failed to construct a PluralRules struct.");

match pr.select(1_usize) {
    PluralCategory::One => "One item",
    PluralCategory::Other => "Many items",
    _ => { unreachable!(); }
};

The method accepts any input that can be calculated into Plural Operands. All unsigned primitive number types can infallibly be converted so they can be used as an input.

For signed numbers and strings, Plural Operands implement TryFrom and FromStr, which should be used before passing the result to select().

Examples

use std::convert::TryFrom;
use icu::locid::macros::langid;
use icu::plurals::{PluralRules, PluralRuleType};
use icu::plurals::{PluralCategory, PluralOperands};
use icu_provider::inv::InvariantDataProvider;

let operands = PluralOperands::try_from(-5)
    .expect("Failed to parse to operands.");
let operands2: PluralOperands = "5.10".parse()
    .expect("Failed to parse to operands.");

assert_eq!(pr.select(operands), PluralCategory::Other);
assert_eq!(pr.select(operands2), PluralCategory::Other);

Returns all Plural Categories appropriate for a PluralRules object based on the LanguageIdentifier and PluralRuleType.

The Plural Categories are returned in alphabetically sorted order.

The category PluralCategory::Other is always included.

Examples

use icu::locid::macros::langid;
use icu::plurals::{PluralRules, PluralRuleType, PluralCategory};
use icu_provider::inv::InvariantDataProvider;

let lid = langid!("fr");

let dp = icu_testdata::get_provider();

let pr = PluralRules::try_new(lid, &dp, PluralRuleType::Cardinal)
    .expect("Failed to construct a PluralRules struct.");

let mut categories = pr.categories();
assert_eq!(categories.next(), Some(&PluralCategory::Many));
assert_eq!(categories.next(), Some(&PluralCategory::One));
assert_eq!(categories.next(), Some(&PluralCategory::Other));
assert_eq!(categories.next(), None);

Lower-level constructor that allows constructing a PluralRules directly from data obtained from a provider.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Clone this trait object reference, returning a boxed trait object.

Return this boxed trait object as Box<dyn Any>. Read more

Return this trait object reference as &dyn Any. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.