Skip to content

Commit f21de98

Browse files
authored
ICU4X rel date time - non-Latn number system --> unsupported (#289)
* ICU4X relative date time - label non-Latn numbering system as unsupported * Better setting of unicode extension in locale
1 parent e35ad94 commit f21de98

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

executors/rust/1.3/src/relativedatetime_fmt.rs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
// https://docs.rs/icu/1.3.2/icu/relativetime/struct.RelativeTimeFormatter.html
2+
// https://docs.rs/icu_provider/1.3.0/icu_provider/struct.DataLocale.html#method.get_unicode_ext
23

34
use fixed_decimal::FixedDecimal;
5+
6+
use icu::locid::extensions::unicode;
7+
use icu::locid::extensions::unicode::key;
48
use icu::locid::Locale;
9+
10+
use std::str::FromStr;
11+
512
use icu_provider::DataLocale;
613

714
use icu::relativetime::options::Numeric;
@@ -128,16 +135,13 @@ pub fn run_relativedatetimeformat_test(json_obj: &Value) -> Result<Value, String
128135
let option_struct: RelativeDateTimeFormatterOptions =
129136
serde_json::from_str(&options.to_string()).unwrap();
130137

131-
let numbering_system_str = &option_struct.numbering_system;
138+
let numbering_system_str: &Option<String> = &option_struct.numbering_system;
132139

140+
// Set up the locale with its options.
133141
let locale_json_str: &str = json_obj["locale"].as_str().unwrap();
134-
let mut locale_str: String = locale_json_str.to_string();
135-
if numbering_system_str.is_some() {
136-
locale_str =
137-
locale_json_str.to_string() + "-u-nu-" + &numbering_system_str.as_ref().unwrap();
138-
}
142+
let locale_str: String = locale_json_str.to_string();
139143

140-
let lang_id = if let Ok(lc) = locale_str.parse::<Locale>() {
144+
let locale_id = if let Ok(lc) = locale_str.parse::<Locale>() {
141145
lc
142146
} else {
143147
return Ok(json!({
@@ -146,7 +150,31 @@ pub fn run_relativedatetimeformat_test(json_obj: &Value) -> Result<Value, String
146150
"error_type": "locale problem",
147151
}));
148152
};
149-
let data_locale = DataLocale::from(lang_id);
153+
154+
let mut data_locale = DataLocale::from(locale_id);
155+
156+
if numbering_system_str.is_some() {
157+
let numbering_system: &String = numbering_system_str.as_ref().unwrap();
158+
159+
// Set up the numbering system in the locale.
160+
data_locale.set_unicode_ext(
161+
key!("nu"),
162+
unicode::Value::from_str(numbering_system).unwrap(),
163+
);
164+
165+
// TODO: update when ICU4X supports numbering systems.
166+
// Note that "actual_options" returns the expanded locale,
167+
// e.g., "actual_options":"DataLocale{en-US-u-nu-arab}"
168+
if numbering_system != "latn" {
169+
return Ok(json!({
170+
"error": "Number system not supported",
171+
"error_msg": numbering_system,
172+
"error_detail": format!("{data_locale:?}"),
173+
"label": label,
174+
"unsupported": "non-Latn numbering system",
175+
}));
176+
}
177+
}
150178

151179
let count_str: &str = json_obj["count"].as_str().unwrap();
152180
let count = count_str

0 commit comments

Comments
 (0)