poly_l10n 0.0.7

Handle locali(s|z)ations the correct way
Documentation
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! `poly_l10n`: Handle locali(s|z)ations the correct way
//!
//! ## Intentions
//!
//! See <https://blog.fyralabs.com/advice-on-internationalization/#language-fallbacks>.
//!
//! In short, this crate handles language fallbacks and detect system languages *the correct way*.
//!
//! Get started by [`LocaleFallbackSolver`], [`system_want_langids()`] and [`langid!`].
//!
//! ## 📃 License
//!
//! `GPL-3.0-or-later`
//!
//!    Copyright (C) 2025  madonuko <mado@fyralabs.com> <madonuko@outlook.com>
//!
//!    This program is free software: you can redistribute it and/or modify
//!    it under the terms of the GNU General Public License as published by
//!    the Free Software Foundation, either version 3 of the License, or
//!    (at your option) any later version.
//!
//!    This program is distributed in the hope that it will be useful,
//!    but WITHOUT ANY WARRANTY; without even the implied warranty of
//!    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//!    GNU General Public License for more details.
//!
//!    You should have received a copy of the GNU General Public License
//!    along with this program.  If not, see <https://www.gnu.org/licenses/>.

mod default_rulebook;
#[cfg(feature = "getlang")]
pub mod getlang;
pub mod macros;
#[cfg(feature = "per_lang_default_rules")]
pub mod per_lang_default_rules;

use std::{rc::Rc, sync::Arc};

#[cfg(feature = "getlang")]
pub use getlang::system_want_langids;
use itertools::Itertools;
pub use unic_langid::{self, LanguageIdentifier};

/// Entry point of `poly_l10n`.
///
/// A solver requires a [`Rulebook`] or [`ARulebook`] to process and solve locales. The latter is
/// used by [`Self::default`].
///
/// # Examples
/// ```
/// let solver = poly_l10n::LocaleFallbackSolver::<poly_l10n::ARulebook>::default();
/// # #[cfg(feature = "per_lang_default_rules")]
/// assert_eq!(solver.solve_locale(poly_l10n::langid!("arb")), poly_l10n::langid!["arb", "ar-AE", "ara-AE", "arb-AE", "ar", "ara"]);
/// ```
#[derive(Clone, Copy, Debug, Default)]
pub struct LocaleFallbackSolver<R: for<'a> PolyL10nRulebook<'a> = ARulebook> {
    pub rulebook: R,
}

impl<R: for<'a> PolyL10nRulebook<'a>> LocaleFallbackSolver<R> {
    /// Find alternative fallbacks for the given `locale` as specified by the `rulebook`. This
    /// operation is recursive and expensive.
    ///
    /// ```
    /// let solver = poly_l10n::LocaleFallbackSolver::<poly_l10n::Rulebook>::default();
    /// # #[cfg(feature = "per_lang_default_rules")]
    /// assert_eq!(solver.solve_locale(poly_l10n::langid!("arb")), poly_l10n::langid!["arb", "ar-AE", "ara-AE", "arb-AE", "ar", "ara"]);
    /// ```
    pub fn solve_locale<L: AsRef<LanguageIdentifier>>(&self, locale: L) -> Vec<LanguageIdentifier> {
        use std::hash::{Hash, Hasher};
        let locale = locale.as_ref();
        let mut locales = self.rulebook.find_fallback_locale(locale).collect_vec();
        let h = |l: &LanguageIdentifier| {
            let mut hasher = std::hash::DefaultHasher::default();
            l.hash(&mut hasher);
            hasher.finish()
        };
        let mut locale_hashes = locales.iter().map(h).collect_vec();
        let mut old_len = 0;
        while old_len != locales.len() {
            #[allow(clippy::indexing_slicing)]
            let new_locales = locales[old_len..]
                .iter()
                .flat_map(|locale| {
                    self.rulebook.find_fallback_locale(locale).chain(
                        self.rulebook
                            .find_fallback_locale_ref(locale)
                            .map(Clone::clone),
                    )
                })
                .filter(|l| !locale_hashes.contains(&h(l)))
                .unique()
                .collect_vec();
            old_len = locales.len();
            locales.extend_from_slice(&new_locales);
            locale_hashes.extend(new_locales.iter().map(h));
        }
        locales.into_iter().unique().collect_vec()
    }
}

/// Rulebook trait.
///
/// A rulebook is a set of rules for [`LocaleFallbackSolver`]. The solver obtains the list of
/// fallback locales from the rules in the solver's rulebook.
///
/// The default rulebook is [`ARulebook`] and you may create a solver with it using:
///
/// ```
/// poly_l10n::LocaleFallbackSolver::<poly_l10n::ARulebook>::default()
/// # ;
/// ```
///
/// With that being said, a custom tailor-made rulebook is possible by implementing this trait for
/// a new struct.
///
/// # Implementation
/// Only one of [`PolyL10nRulebook::find_fallback_locale`] and
/// [`PolyL10nRulebook::find_fallback_locale_ref`] SHOULD be implemented. Note that for the latter,
/// [`LocaleFallbackSolver`] will clone the items in the returned iterator, so there are virtually
/// no performance difference between the two.
///
/// If both functions are implemented, the solver will [`Iterator::chain`] them together.
pub trait PolyL10nRulebook<'s> {
    fn find_fallback_locale(
        &self,
        _: &LanguageIdentifier,
    ) -> impl Iterator<Item = LanguageIdentifier> {
        std::iter::empty()
    }

    fn find_fallback_locale_ref(
        &'s self,
        _: &LanguageIdentifier,
    ) -> impl Iterator<Item = &'s LanguageIdentifier> {
        std::iter::empty()
    }
}

// NOTE: rust disallows multiple blanket impls, so unfortunately we need to choose one
/*
impl<'s, M> PolyL10nRulebook<'s> for M
where
    M: for<'a> std::ops::Index<&'a LanguageIdentifier, Output = LanguageIdentifier>,
{
    fn find_fallback_locale(
        &'s self,
        locale: &LanguageIdentifier,
    ) -> impl Iterator<Item = &'s LanguageIdentifier> {
        std::iter::once(&self[locale])
    }
}
*/

impl<'s, M, LS: 's> PolyL10nRulebook<'s> for M
where
    M: for<'a> std::ops::Index<&'a LanguageIdentifier, Output = LS>,
    &'s LS: IntoIterator<Item = &'s LanguageIdentifier>,
{
    fn find_fallback_locale_ref(
        &'s self,
        locale: &LanguageIdentifier,
    ) -> impl Iterator<Item = &'s LanguageIdentifier> {
        (&self[locale]).into_iter()
    }
}

pub type FnRules = Vec<Box<dyn Fn(&LanguageIdentifier) -> Vec<LanguageIdentifier>>>;

/// A set of rules that govern how [`LocaleFallbackSolver`] should handle fallbacks.
///
/// For the thread-safe version, see [`ARulebook<A>`].
///
/// [`Rulebook<A>`], regardless of type `A`, stores the rules as [`FnRules`], a vector of boxed
/// `dyn Fn(&LanguageIdentifier) -> Vec<LanguageIdentifier>`. Therefore, the actual correct name of
/// this struct should be something along the lines of `FnsRulebook`.
///
/// Obviously this rulebook can be used with the solver because it implements [`PolyL10nRulebook`].
///
/// In addition, the default rulebook [`Rulebook::default()`] can and probably should be used for
/// most situations you ever need to deal with.
pub struct Rulebook<A = ()> {
    pub rules: FnRules,
    pub owned_values: A,
}

impl<A: std::fmt::Debug> std::fmt::Debug for Rulebook<A> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Rulebook")
            .field("owned_values", &self.owned_values)
            .field("rules", &PseudoFnRules::from(&self.rules))
            .finish_non_exhaustive()
    }
}
/// Used for implementing [`Debug`] for [`Rulebook`].
struct PseudoFnRules {
    len: usize,
}
impl From<&FnRules> for PseudoFnRules {
    fn from(value: &FnRules) -> Self {
        Self { len: value.len() }
    }
}
impl std::fmt::Debug for PseudoFnRules {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FnRules")
            .field("len", &self.len)
            .finish_non_exhaustive()
    }
}

impl<A> PolyL10nRulebook<'_> for Rulebook<A> {
    fn find_fallback_locale(
        &self,
        locale: &LanguageIdentifier,
    ) -> impl Iterator<Item = LanguageIdentifier> {
        self.rules.iter().flat_map(|f| f(locale))
    }
}

impl Rulebook<Rc<Vec<Rulebook>>> {
    /// Combine multiple rulebooks into one.
    ///
    /// See also: [`Self::from_ref_rulebooks`].
    ///
    /// # Examples
    /// ```
    /// let rb1 = poly_l10n::Rulebook::from_fn(|l| {
    ///   let mut l = l.clone();
    ///   l.script = None;
    ///   vec![l]
    /// });
    /// let rb2 = poly_l10n::Rulebook::from_fn(|l| {
    ///   let mut l = l.clone();
    ///   l.region = None;
    ///   vec![l]
    /// });
    /// let rulebook = poly_l10n::Rulebook::from_rulebooks([rb1, rb2].into_iter());
    /// let solv = poly_l10n::LocaleFallbackSolver { rulebook };
    ///
    /// assert_eq!(
    ///   solv.solve_locale(poly_l10n::langid!["zh-Hant-HK"]),
    ///   poly_l10n::langid!["zh-HK", "zh-Hant", "zh"]
    /// );
    /// ```
    pub fn from_rulebooks<I: Iterator<Item = Rulebook>>(rulebooks: I) -> Self {
        let mut new = Self {
            owned_values: Rc::new(rulebooks.collect_vec()),
            rules: vec![],
        };
        let owned_values = Rc::clone(&new.owned_values);
        new.rules = vec![Box::new(move |l: &LanguageIdentifier| {
            owned_values
                .iter()
                .flat_map(|rulebook| rulebook.find_fallback_locale(l).collect_vec())
                .collect()
        })];
        new
    }
}
impl<RR, R> Rulebook<(Rc<Vec<RR>>, std::marker::PhantomData<R>)>
where
    RR: AsRef<Rulebook<R>> + 'static,
{
    /// Combine multiple rulebooks into one. Each given rulebook `r` must implement
    /// [`AsRef::as_ref`].
    ///
    /// For the owned version, see [`Self::from_rulebooks`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use std::rc::Rc;
    /// let rb1 = poly_l10n::Rulebook::from_fn(|l| {
    ///   let mut l = l.clone();
    ///   l.script = None;
    ///   vec![l]
    /// });
    /// let rb2 = poly_l10n::Rulebook::from_fn(|l| {
    ///   let mut l = l.clone();
    ///   l.region = None;
    ///   vec![l]
    /// });
    /// let (rb1, rb2) = (Rc::new(rb1), Rc::new(rb2));
    /// let rulebook = poly_l10n::Rulebook::from_ref_rulebooks([rb1, rb2].iter().cloned());
    /// let solv = poly_l10n::LocaleFallbackSolver { rulebook };
    ///
    /// assert_eq!(
    ///   solv.solve_locale(poly_l10n::langid!["zh-Hant-HK"]),
    ///   poly_l10n::langid!["zh-HK", "zh-Hant", "zh"]
    /// );
    /// ```
    pub fn from_ref_rulebooks<I: Iterator<Item = RR>>(rulebooks: I) -> Self {
        let mut new = Self {
            owned_values: (Rc::new(rulebooks.collect_vec()), std::marker::PhantomData),
            rules: vec![],
        };
        let owned_values = Rc::clone(&new.owned_values.0);
        new.rules = vec![Box::new(move |l: &LanguageIdentifier| {
            (owned_values.iter())
                .flat_map(|rulebook| rulebook.as_ref().find_fallback_locale(l).collect_vec())
                .collect()
        })];
        new
    }
}

impl Rulebook {
    #[must_use]
    pub fn from_fn<F: Fn(&LanguageIdentifier) -> Vec<LanguageIdentifier> + 'static>(f: F) -> Self {
        Self {
            rules: vec![Box::new(f)],
            owned_values: (),
        }
    }
    #[must_use]
    pub const fn from_fns(rules: FnRules) -> Self {
        Self {
            rules,
            owned_values: (),
        }
    }
    /// Convert a map (or anything that impl [`std::ops::Index<&LanguageIdentifier>`]) into
    /// a rulebook.
    ///
    /// The output of the map must implement [`IntoIterator<Item = &LanguageIdentifier>`].
    ///
    /// While any valid arguments to this constructor are guaranteed to satisfy the trait
    /// [`PolyL10nRulebook`], it could be useful to convert them to rulebooks, e.g. to combine
    /// multiple rulebooks using [`Self::from_rulebooks`].
    pub fn from_map<M, LS>(map: M) -> Self
    where
        M: for<'a> std::ops::Index<&'a LanguageIdentifier, Output = LS> + 'static,
        for<'b> &'b LS: IntoIterator<Item = &'b LanguageIdentifier>,
    {
        Self::from_fn(move |l| map[l].into_iter().cloned().collect())
    }
}

// TODO: rules?
impl Default for Rulebook {
    fn default() -> Self {
        Self::from_fn(default_rulebook::default_rulebook)
    }
}

pub type AFnRules = Vec<Box<dyn Fn(&LanguageIdentifier) -> Vec<LanguageIdentifier> + Send + Sync>>;

/// A set of rules that govern how [`LocaleFallbackSolver`] should handle fallbacks.
///
/// This is the thread-safe version of [`Rulebook`].
///
/// [`ARulebook<A>`], regardless of type `A`, stores the rules as [`AFnRules`], a vector of boxed
/// `dyn Fn(&LanguageIdentifier) -> Vec<LanguageIdentifier> + Send + Sync`. Therefore, the actual
/// correct name of this struct should be something along the lines of `AFnsRulebook`.
///
/// Obviously this rulebook can be used with the solver because it implements [`PolyL10nRulebook`].
///
/// In addition, the default rulebook [`ARulebook::default()`] can and probably should be used for
/// most situations you ever need to deal with.
pub struct ARulebook<A = ()> {
    pub rules: AFnRules,
    pub owned_values: A,
}

impl<A: std::fmt::Debug> std::fmt::Debug for ARulebook<A> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ARulebook")
            .field("owned_values", &self.owned_values)
            .field("rules", &APseudoFnRules::from(&self.rules))
            .finish_non_exhaustive()
    }
}
/// Used for implementing [`Debug`] for [`ARulebook`].
struct APseudoFnRules {
    len: usize,
}
impl From<&AFnRules> for APseudoFnRules {
    fn from(value: &AFnRules) -> Self {
        Self { len: value.len() }
    }
}
impl std::fmt::Debug for APseudoFnRules {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AFnRules")
            .field("len", &self.len)
            .finish_non_exhaustive()
    }
}

impl<A> PolyL10nRulebook<'_> for ARulebook<A> {
    fn find_fallback_locale(
        &self,
        locale: &LanguageIdentifier,
    ) -> impl Iterator<Item = LanguageIdentifier> {
        self.rules.iter().flat_map(|f| f(locale))
    }
}

impl ARulebook<Arc<Vec<ARulebook>>> {
    /// Combine multiple rulebooks into one.
    ///
    /// See also: [`Self::from_ref_rulebooks`].
    ///
    /// # Examples
    /// ```
    /// let rb1 = poly_l10n::ARulebook::from_fn(|l| {
    ///   let mut l = l.clone();
    ///   l.script = None;
    ///   vec![l]
    /// });
    /// let rb2 = poly_l10n::ARulebook::from_fn(|l| {
    ///   let mut l = l.clone();
    ///   l.region = None;
    ///   vec![l]
    /// });
    /// let rulebook = poly_l10n::ARulebook::from_rulebooks([rb1, rb2].into_iter());
    /// let solv = poly_l10n::LocaleFallbackSolver { rulebook };
    ///
    /// assert_eq!(
    ///   solv.solve_locale(poly_l10n::langid!["zh-Hant-HK"]),
    ///   poly_l10n::langid!["zh-HK", "zh-Hant", "zh"]
    /// );
    /// ```
    pub fn from_rulebooks<I: Iterator<Item = ARulebook>>(rulebooks: I) -> Self {
        let mut new = Self {
            owned_values: Arc::new(rulebooks.collect_vec()),
            rules: vec![],
        };
        let owned_values = Arc::clone(&new.owned_values);
        new.rules = vec![Box::new(move |l: &LanguageIdentifier| {
            owned_values
                .iter()
                .flat_map(|rulebook| rulebook.find_fallback_locale(l).collect_vec())
                .collect()
        })];
        new
    }
}
impl<RR, R> ARulebook<(Arc<Vec<RR>>, std::marker::PhantomData<R>)>
where
    RR: AsRef<ARulebook<R>> + 'static + Send + Sync,
{
    /// Combine multiple rulebooks into one. Each given rulebook `r` must implement
    /// [`AsRef::as_ref`].
    ///
    /// For the owned version, see [`Self::from_rulebooks`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use std::sync::Arc;
    /// let rb1 = poly_l10n::ARulebook::from_fn(|l| {
    ///   let mut l = l.clone();
    ///   l.script = None;
    ///   vec![l]
    /// });
    /// let rb2 = poly_l10n::ARulebook::from_fn(|l| {
    ///   let mut l = l.clone();
    ///   l.region = None;
    ///   vec![l]
    /// });
    /// let (rb1, rb2) = (Arc::new(rb1), Arc::new(rb2));
    /// let rulebook = poly_l10n::ARulebook::from_ref_rulebooks([rb1, rb2].iter().cloned());
    /// let solv = poly_l10n::LocaleFallbackSolver { rulebook };
    ///
    /// assert_eq!(
    ///   solv.solve_locale(poly_l10n::langid!["zh-Hant-HK"]),
    ///   poly_l10n::langid!["zh-HK", "zh-Hant", "zh"]
    /// );
    /// ```
    pub fn from_ref_rulebooks<I: Iterator<Item = RR>>(rulebooks: I) -> Self {
        let mut new = Self {
            owned_values: (Arc::new(rulebooks.collect_vec()), std::marker::PhantomData),
            rules: vec![],
        };
        let owned_values = Arc::clone(&new.owned_values.0);
        new.rules = vec![Box::new(move |l: &LanguageIdentifier| {
            (owned_values.iter())
                .flat_map(|rulebook| rulebook.as_ref().find_fallback_locale(l).collect_vec())
                .collect()
        })];
        new
    }
}

impl ARulebook {
    #[must_use]
    pub fn from_fn<
        F: Fn(&LanguageIdentifier) -> Vec<LanguageIdentifier> + 'static + Send + Sync,
    >(
        f: F,
    ) -> Self {
        Self {
            rules: vec![Box::new(f)],
            owned_values: (),
        }
    }
    #[must_use]
    pub const fn from_fns(rules: AFnRules) -> Self {
        Self {
            rules,
            owned_values: (),
        }
    }
    /// Convert a map (or anything that impl [`std::ops::Index<&LanguageIdentifier>`]) into
    /// a rulebook.
    ///
    /// The output of the map must implement [`IntoIterator<Item = &LanguageIdentifier>`].
    ///
    /// While any valid arguments to this constructor are guaranteed to satisfy the trait
    /// [`PolyL10nRulebook`], it could be useful to convert them to rulebooks, e.g. to combine
    /// multiple rulebooks using [`Self::from_rulebooks`].
    pub fn from_map<M, LS>(map: M) -> Self
    where
        M: for<'a> std::ops::Index<&'a LanguageIdentifier, Output = LS> + 'static + Send + Sync,
        for<'b> &'b LS: IntoIterator<Item = &'b LanguageIdentifier>,
    {
        Self::from_fn(move |l| map[l].into_iter().cloned().collect())
    }
}

// TODO: rules?
impl Default for ARulebook {
    fn default() -> Self {
        Self::from_fn(default_rulebook::default_rulebook)
    }
}