Skip to content

Commit e04e458

Browse files
committed
Re-add cldr feature
1 parent c1b8ff7 commit e04e458

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ maintenance = { status = "actively-developed" }
2626

2727
[dependencies]
2828
icu_locid = "1.4"
29+
icu_locid_transform = { version = "1.4", optional = true }
2930

3031
[dev-dependencies]
3132
serde = { version = "1.0", features = ["derive"] }
@@ -38,3 +39,4 @@ harness = false
3839

3940
[features]
4041
default = []
42+
cldr = ["icu_locid_transform"]

src/negotiate/likely_subtags.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,21 @@ static REGION_MATCHING_KEYS: &[(Language, Region)] = &[
2222
(language!("ru"), region!("RU")),
2323
];
2424

25-
pub trait MockLikelySubtags {
26-
fn maximize(&mut self) -> bool;
25+
#[derive(PartialEq, Eq, Debug)]
26+
pub enum TransformResult {
27+
Modified,
28+
Unmodified,
2729
}
2830

29-
impl MockLikelySubtags for LanguageIdentifier {
30-
fn maximize(&mut self) -> bool {
31-
let extended = match &self {
31+
pub struct LocaleExpander;
32+
33+
impl LocaleExpander {
34+
pub fn new() -> Self {
35+
Self
36+
}
37+
38+
pub fn maximize(&self, input: &mut LanguageIdentifier) -> TransformResult {
39+
let extended = match &input {
3240
b if *b == &langid!("en") => langid!("en-Latn-US"),
3341
b if *b == &langid!("fr") => langid!("fr-Latn-FR"),
3442
b if *b == &langid!("sr") => langid!("sr-Cyrl-SR"),
@@ -37,20 +45,20 @@ impl MockLikelySubtags for LanguageIdentifier {
3745
b if *b == &langid!("zh-GB") => langid!("zh-Hant-GB"),
3846
b if *b == &langid!("zh-US") => langid!("zh-Hant-US"),
3947
_ => {
40-
let lang = &self.language;
48+
let lang = &input.language;
4149

4250
if let Ok(idx) = REGION_MATCHING_KEYS.binary_search_by(|(l, _)| l.cmp(lang)) {
4351
let subtag = REGION_MATCHING_KEYS[idx].1;
44-
self.region = Some(subtag);
45-
return true;
52+
input.region = Some(subtag);
53+
return TransformResult::Modified;
4654
}
47-
return false;
55+
return TransformResult::Unmodified;
4856
}
4957
};
5058
let (language, script, region) = (extended.language, extended.script, extended.region);
51-
self.language = language;
52-
self.script = script;
53-
self.region = region;
54-
true
59+
input.language = language;
60+
input.script = script;
61+
input.region = region;
62+
TransformResult::Modified
5563
}
5664
}

src/negotiate/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,10 @@ use icu_locid::LanguageIdentifier;
123123

124124
#[cfg(not(feature = "cldr"))]
125125
mod likely_subtags;
126+
#[cfg(feature = "cldr")]
127+
use icu_locid_transform::{LocaleExpander, TransformResult};
126128
#[cfg(not(feature = "cldr"))]
127-
use likely_subtags::MockLikelySubtags;
129+
use likely_subtags::{LocaleExpander, TransformResult};
128130

129131
#[derive(PartialEq, Debug, Clone, Copy)]
130132
pub enum NegotiationStrategy {
@@ -163,6 +165,8 @@ pub fn filter_matches<'a, R: 'a + AsRef<LanguageIdentifier>, A: 'a + AsRef<Langu
163165
available: &'a [A],
164166
strategy: NegotiationStrategy,
165167
) -> Vec<&'a A> {
168+
let lc = LocaleExpander::new();
169+
166170
let mut supported_locales = vec![];
167171

168172
let mut available_locales: Vec<&A> = available.iter().collect();
@@ -210,7 +214,7 @@ pub fn filter_matches<'a, R: 'a + AsRef<LanguageIdentifier>, A: 'a + AsRef<Langu
210214

211215
let mut req = req.to_owned();
212216
// 3) Try to match against a maximized version of the requested locale
213-
if req.maximize() {
217+
if lc.maximize(&mut req) == TransformResult::Modified {
214218
test_strategy!(req, true, false);
215219
}
216220

@@ -220,7 +224,7 @@ pub fn filter_matches<'a, R: 'a + AsRef<LanguageIdentifier>, A: 'a + AsRef<Langu
220224

221225
// 5) Try to match against the likely subtag without region
222226
req.region = None;
223-
if req.maximize() {
227+
if lc.maximize(&mut req) == TransformResult::Modified {
224228
test_strategy!(req, true, false);
225229
}
226230

0 commit comments

Comments
 (0)