1414//! use fluent_langneg::negotiate_languages;
1515//! use fluent_langneg::NegotiationStrategy;
1616//! use fluent_langneg::convert_vec_str_to_langids_lossy;
17- //! use unic_langid ::LanguageIdentifier;
17+ //! use icu_locid ::LanguageIdentifier;
1818//!
1919//! let requested = convert_vec_str_to_langids_lossy(&["pl", "fr", "en-US"]);
2020//! let available = convert_vec_str_to_langids_lossy(&["it", "de", "fr", "en-GB", "en_US"]);
119119//! ```
120120//!
121121
122- use unic_langid :: LanguageIdentifier ;
122+ use icu_locid :: LanguageIdentifier ;
123123
124124#[ cfg( not( feature = "cldr" ) ) ]
125125mod likely_subtags;
@@ -133,6 +133,40 @@ pub enum NegotiationStrategy {
133133 Lookup ,
134134}
135135
136+ fn subtag_matches < P : PartialEq > (
137+ subtag1 : & Option < P > ,
138+ subtag2 : & Option < P > ,
139+ as_range1 : bool ,
140+ as_range2 : bool ,
141+ ) -> bool {
142+ ( as_range1 && subtag1. is_none ( ) ) || ( as_range2 && subtag2. is_none ( ) ) || subtag1 == subtag2
143+ }
144+
145+ fn matches (
146+ lid1 : & LanguageIdentifier ,
147+ lid2 : & LanguageIdentifier ,
148+ range1 : bool ,
149+ range2 : bool ,
150+ ) -> bool {
151+ let language = ( range1 && lid1. language . is_empty ( ) )
152+ || ( range2 && lid2. language . is_empty ( ) )
153+ || lid1. language == lid2. language ;
154+ if !language {
155+ return false ;
156+ }
157+ let script = subtag_matches ( & lid1. script , & lid2. script , range1, range2) ;
158+ if !script {
159+ return false ;
160+ }
161+ let region = subtag_matches ( & lid1. region , & lid2. region , range1, range2) ;
162+ if !region {
163+ return false ;
164+ }
165+ ( range1 && lid1. variants . is_empty ( ) )
166+ || ( range2 && lid2. variants . is_empty ( ) )
167+ || lid1. variants == lid2. variants
168+ }
169+
136170pub fn filter_matches < ' a , R : ' a + AsRef < LanguageIdentifier > , A : ' a + AsRef < LanguageIdentifier > > (
137171 requested : & [ R ] ,
138172 available : & ' a [ A ] ,
@@ -152,10 +186,7 @@ pub fn filter_matches<'a, R: 'a + AsRef<LanguageIdentifier>, A: 'a + AsRef<Langu
152186 return true ;
153187 }
154188
155- if locale
156- . as_ref( )
157- . matches( & req, $self_as_range, $other_as_range)
158- {
189+ if matches( locale. as_ref( ) , & req, $self_as_range, $other_as_range) {
159190 match_found = true ;
160191 supported_locales. push( * locale) ;
161192 return false ;
@@ -191,7 +222,7 @@ pub fn filter_matches<'a, R: 'a + AsRef<LanguageIdentifier>, A: 'a + AsRef<Langu
191222 }
192223
193224 // 4) Try to match against a variant as a range
194- req. clear_variants ( ) ;
225+ req. variants . clear ( ) ;
195226 test_strategy ! ( true , true ) ;
196227
197228 // 5) Try to match against the likely subtag without region
0 commit comments