1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//! Language Negotiation is a process in which locales from different
//! sources are filtered and sorted in an effort to produce the best
//! possible selection of them.
//!
//! There are multiple language negotiation strategies, most popular is
//! described in [RFC4647](https://www.ietf.org/rfc/rfc4647.txt).
//!
//! The algorithm is based on the BCP4647 3.3.2 Extended Filtering algorithm,
//! with several modifications.
//!
//! # Example:
//!
//! ```
//! use fluent_locale::negotiate_languages;
//! use fluent_locale::NegotiationStrategy;
//! use fluent_locale::convert_vec_str_to_langids_lossy;
//! use unic_langid::LanguageIdentifier;
//!
//! let requested = convert_vec_str_to_langids_lossy(&["pl", "fr", "en-US"]);
//! let available = convert_vec_str_to_langids_lossy(&["it", "de", "fr", "en-GB", "en_US"]);
//! let default: LanguageIdentifier = "en-US".parse().expect("Parsing langid failed.");
//!
//! let supported = negotiate_languages(
//!   &requested,
//!   &available,
//!   Some(&default),
//!   NegotiationStrategy::Filtering
//! );
//!
//! let expected = convert_vec_str_to_langids_lossy(&["fr", "en-US", "en-GB"]);
//! assert_eq!(supported,
//!            expected.iter().map(|t| t.as_ref()).collect::<Vec<&LanguageIdentifier>>());
//! ```
//!
//! # The exact algorithm is custom, and consists of a 6 level strategy:
//!
//! ### 1) Attempt to find an exact match for each requested locale in available locales.
//!
//! Example:
//!
//! ```text
//! // [requested] * [available] = [supported]
//!
//! ["en-US"] * ["en-US"] = ["en-US"]
//! ```
//!
//! ### 2) Attempt to match a requested locale to an available locale treated as a locale range.
//!
//! Example:
//!
//! ```text
//! // [requested] * [available] = [supported]
//!
//! ["en-US"] * ["en"] = ["en"]
//!               ^^
//!                |-- becomes "en-*-*-*"
//! ```
//!
//! ### 3) Maximize the requested locale to find the best match in available locales.
//!
//! This part uses ICU's likelySubtags or similar database.
//!
//! Example:
//!
//! ```text
//! // [requested] * [available] = [supported]
//!
//! ["en"] * ["en-GB", "en-US"] = ["en-US"]
//!   ^^       ^^^^^    ^^^^^
//!    |           |        |
//!    |           |----------- become "en-*-GB-*" and "en-*-US-*"
//!    |
//!    |-- ICU likelySubtags expands it to "en-Latn-US"
//! ```
//!
//! ### 4) Attempt to look up for a different variant of the same locale.
//!
//! Example:
//!
//! ```text
//! // [requested] * [available] = [supported]
//!
//! ["ja-JP-win"] * ["ja-JP-mac"] = ["ja-JP-mac"]
//!   ^^^^^^^^^       ^^^^^^^^^
//!           |               |-- become "ja-*-JP-mac"
//!           |
//!           |----------- replace variant with range: "ja-JP-*"
//! ```
//!
//! ### 5) Look up for a maximized version of the requested locale, stripped of the region code.
//!
//! Example:
//!
//! ```text
//! // [requested] * [available] = [supported]
//!
//! ["en-CA"] * ["en-ZA", "en-US"] = ["en-US", "en-ZA"]
//!   ^^^^^
//!       |       ^^^^^    ^^^^^
//!       |           |        |
//!       |           |----------- become "en-*-ZA-*" and "en-*-US-*"
//!       |
//!       |----------- strip region produces "en", then lookup likelySubtag: "en-Latn-US"
//! ```
//!
//!
//! ### 6) Attempt to look up for a different region of the same locale.
//!
//! Example:
//!
//! ```text
//! // [requested] * [available] = [supported]
//!
//! ["en-GB"] * ["en-AU"] = ["en-AU"]
//!   ^^^^^       ^^^^^
//!       |           |-- become "en-*-AU-*"
//!       |
//!       |----- replace region with range: "en-*"
//! ```
//!

use std::collections::HashMap;
use unic_langid::LanguageIdentifier;
mod likely_subtags;

#[derive(PartialEq, Debug, Clone, Copy)]
pub enum NegotiationStrategy {
    Filtering,
    Matching,
    Lookup,
}

pub fn filter_matches<
    'a,
    R: 'a + Into<LanguageIdentifier> + Clone,
    A: 'a + Into<LanguageIdentifier> + Clone,
>(
    requested: impl IntoIterator<Item = &'a R>,
    available: impl IntoIterator<Item = &'a A>,
    strategy: NegotiationStrategy,
) -> Vec<&'a A> {
    let mut supported_locales = vec![];

    let mut av_map: HashMap<LanguageIdentifier, &'a A> = HashMap::new();

    for av in available.into_iter() {
        av_map.insert(av.clone().into(), av);
    }

    let req_langids: Vec<LanguageIdentifier> =
        requested.into_iter().map(|a| a.clone().into()).collect();

    for req in req_langids {
        if req.get_language() == "und" {
            continue;
        }

        let mut match_found = false;

        // 1) Try to find a simple (case-insensitive) string match for the request.
        av_map.retain(|key, value| {
            if strategy != NegotiationStrategy::Filtering && match_found {
                return true;
            }

            if key.matches(&req, false, false) {
                match_found = true;
                supported_locales.push(*value);
                return false;
            }
            true
        });

        if match_found {
            match strategy {
                NegotiationStrategy::Filtering => {}
                NegotiationStrategy::Matching => continue,
                NegotiationStrategy::Lookup => break,
            }
        }

        match_found = false;

        // 2) Try to match against the available locales treated as ranges.
        av_map.retain(|key, value| {
            if strategy != NegotiationStrategy::Filtering && match_found {
                return true;
            }

            if key.matches(&req, true, false) {
                match_found = true;
                supported_locales.push(*value);
                return false;
            }
            true
        });

        if match_found {
            match strategy {
                NegotiationStrategy::Filtering => {}
                NegotiationStrategy::Matching => continue,
                NegotiationStrategy::Lookup => break,
            };
        }

        match_found = false;

        // 3) Try to match against a maximized version of the requested locale
        let mut req = if let Some(req) = likely_subtags::add(&req) {
            av_map.retain(|key, value| {
                if strategy != NegotiationStrategy::Filtering && match_found {
                    return true;
                }

                if key.matches(&req, true, false) {
                    match_found = true;
                    supported_locales.push(*value);
                    return false;
                }
                true
            });

            if match_found {
                match strategy {
                    NegotiationStrategy::Filtering => {}
                    NegotiationStrategy::Matching => continue,
                    NegotiationStrategy::Lookup => break,
                };
            }

            match_found = false;
            req
        } else {
            req
        };

        // 4) Try to match against a variant as a range
        req.set_variants(&[]).unwrap();
        av_map.retain(|key, value| {
            if strategy != NegotiationStrategy::Filtering && match_found {
                return true;
            }

            if key.matches(&req, true, true) {
                match_found = true;
                supported_locales.push(*value);
                return false;
            }
            true
        });

        if match_found {
            match strategy {
                NegotiationStrategy::Filtering => {}
                NegotiationStrategy::Matching => continue,
                NegotiationStrategy::Lookup => break,
            };
        }

        match_found = false;

        // 5) Try to match against the likely subtag without region
        req.set_region(None).unwrap();
        if let Some(req) = likely_subtags::add(&req) {
            av_map.retain(|key, value| {
                if strategy != NegotiationStrategy::Filtering && match_found {
                    return true;
                }

                if key.matches(&req, true, false) {
                    match_found = true;
                    supported_locales.push(*value);
                    return false;
                }
                true
            });

            if match_found {
                match strategy {
                    NegotiationStrategy::Filtering => {}
                    NegotiationStrategy::Matching => continue,
                    NegotiationStrategy::Lookup => break,
                };
            }

            match_found = false;
        }

        // 6) Try to match against a region as a range
        req.set_region(None).unwrap();
        av_map.retain(|key, value| {
            if strategy != NegotiationStrategy::Filtering && match_found {
                return true;
            }

            if key.matches(&req, true, true) {
                match_found = true;
                supported_locales.push(*value);
                return false;
            }
            true
        });

        if match_found {
            match strategy {
                NegotiationStrategy::Filtering => {}
                NegotiationStrategy::Matching => continue,
                NegotiationStrategy::Lookup => break,
            };
        }
    }

    supported_locales
}

pub fn negotiate_languages<
    'a,
    R: 'a + Into<LanguageIdentifier> + Clone,
    A: 'a + Into<LanguageIdentifier> + PartialEq + Clone,
>(
    requested: impl IntoIterator<Item = &'a R>,
    available: impl IntoIterator<Item = &'a A>,
    default: Option<&'a A>,
    strategy: NegotiationStrategy,
) -> Vec<&A> {
    let mut supported = filter_matches(requested, available, strategy);

    if let Some(default) = default {
        if strategy == NegotiationStrategy::Lookup {
            if supported.is_empty() {
                supported.push(default);
            }
        } else if !supported.contains(&default) {
            supported.push(default);
        }
    }
    supported
}