Skip to content

Commit b9ea1cb

Browse files
Xiretzaoliver-ni
authored andcommitted
feat(fluent-bundle): Add options for cardinal/ordinal
Co-Authored-By: Oliver Ni <[email protected]>
1 parent 704e4ae commit b9ea1cb

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,23 @@ use intl_pluralrules::operands::PluralOperands;
88
use crate::args::FluentArgs;
99
use crate::types::FluentValue;
1010

11+
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)]
12+
pub enum FluentNumberType {
13+
#[default]
14+
Cardinal,
15+
Ordinal,
16+
}
17+
18+
impl From<&str> for FluentNumberType {
19+
fn from(input: &str) -> Self {
20+
match input {
21+
"cardinal" => Self::Cardinal,
22+
"ordinal" => Self::Ordinal,
23+
_ => Self::default(),
24+
}
25+
}
26+
}
27+
1128
#[derive(Debug, Copy, Clone, Default, Hash, PartialEq, Eq)]
1229
pub enum FluentNumberStyle {
1330
#[default]
@@ -48,6 +65,7 @@ impl From<&str> for FluentNumberCurrencyDisplayStyle {
4865

4966
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
5067
pub struct FluentNumberOptions {
68+
pub r#type: FluentNumberType,
5169
pub style: FluentNumberStyle,
5270
pub currency: Option<String>,
5371
pub currency_display: FluentNumberCurrencyDisplayStyle,
@@ -62,6 +80,7 @@ pub struct FluentNumberOptions {
6280
impl Default for FluentNumberOptions {
6381
fn default() -> Self {
6482
Self {
83+
r#type: Default::default(),
6584
style: Default::default(),
6685
currency: None,
6786
currency_display: Default::default(),
@@ -79,6 +98,9 @@ impl FluentNumberOptions {
7998
pub fn merge(&mut self, opts: &FluentArgs) {
8099
for (key, value) in opts.iter() {
81100
match (key, value) {
101+
("type", FluentValue::String(n)) => {
102+
self.r#type = n.as_ref().into();
103+
}
82104
("style", FluentValue::String(n)) => {
83105
self.style = n.as_ref().into();
84106
}

0 commit comments

Comments
 (0)