| 1 |
# icu_plurals [](https://crates.io/crates/icu_plurals) |
| 2 |
|
| 3 |
<!-- cargo-rdme start --> |
| 4 |
|
| 5 |
Determine the plural category appropriate for a given number in a given language. |
| 6 |
|
| 7 |
This module is published as its own crate ([`icu_plurals`](https://docs.rs/icu_plurals/latest/icu_plurals/)) |
| 8 |
and as part of the [`icu`](https://docs.rs/icu/latest/icu/) crate. See the latter for more details on the ICU4X project. |
| 9 |
|
| 10 |
For example in English, when constructing a message |
| 11 |
such as `{ num } items`, the user has to prepare |
| 12 |
two variants of the message: |
| 13 |
|
| 14 |
* `1 item` |
| 15 |
* `0 items`, `2 items`, `5 items`, `0.5 items` etc. |
| 16 |
|
| 17 |
The former variant is used when the placeholder variable has value `1`, |
| 18 |
while the latter is used for all other values of the variable. |
| 19 |
|
| 20 |
Unicode defines [Language Plural Rules] as a mechanism to codify those |
| 21 |
variants and provides data and algorithms to calculate |
| 22 |
appropriate [`PluralCategory`]. |
| 23 |
|
| 24 |
## Examples |
| 25 |
|
| 26 |
```rust |
| 27 |
use icu::locale::locale; |
| 28 |
use icu::plurals::{PluralCategory, PluralRules}; |
| 29 |
|
| 30 |
let pr = PluralRules::try_new(locale!("en").into(), Default::default()) |
| 31 |
.expect("locale should be present"); |
| 32 |
|
| 33 |
assert_eq!(pr.category_for(5_usize), PluralCategory::Other); |
| 34 |
``` |
| 35 |
|
| 36 |
### Plural Rules |
| 37 |
|
| 38 |
The crate provides the main struct [`PluralRules`] which handles selection |
| 39 |
of the correct [`PluralCategory`] for a given language and [`PluralRuleType`]. |
| 40 |
|
| 41 |
### Plural Category |
| 42 |
|
| 43 |
Every number in every language belongs to a certain [`PluralCategory`]. |
| 44 |
For example, the Polish language uses four: |
| 45 |
|
| 46 |
* [`One`](PluralCategory::One): `1 miesiąc` |
| 47 |
* [`Few`](PluralCategory::Few): `2 miesiące` |
| 48 |
* [`Many`](PluralCategory::Many): `5 miesięcy` |
| 49 |
* [`Other`](PluralCategory::Other): `1.5 miesiąca` |
| 50 |
|
| 51 |
### `PluralRuleType` |
| 52 |
|
| 53 |
Plural rules depend on the use case. This crate supports two types of plural rules: |
| 54 |
|
| 55 |
* [`Cardinal`](PluralRuleType::Cardinal): `3 doors`, `1 month`, `10 dollars` |
| 56 |
* [`Ordinal`](PluralRuleType::Ordinal): `1st place`, `10th day`, `11th floor` |
| 57 |
|
| 58 |
[Language Plural Rules]: https://unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules |
| 59 |
|
| 60 |
<!-- cargo-rdme end --> |
| 61 |
|
| 62 |
## More Information |
| 63 |
|
| 64 |
For more information on development, authorship, contributing etc. please visit [`ICU4X home page`](https://github.com/unicode-org/icu4x). |