Skip to content

Commit 6b4eb16

Browse files
Xiretzaoliver-ni
andcommitted
Add options for cardinal/ordinal
Co-Authored-By: Oliver Ni <[email protected]>
1 parent 1834376 commit 6b4eb16

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

fluent-bundle/src/types/mod.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,16 @@ impl<'source> FluentValue<'source> {
199199
};
200200
// This string matches a plural rule keyword. Check if the number
201201
// matches the plural rule category.
202+
let r#type = match b.options.r#type {
203+
FluentNumberType::Cardinal => PluralRuleType::CARDINAL,
204+
FluentNumberType::Ordinal => PluralRuleType::ORDINAL,
205+
};
202206
scope
203207
.bundle
204208
.intls
205-
.with_try_get_threadsafe::<PluralRules, _, _>(
206-
(PluralRuleType::CARDINAL,),
207-
|pr| pr.0.select(b) == Ok(cat),
208-
)
209+
.with_try_get_threadsafe::<PluralRules, _, _>((r#type,), |pr| {
210+
pr.0.select(b) == Ok(cat)
211+
})
209212
.unwrap()
210213
}
211214
_ => false,

fluent-bundle/src/types/number.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,28 @@ use intl_pluralrules::operands::PluralOperands;
88
use crate::args::FluentArgs;
99
use crate::types::FluentValue;
1010

11+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
12+
pub enum FluentNumberType {
13+
Cardinal,
14+
Ordinal,
15+
}
16+
17+
impl std::default::Default for FluentNumberType {
18+
fn default() -> Self {
19+
Self::Cardinal
20+
}
21+
}
22+
23+
impl From<&str> for FluentNumberType {
24+
fn from(input: &str) -> Self {
25+
match input {
26+
"cardinal" => Self::Cardinal,
27+
"ordinal" => Self::Ordinal,
28+
_ => Self::default(),
29+
}
30+
}
31+
}
32+
1133
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
1234
pub enum FluentNumberStyle {
1335
Decimal,
@@ -58,6 +80,7 @@ impl From<&str> for FluentNumberCurrencyDisplayStyle {
5880

5981
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
6082
pub struct FluentNumberOptions {
83+
pub r#type: FluentNumberType,
6184
pub style: FluentNumberStyle,
6285
pub currency: Option<String>,
6386
pub currency_display: FluentNumberCurrencyDisplayStyle,
@@ -72,6 +95,7 @@ pub struct FluentNumberOptions {
7295
impl Default for FluentNumberOptions {
7396
fn default() -> Self {
7497
Self {
98+
r#type: Default::default(),
7599
style: Default::default(),
76100
currency: None,
77101
currency_display: Default::default(),
@@ -89,6 +113,9 @@ impl FluentNumberOptions {
89113
pub fn merge(&mut self, opts: &FluentArgs) {
90114
for (key, value) in opts.iter() {
91115
match (key, value) {
116+
("type", FluentValue::String(n)) => {
117+
self.r#type = n.as_ref().into();
118+
}
92119
("style", FluentValue::String(n)) => {
93120
self.style = n.as_ref().into();
94121
}

0 commit comments

Comments
 (0)