Skip to content

Commit 6a796e7

Browse files
committed
Support unic-langid with CLDR backed likelysubtags
1 parent 01b4e49 commit 6a796e7

File tree

3 files changed

+34
-22
lines changed

3 files changed

+34
-22
lines changed

Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,19 @@ coveralls = { repository = "projectfluent/fluent-locale-rs", branch = "master",
1919
maintenance = { status = "actively-developed" }
2020

2121
[dependencies]
22-
unic-langid = "0.5"
22+
unic-langid = "0.5.2"
2323

2424
[dev-dependencies]
2525
serde = { version = "1.0", features = ["derive"] }
2626
serde_json = "1.0"
27-
unic-langid = { version = "0.5", features = ["macros"] }
27+
unic-langid = { version = "0.5.2", features = ["macros"] }
2828
unic-locale = { version = "0.5", features = ["macros"] }
2929
criterion = "0.3"
3030

3131
[[bench]]
3232
name = "negotiate"
33-
harness = false
33+
harness = false
34+
35+
[features]
36+
default = []
37+
cldr = ["unic-langid/likelysubtags"]

src/negotiate/likely_subtags.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ static REGION_MATCHING_KEYS: &[&str] = &[
44
"az", "bg", "cs", "de", "es", "fi", "fr", "hu", "it", "lt", "lv", "nl", "pl", "ro", "ru",
55
];
66

7-
pub fn add(langid: &LanguageIdentifier) -> Option<LanguageIdentifier> {
8-
let extended = match langid.to_string().as_str() {
7+
pub trait MockLikelySubtags {
8+
fn add_likely_subtags(&mut self) -> bool;
9+
}
10+
11+
impl MockLikelySubtags for LanguageIdentifier {
12+
fn add_likely_subtags(&mut self) -> bool {
13+
let extended = match self.to_string().as_str() {
914
"en" => "en-Latn-US",
1015
"fr" => "fr-Latn-FR",
1116
"sr" => "sr-Cyrl-SR",
@@ -14,19 +19,21 @@ pub fn add(langid: &LanguageIdentifier) -> Option<LanguageIdentifier> {
1419
"zh-GB" => "zh-Hant-GB",
1520
"zh-US" => "zh-Hant-US",
1621
_ => {
17-
let lang = langid.get_language();
22+
let lang = self.get_language();
1823

1924
for subtag in REGION_MATCHING_KEYS {
20-
if lang == *subtag {
21-
let mut new_lang = langid.clone();
22-
new_lang.set_region(Some(subtag)).unwrap();
23-
return Some(new_lang);
24-
}
25+
if lang == *subtag {
26+
self.set_region(Some(subtag)).unwrap();
27+
return true;
28+
}
2529
}
26-
return None;
30+
return false;
2731
}
28-
};
29-
let langid: LanguageIdentifier = extended.parse().expect("Failed to parse langid.");
30-
31-
Some(langid)
32+
};
33+
let langid: LanguageIdentifier = extended.parse().expect("Failed to parse langid.");
34+
self.set_language(Some(langid.get_language())).unwrap();
35+
self.set_script(langid.get_script()).unwrap();
36+
self.set_region(langid.get_region()).unwrap();
37+
return true;
38+
}
3239
}

src/negotiate/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@
121121
122122
use std::collections::HashMap;
123123
use unic_langid::LanguageIdentifier;
124+
125+
#[cfg(not(feature = "cldr"))]
124126
mod likely_subtags;
127+
#[cfg(not(feature = "cldr"))]
128+
use likely_subtags::MockLikelySubtags;
125129

126130
#[derive(PartialEq, Debug, Clone, Copy)]
127131
pub enum NegotiationStrategy {
@@ -144,7 +148,7 @@ pub fn filter_matches<'a, R: 'a + AsRef<LanguageIdentifier>, A: 'a + AsRef<Langu
144148
}
145149

146150
for req in requested {
147-
let req = req.as_ref();
151+
let mut req = req.as_ref().to_owned();
148152
if req.get_language() == "und" {
149153
continue;
150154
}
@@ -200,7 +204,7 @@ pub fn filter_matches<'a, R: 'a + AsRef<LanguageIdentifier>, A: 'a + AsRef<Langu
200204
match_found = false;
201205

202206
// 3) Try to match against a maximized version of the requested locale
203-
let mut req = if let Some(req) = likely_subtags::add(req) {
207+
if req.add_likely_subtags() {
204208
av_map.retain(|key, value| {
205209
if strategy != NegotiationStrategy::Filtering && match_found {
206210
return true;
@@ -223,9 +227,6 @@ pub fn filter_matches<'a, R: 'a + AsRef<LanguageIdentifier>, A: 'a + AsRef<Langu
223227
}
224228

225229
match_found = false;
226-
req
227-
} else {
228-
req.to_owned()
229230
};
230231

231232
// 4) Try to match against a variant as a range
@@ -255,7 +256,7 @@ pub fn filter_matches<'a, R: 'a + AsRef<LanguageIdentifier>, A: 'a + AsRef<Langu
255256

256257
// 5) Try to match against the likely subtag without region
257258
req.set_region(None).unwrap();
258-
if let Some(req) = likely_subtags::add(&req) {
259+
if req.add_likely_subtags() {
259260
av_map.retain(|key, value| {
260261
if strategy != NegotiationStrategy::Filtering && match_found {
261262
return true;

0 commit comments

Comments
 (0)