1
1
// 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
2
3
3
4
use fixed_decimal:: FixedDecimal ;
5
+
6
+ use icu:: locid:: extensions:: unicode;
7
+ use icu:: locid:: extensions:: unicode:: key;
4
8
use icu:: locid:: Locale ;
9
+
10
+ use std:: str:: FromStr ;
11
+
5
12
use icu_provider:: DataLocale ;
6
13
7
14
use icu:: relativetime:: options:: Numeric ;
@@ -128,16 +135,13 @@ pub fn run_relativedatetimeformat_test(json_obj: &Value) -> Result<Value, String
128
135
let option_struct: RelativeDateTimeFormatterOptions =
129
136
serde_json:: from_str ( & options. to_string ( ) ) . unwrap ( ) ;
130
137
131
- let numbering_system_str = & option_struct. numbering_system ;
138
+ let numbering_system_str: & Option < String > = & option_struct. numbering_system ;
132
139
140
+ // Set up the locale with its options.
133
141
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 ( ) ;
139
143
140
- let lang_id = if let Ok ( lc) = locale_str. parse :: < Locale > ( ) {
144
+ let locale_id = if let Ok ( lc) = locale_str. parse :: < Locale > ( ) {
141
145
lc
142
146
} else {
143
147
return Ok ( json ! ( {
@@ -146,7 +150,31 @@ pub fn run_relativedatetimeformat_test(json_obj: &Value) -> Result<Value, String
146
150
"error_type" : "locale problem" ,
147
151
} ) ) ;
148
152
} ;
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
+ }
150
178
151
179
let count_str: & str = json_obj[ "count" ] . as_str ( ) . unwrap ( ) ;
152
180
let count = count_str
0 commit comments