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
pub fn try_new<'data, D: DataProvider<'data, PluralRuleStringsV1Marker> + ?Sized>(
langid: LanguageIdentifier,
data_provider: &D,
type_: PluralRuleType
) -> Result<Self, PluralRulesError>
pub fn try_new<'data, D: DataProvider<'data, PluralRuleStringsV1Marker> + ?Sized>(
langid: LanguageIdentifier,
data_provider: &D,
type_: PluralRuleType
) -> Result<Self, PluralRulesError>
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);
pub fn new_from_data(
langid: LanguageIdentifier,
data: &PluralRuleStringsV1<'_>
) -> Result<Self, PluralRulesError>
pub fn new_from_data(
langid: LanguageIdentifier,
data: &PluralRuleStringsV1<'_>
) -> Result<Self, PluralRulesError>
Lower-level constructor that allows constructing a PluralRules
directly from
data obtained from a provider.
Auto Trait Implementations
impl RefUnwindSafe for PluralRules
impl Send for PluralRules
impl Sync for PluralRules
impl Unpin for PluralRules
impl UnwindSafe for PluralRules
Blanket Implementations
Mutably borrows from an owned value. Read more
Clone this trait object reference, returning a boxed trait object.