zng_ext_l10n/
types.rs

1use std::{borrow::Cow, collections::HashMap, fmt, mem, ops, path::PathBuf, sync::Arc};
2
3use fluent::types::FluentNumber;
4use once_cell::sync::Lazy;
5use semver::Version;
6use zng_ext_fs_watcher::WatcherReadStatus;
7use zng_layout::context::LayoutDirection;
8use zng_txt::{ToTxt, Txt};
9use zng_var::{ArcEq, IntoVar, Var, VarValue, const_var, context_var, impl_from_and_into_var};
10
11use crate::{L10N, lang, service::L10N_SV};
12
13/// Handle to multiple localization resources.
14#[derive(Clone, Debug)]
15pub struct LangResources(pub Vec<LangResource>);
16impl ops::Deref for LangResources {
17    type Target = Vec<LangResource>;
18
19    fn deref(&self) -> &Self::Target {
20        &self.0
21    }
22}
23impl ops::DerefMut for LangResources {
24    fn deref_mut(&mut self) -> &mut Self::Target {
25        &mut self.0
26    }
27}
28impl LangResources {
29    /// Wait for all the resources to load.
30    pub async fn wait(&self) {
31        for res in &self.0 {
32            res.wait().await;
33        }
34    }
35
36    /// Drop all handles without dropping the resource.
37    pub fn perm(self) {
38        for res in self.0 {
39            res.perm()
40        }
41    }
42}
43
44/// Handle to a localization resource.
45#[derive(Clone)]
46#[must_use = "resource can unload if dropped"]
47pub struct LangResource {
48    pub(super) res: Var<Option<ArcEq<fluent::FluentResource>>>,
49    pub(super) status: Var<LangResourceStatus>,
50}
51
52impl fmt::Debug for LangResource {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        f.debug_struct("LangResource")
55            .field("status", &self.status.get())
56            .finish_non_exhaustive()
57    }
58}
59impl LangResource {
60    /// Read-only variable with the resource.
61    pub fn resource(&self) -> &Var<Option<ArcEq<fluent::FluentResource>>> {
62        &self.res
63    }
64
65    /// Read-only variable with the resource status.
66    pub fn status(&self) -> &Var<LangResourceStatus> {
67        &self.status
68    }
69
70    /// Drop the handle without unloading the resource.
71    pub fn perm(self) {
72        L10N_SV.write().push_perm_resource(self);
73    }
74
75    /// Await resource status to not be loading.
76    pub async fn wait(&self) {
77        while matches!(self.status.get(), LangResourceStatus::Loading) {
78            self.status.wait_update().await;
79        }
80    }
81}
82
83/// Status of a localization resource.
84#[derive(Clone, Debug)]
85pub enum LangResourceStatus {
86    /// Resource not available.
87    ///
88    /// This can change if the localization directory changes, or the file is created.
89    NotAvailable,
90    /// Resource is loading.
91    Loading,
92    /// Resource loaded ok.
93    Loaded,
94    /// Resource failed to load.
95    ///
96    /// This can be any IO or parse errors. If the resource if *not found* the status is set to
97    /// `NotAvailable`, not an error. Localization messages fallback on error just like they do
98    /// for not available.
99    Errors(StatusError),
100}
101impl fmt::Display for LangResourceStatus {
102    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
103        match self {
104            LangResourceStatus::NotAvailable => write!(f, "not available"),
105            LangResourceStatus::Loading => write!(f, "loading…"),
106            LangResourceStatus::Loaded => write!(f, "loaded"),
107            LangResourceStatus::Errors(e) => {
108                writeln!(f, "errors:")?;
109                for e in e {
110                    writeln!(f, "   {e}")?;
111                }
112                Ok(())
113            }
114        }
115    }
116}
117impl PartialEq for LangResourceStatus {
118    fn eq(&self, other: &Self) -> bool {
119        match (self, other) {
120            (Self::Errors(a), Self::Errors(b)) => a.is_empty() && b.is_empty(),
121            _ => core::mem::discriminant(self) == core::mem::discriminant(other),
122        }
123    }
124}
125impl Eq for LangResourceStatus {}
126impl WatcherReadStatus<StatusError> for LangResourceStatus {
127    fn idle() -> Self {
128        Self::Loaded
129    }
130
131    fn reading() -> Self {
132        Self::Loading
133    }
134
135    fn read_error(e: StatusError) -> Self {
136        Self::Errors(e)
137    }
138}
139impl WatcherReadStatus<LangResourceStatus> for LangResourceStatus {
140    fn idle() -> Self {
141        Self::Loaded
142    }
143
144    fn reading() -> Self {
145        Self::Loading
146    }
147
148    fn read_error(e: LangResourceStatus) -> Self {
149        e
150    }
151}
152
153type StatusError = Vec<Arc<dyn std::error::Error + Send + Sync>>;
154
155/// Localized message variable builder.
156///
157/// See [`L10N.message`] for more details.
158///
159/// [`L10N.message`]: L10N::message
160pub struct L10nMessageBuilder {
161    pub(super) file: LangFilePath,
162    pub(super) id: Txt,
163    pub(super) attribute: Txt,
164    pub(super) fallback: Txt,
165    pub(super) args: Vec<(Txt, Var<L10nArgument>)>,
166}
167impl L10nMessageBuilder {
168    /// Add a format arg variable.
169    pub fn arg(mut self, name: Txt, value: impl IntoVar<L10nArgument>) -> Self {
170        self.args.push((name, value.into_var()));
171        self
172    }
173    #[doc(hidden)]
174    pub fn l10n_arg(self, name: &'static str, value: Var<L10nArgument>) -> Self {
175        self.arg(Txt::from_static(name), value)
176    }
177
178    /// Build the message var for the given languages.
179    pub fn build_for(self, lang: impl Into<Langs>) -> Var<Txt> {
180        L10N_SV
181            .write()
182            .localized_message(lang.into(), self.file, self.id, self.attribute, self.fallback, self.args)
183    }
184
185    /// Build the message var for the contextual language.
186    pub fn build(self) -> Var<Txt> {
187        let Self {
188            file,
189            id,
190            attribute,
191            fallback,
192            args,
193        } = self;
194        LANG_VAR.flat_map(move |l| {
195            L10N_SV.write().localized_message(
196                l.clone(),
197                file.clone(),
198                id.clone(),
199                attribute.clone(),
200                fallback.clone(),
201                args.clone(),
202            )
203        })
204    }
205}
206
207/// Represents an argument value for a localization message.
208///
209/// See [`L10nMessageBuilder::arg`] for more details.
210#[derive(Clone, Debug, PartialEq)]
211pub enum L10nArgument {
212    /// String.
213    Txt(Txt),
214    /// Number, with optional style details.
215    Number(FluentNumber),
216}
217impl_from_and_into_var! {
218    fn from(txt: Txt) -> L10nArgument {
219        L10nArgument::Txt(txt)
220    }
221    fn from(txt: &'static str) -> L10nArgument {
222        L10nArgument::Txt(Txt::from_static(txt))
223    }
224    fn from(txt: String) -> L10nArgument {
225        L10nArgument::Txt(Txt::from(txt))
226    }
227    fn from(t: char) -> L10nArgument {
228        L10nArgument::Txt(Txt::from_char(t))
229    }
230    fn from(number: FluentNumber) -> L10nArgument {
231        L10nArgument::Number(number)
232    }
233    fn from(b: bool) -> L10nArgument {
234        b.to_txt().into()
235    }
236}
237macro_rules! impl_from_and_into_var_number {
238    ($($literal:tt),+) => {
239        impl_from_and_into_var! {
240            $(
241                fn from(number: $literal) -> L10nArgument {
242                    FluentNumber::from(number).into()
243                }
244            )+
245        }
246    }
247}
248impl_from_and_into_var_number! { u8, i8, u16, i16, u32, i32, u64, i64, u128, i128, usize, isize, f32, f64 }
249impl L10nArgument {
250    /// Borrow argument as a fluent value.
251    pub fn fluent_value(&self) -> fluent::FluentValue<'_> {
252        match self {
253            L10nArgument::Txt(t) => fluent::FluentValue::String(Cow::Borrowed(t.as_str())),
254            L10nArgument::Number(n) => fluent::FluentValue::Number(n.clone()),
255        }
256    }
257    /// Clone argument as a fluent value.
258    pub fn to_fluent_value(&self) -> fluent::FluentValue<'static> {
259        match self {
260            L10nArgument::Txt(t) => fluent::FluentValue::String(Cow::Owned(t.to_string())),
261            L10nArgument::Number(n) => fluent::FluentValue::Number(n.clone()),
262        }
263    }
264}
265
266#[doc(hidden)]
267pub struct L10nSpecialize<T>(pub Option<T>);
268#[doc(hidden)]
269pub trait IntoL10nVar {
270    fn to_l10n_var(&mut self) -> Var<L10nArgument>;
271}
272
273impl<T: Into<L10nArgument>> IntoL10nVar for L10nSpecialize<T> {
274    fn to_l10n_var(&mut self) -> Var<L10nArgument> {
275        const_var(self.0.take().unwrap().into())
276    }
277}
278impl<T: VarValue + Into<L10nArgument>> IntoL10nVar for &mut L10nSpecialize<Var<T>> {
279    fn to_l10n_var(&mut self) -> Var<L10nArgument> {
280        self.0.take().unwrap().map_into()
281    }
282}
283impl IntoL10nVar for &mut &mut L10nSpecialize<Var<L10nArgument>> {
284    fn to_l10n_var(&mut self) -> Var<L10nArgument> {
285        self.0.take().unwrap()
286    }
287}
288
289context_var! {
290    /// Language of text in a widget context.
291    ///
292    /// Is [`L10N.app_lang`] by default.
293    ///
294    /// [`L10N.app_lang`]: L10N::app_lang
295    pub static LANG_VAR: Langs = L10N.app_lang();
296}
297
298/// Identifies the language, region and script of text.
299///
300/// Use the [`lang!`] macro to construct one, it does compile-time validation.
301///
302/// Use the [`unic_langid`] crate for more advanced operations such as runtime parsing and editing identifiers, this
303/// type is just an alias for the core struct of that crate.
304///
305/// [`unic_langid`]: https://docs.rs/unic-langid
306#[derive(PartialEq, Eq, Hash, Clone, Default, PartialOrd, Ord, serde::Serialize, serde::Deserialize)]
307#[serde(transparent)]
308pub struct Lang(pub unic_langid::LanguageIdentifier);
309impl Lang {
310    /// Returns character direction of the language.
311    pub fn direction(&self) -> LayoutDirection {
312        if self == &lang!("ms") {
313            // Malay should default to "ms-Latn" that LTR, but unic_langid is matching "ms-Arab"
314            return LayoutDirection::LTR;
315        }
316        if self == &lang!("uz") {
317            // Uzbek should default to "uz-Latn"
318            return LayoutDirection::LTR;
319        }
320        crate::from_unic_char_direction(self.0.character_direction())
321    }
322
323    /// Compares a language to another allowing for either side to use the missing fields as wildcards.
324    ///
325    /// This allows for matching between `en` (treated as `en-*-*-*`) and `en-US`.
326    ///
327    /// # Examples
328    ///
329    /// ```
330    /// # use zng_ext_l10n::*;
331    /// #
332    /// let l1 = lang!("en");
333    /// let l2 = lang!("en-US");
334    ///
335    /// assert_ne!(l1, l2); // "en" != "en-US"
336    /// assert!(!l1.matches(&l2, false, false)); // "en" != "en-US"
337    ///
338    /// assert!(l1.matches(&l2, true, false)); // "en-*-*-*" == "en-US"
339    /// assert!(!l1.matches(&l2, false, true)); // "en" != "en-*-US-*"
340    /// assert!(l1.matches(&l2, true, true)); // "en-*-*-*" == "en-*-US-*"
341    /// ```
342    ///
343    /// Note that `machine` variant is ignored when matching.
344    ///
345    /// ```
346    /// # use zng_ext_l10n::*;
347    ///
348    /// let pt_br = lang!("pt-BR");
349    /// let pt_pt = lang!("pt-PT");
350    /// let pt = lang!("pt");
351    /// let pt_m = lang!("pt-machine");
352    ///
353    /// assert!(!pt_br.matches(&pt_pt, true, false)); // "pt-BR-*-*" != "pt-PT"
354    /// assert!(pt.matches(&pt_pt, true, false)); // "pt-*-*-*" == "pt-PT"
355    /// assert!(pt_m.matches(&pt_pt, true, false)); // "pt-machine-*-*" == "pt-PT"
356    ///
357    /// assert!(pt_m.matches(&pt, false, false)); // "pt-machine" == "pt"
358    /// assert_ne!(pt_m, pt); // but "pt-machine" != "pt" in `PartialEq`
359    /// ```
360    pub fn matches(&self, other: &Self, self_as_range: bool, other_as_range: bool) -> bool {
361        if !self.0.language.matches(other.0.language, self_as_range, other_as_range) {
362            return false;
363        }
364
365        fn tag_matches<P: PartialEq>(tag1: &Option<P>, tag2: &Option<P>, as_range1: bool, as_range2: bool) -> bool {
366            (as_range1 && tag1.is_none()) || (as_range2 && tag2.is_none()) || tag1 == tag2
367        }
368        if !tag_matches(&self.0.script, &other.0.script, self_as_range, other_as_range)
369            || !tag_matches(&self.region, &other.region, self_as_range, other_as_range)
370        {
371            return false;
372        }
373
374        (self_as_range && self.variants_no_machine().next().is_none())
375            || (other_as_range && other.variants_no_machine().next().is_none())
376            || self.variants_no_machine().eq(other.variants_no_machine())
377    }
378    fn variants_no_machine(&self) -> impl Iterator<Item = &unic_langid::subtags::Variant> {
379        self.0.variants().filter(|v| v.as_str() != "machine")
380    }
381
382    /// If the language has a `machine` variant.
383    ///
384    /// Machine translated resources are expected to be of lesser quality, but better than nothing.
385    /// This variant indicates that the resource should be given lower priority. If a machine generated
386    /// resource if reviewed by a human translator the `machine` variant must be removed.
387    pub fn is_machine_translation(&self) -> bool {
388        self.0.variants().any(|v| v == "machine")
389    }
390
391    /// Returns the name of the language and region in own language.
392    ///
393    /// The names are in localized title case, when the name is the same in different scripts the script name
394    /// is part of the language name (currently Hans/Hant).
395    ///
396    /// Returns `None` if the locale is not one of the 90 most common locales embedded. Language must match,
397    /// script must match exactly (omitted different from explicit), region is optional (if not matched returns only language name),
398    /// variants are ignored.
399    #[cfg(feature = "lang_autonym")]
400    pub fn autonym(&self) -> Option<LangAutonym> {
401        let lang = self.0.language.as_str();
402        let script = self.0.script.as_ref().map(|s| s.as_str()).unwrap_or("");
403        let region = self.0.region.as_ref().map(|r| r.as_str()).unwrap_or("");
404        let (name, region) = match (lang, script, region) {
405            // generated with cargo run --manifest-path tools/lang-autonym-gen/Cargo.toml
406            // please implement any manual corrections in the tool, not here
407            ("af", "", "") => ("Afrikaans", ""),
408            ("am", "", "") => ("አማርኛ", ""),
409            ("ar", "", "") => ("العربية", ""),
410            ("as", "", "") => ("অসমীয়া", ""),
411            ("az", "", "") => ("Azərbaycan", ""),
412            ("be", "", "") => ("Беларуская", ""),
413            ("bg", "", "") => ("Български", ""),
414            ("bn", "", "") => ("বাংলা", ""),
415            ("bs", "", "") => ("Bosanski", ""),
416            ("ca", "", "") => ("Català", ""),
417            ("cs", "", "") => ("Čeština", ""),
418            ("cy", "", "") => ("Cymraeg", ""),
419            ("da", "", "") => ("Dansk", ""),
420            ("de", "", "") => ("Deutsch", ""),
421            ("el", "", "") => ("Ελληνικά", ""),
422            ("en", "", "") => ("English", ""),
423            ("en", "", "GB") => ("English", "United Kingdom"),
424            ("en", "", "US") => ("English", "United States"),
425            ("es", "", "") => ("Español", ""),
426            ("es", "", "419") => ("Español", "Latinoamérica"),
427            ("es", "", "ES") => ("Español", "España"),
428            ("et", "", "") => ("Eesti", ""),
429            ("eu", "", "") => ("Euskara", ""),
430            ("fa", "", "") => ("فارسی", ""),
431            ("fi", "", "") => ("Suomi", ""),
432            ("fil", "", "") => ("Filipino", ""),
433            ("fr", "", "") => ("Français", ""),
434            ("fr", "", "FR") => ("Français", "France"),
435            ("fr", "", "CA") => ("Français", "Canada"),
436            ("ga", "", "") => ("Gaeilge", ""),
437            ("gd", "", "") => ("Gàidhlig", ""),
438            ("gl", "", "") => ("Galego", ""),
439            ("gu", "", "") => ("ગુજરાતી", ""),
440            ("he", "", "") => ("עברית", ""),
441            ("hi", "", "") => ("हिन्दी", ""),
442            ("hr", "", "") => ("Hrvatski", ""),
443            ("hu", "", "") => ("Magyar", ""),
444            ("hy", "", "") => ("Հայերեն", ""),
445            ("id", "", "") => ("Indonesia", ""),
446            ("is", "", "") => ("Íslenska", ""),
447            ("it", "", "") => ("Italiano", ""),
448            ("ja", "", "") => ("日本語", ""),
449            ("ka", "", "") => ("ქართული", ""),
450            ("kk", "", "") => ("Қазақ тілі", ""),
451            ("km", "", "") => ("ខ្មែរ", ""),
452            ("kn", "", "") => ("ಕನ್ನಡ", ""),
453            ("ko", "", "") => ("한국어", ""),
454            ("ky", "", "") => ("Кыргызча", ""),
455            ("lo", "", "") => ("ລາວ", ""),
456            ("lt", "", "") => ("Lietuvių", ""),
457            ("lv", "", "") => ("Latviešu", ""),
458            ("mk", "", "") => ("Македонски", ""),
459            ("ml", "", "") => ("മലയാളം", ""),
460            ("mn", "", "") => ("Монгол", ""),
461            ("mr", "", "") => ("मराठी", ""),
462            ("ms", "", "") => ("Melayu", ""),
463            ("my", "", "") => ("မြန်မာ", ""),
464            ("nb", "", "") => ("Norsk bokmål", ""),
465            ("ne", "", "") => ("नेपाली", ""),
466            ("nl", "", "") => ("Nederlands", ""),
467            ("nn", "", "") => ("Norsk nynorsk", ""),
468            ("or", "", "") => ("ଓଡ଼ିଆ", ""),
469            ("pa", "", "") => ("ਪੰਜਾਬੀ", ""),
470            ("pl", "", "") => ("Polski", ""),
471            ("ps", "", "") => ("پښتو", ""),
472            ("pt", "", "") => ("Português", ""),
473            ("pt", "", "BR") => ("Português", "Brasil"),
474            ("pt", "", "PT") => ("Português", "Portugal"),
475            ("ro", "", "") => ("Română", ""),
476            ("ru", "", "") => ("Русский", ""),
477            ("si", "", "") => ("සිංහල", ""),
478            ("sk", "", "") => ("Slovenčina", ""),
479            ("sl", "", "") => ("Slovenščina", ""),
480            ("sq", "", "") => ("Shqip", ""),
481            ("sr", "", "") => ("Српски", ""),
482            ("sr", "Latn", "") => ("Srpski", ""),
483            ("sv", "", "") => ("Svenska", ""),
484            ("sw", "", "") => ("Kiswahili", ""),
485            ("ta", "", "") => ("தமிழ்", ""),
486            ("te", "", "") => ("తెలుగు", ""),
487            ("th", "", "") => ("ไทย", ""),
488            ("tr", "", "") => ("Türkçe", ""),
489            ("uk", "", "") => ("Українська", ""),
490            ("ur", "", "") => ("اردو", ""),
491            ("uz", "", "") => ("O‘zbek", ""),
492            ("vi", "", "") => ("Tiếng việt", ""),
493            ("zh", "", "") => ("中文", ""),
494            ("zh", "Hans", "") => ("简体中文", ""),
495            ("zh", "Hant", "") => ("繁體中文", ""),
496            ("zh", "", "TW") => ("繁體中文", "台灣"),
497            ("zu", "", "") => ("Isizulu", ""),
498            ("pseudo", "", "") => ("Ƥşeuḓo", ""),
499            ("pseudo", "Mirr", "") => ("Ԁsǝnpo-Wıɹɹoɹǝp", ""),
500            ("pseudo", "Wide", "") => ("Ƥşeeuuḓoo-Ẇiḓee", ""),
501            _ => {
502                if self.0.region.is_some() {
503                    let q = Lang(unic_langid::LanguageIdentifier::from_parts(
504                        self.0.language,
505                        self.0.script,
506                        None,
507                        &[],
508                    ));
509                    return q.autonym();
510                } else {
511                    return None;
512                }
513            }
514        };
515        Some(LangAutonym {
516            language: name,
517            region: if region.is_empty() { None } else { Some(region) },
518        })
519    }
520
521    /// Get ordering of this and `other` as [`fmt::Display`] text.
522    ///
523    /// This method avoids actually allocating a string.
524    pub fn cmp_display(&self, other: &Self) -> std::cmp::Ordering {
525        let (name, region) = self.name_region_str();
526        let (other_name, other_region) = other.name_region_str();
527        name.cmp(other_name).then_with(|| region.cmp(&other_region))
528    }
529
530    fn name_region_str(&self) -> (&str, Option<&str>) {
531        #[cfg(feature = "lang_autonym")]
532        if let Some(a) = self.autonym() {
533            let region = a.region.or_else(|| self.0.region.as_ref().map(|r| r.as_str()));
534            return (a.language, region);
535        }
536        (self.0.language.as_str(), self.0.region.as_ref().map(|r| r.as_str()))
537    }
538}
539impl ops::Deref for Lang {
540    type Target = unic_langid::LanguageIdentifier;
541
542    fn deref(&self) -> &Self::Target {
543        &self.0
544    }
545}
546impl fmt::Debug for Lang {
547    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
548        write!(f, "{}", self.0)
549    }
550}
551/// Prints the autonym if built with `"lang_autonym"` and has one, otherwise prints the tags.
552///
553/// `{}` prints language and region (if set), `{:#}` only prints language.
554impl fmt::Display for Lang {
555    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
556        let (name, region) = self.name_region_str();
557        if f.alternate()
558            && let Some(r) = region
559        {
560            write!(f, "{name} ({r})")
561        } else {
562            write!(f, "{name}")
563        }
564    }
565}
566impl std::str::FromStr for Lang {
567    type Err = unic_langid::LanguageIdentifierError;
568
569    fn from_str(s: &str) -> Result<Self, Self::Err> {
570        let s = s.trim();
571        if s.is_empty() {
572            return Ok(lang!(und));
573        }
574        unic_langid::LanguageIdentifier::from_str(s).map(Lang)
575    }
576}
577
578/// Represents the translated name of a language and region in own language, using the selected script.
579#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
580#[cfg(feature = "lang_autonym")]
581pub struct LangAutonym {
582    /// Language name in its own language.
583    pub language: &'static str,
584    /// Region name in the language.
585    ///
586    /// Can be `None` when a region was requested and only the language name was available.
587    pub region: Option<&'static str>,
588}
589#[cfg(feature = "lang_autonym")]
590impl fmt::Debug for LangAutonym {
591    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
592        if f.alternate() {
593            f.debug_struct("LangAutonym")
594                .field("language", &self.language)
595                .field("region", &self.region)
596                .finish()
597        } else {
598            write!(f, "{self}")
599        }
600    }
601}
602/// `{}` prints language and region (if set), `{:#}` only prints language.
603#[cfg(feature = "lang_autonym")]
604impl fmt::Display for LangAutonym {
605    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
606        write!(f, "{}", self.language)?;
607        if let Some(r) = self.region
608            && !f.alternate()
609        {
610            write!(f, " ({r})")?;
611        }
612        Ok(())
613    }
614}
615
616/// List of languages, in priority order.
617#[derive(Clone, PartialEq, Eq, Default, Hash, serde::Serialize, serde::Deserialize)]
618#[serde(transparent)]
619pub struct Langs(pub Vec<Lang>);
620impl fmt::Debug for Langs {
621    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
622        struct DisplayLangs<'a>(&'a [Lang]);
623        impl fmt::Debug for DisplayLangs<'_> {
624            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
625                f.debug_list().entries(self.0.iter()).finish()
626            }
627        }
628        if f.alternate() {
629            f.debug_tuple("Langs").field(&DisplayLangs(&self.0)).finish()
630        } else {
631            fmt::Debug::fmt(&DisplayLangs(&self.0), f)
632        }
633    }
634}
635impl Langs {
636    /// The first lang on the list or `und` if the list is empty.
637    pub fn best(&self) -> &Lang {
638        static NONE: Lazy<Lang> = Lazy::new(|| lang!(und));
639        self.first().unwrap_or(&NONE)
640    }
641}
642impl ops::Deref for Langs {
643    type Target = Vec<Lang>;
644
645    fn deref(&self) -> &Self::Target {
646        &self.0
647    }
648}
649impl ops::DerefMut for Langs {
650    fn deref_mut(&mut self) -> &mut Self::Target {
651        &mut self.0
652    }
653}
654impl_from_and_into_var! {
655    fn from(lang: Lang) -> Langs {
656        Langs(vec![lang])
657    }
658    fn from(lang: Option<Lang>) -> Langs {
659        Langs(lang.into_iter().collect())
660    }
661}
662impl fmt::Display for Langs {
663    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
664        let mut sep = "";
665        for l in self.iter() {
666            write!(f, "{sep}{l}")?;
667            sep = ", ";
668        }
669        Ok(())
670    }
671}
672impl std::str::FromStr for Langs {
673    type Err = unic_langid::LanguageIdentifierError;
674
675    fn from_str(s: &str) -> Result<Self, Self::Err> {
676        if s.trim().is_empty() {
677            return Ok(Langs(vec![]));
678        }
679        let mut r = Self(vec![]);
680        for lang in s.split(',') {
681            r.0.push(lang.trim().parse()?)
682        }
683        Ok(r)
684    }
685}
686
687/// Represents a map of [`Lang`] keys that can be partially matched.
688#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
689#[serde(transparent)]
690pub struct LangMap<V> {
691    inner: Vec<(Lang, V)>,
692}
693impl<V> Default for LangMap<V> {
694    fn default() -> Self {
695        Self { inner: Default::default() }
696    }
697}
698impl<V> LangMap<V> {
699    /// New empty default.
700    pub fn new() -> Self {
701        LangMap::default()
702    }
703
704    /// New empty with pre-allocated capacity.
705    pub fn with_capacity(capacity: usize) -> Self {
706        LangMap {
707            inner: Vec::with_capacity(capacity),
708        }
709    }
710
711    fn exact_i(&self, lang: &Lang) -> Option<usize> {
712        for (i, (key, _)) in self.inner.iter().enumerate() {
713            if key == lang {
714                return Some(i);
715            }
716        }
717        None
718    }
719
720    fn best_i(&self, lang: &Lang) -> Option<usize> {
721        let mut best = None;
722        let mut best_weight = 0;
723
724        for (i, (key, _)) in self.inner.iter().enumerate() {
725            if lang.matches(key, true, true) {
726                let mut weight = 1;
727                let mut eq = 0;
728
729                // must match language
730                if key.language == lang.language {
731                    weight += 200;
732                    eq += 1;
733                }
734                // must match script
735                if key.script == lang.script {
736                    weight += 100;
737                    eq += 1;
738                }
739                // can fallback to different region
740                if key.region == lang.region {
741                    weight += 30;
742                    eq += 1;
743                }
744
745                if eq == 3 && lang.variants().eq(key.variants()) {
746                    // exact match
747                    return Some(i);
748                }
749
750                // human translated of different regions has priority over machine translated
751                if !key.is_machine_translation() {
752                    weight += 60;
753                }
754
755                if best_weight < weight {
756                    best_weight = weight;
757                    best = Some(i);
758                }
759            }
760        }
761
762        best
763    }
764
765    /// Returns the best match to `lang` currently in the map.
766    pub fn best_match(&self, lang: &Lang) -> Option<&Lang> {
767        if let Some(i) = self.best_i(lang) {
768            Some(&self.inner[i].0)
769        } else {
770            None
771        }
772    }
773
774    /// Returns the best match for `lang`.
775    pub fn get(&self, lang: &Lang) -> Option<&V> {
776        if let Some(i) = self.best_i(lang) {
777            Some(&self.inner[i].1)
778        } else {
779            None
780        }
781    }
782
783    /// Returns the exact match for `lang`.
784    pub fn get_exact(&self, lang: &Lang) -> Option<&V> {
785        if let Some(i) = self.exact_i(lang) {
786            Some(&self.inner[i].1)
787        } else {
788            None
789        }
790    }
791
792    /// Returns the best match for `lang`.
793    pub fn get_mut(&mut self, lang: &Lang) -> Option<&mut V> {
794        if let Some(i) = self.best_i(lang) {
795            Some(&mut self.inner[i].1)
796        } else {
797            None
798        }
799    }
800
801    /// Returns the exact match for `lang`.
802    pub fn get_exact_mut(&mut self, lang: &Lang) -> Option<&mut V> {
803        if let Some(i) = self.exact_i(lang) {
804            Some(&mut self.inner[i].1)
805        } else {
806            None
807        }
808    }
809
810    /// Returns the current value or insert `new` and return a reference to it.
811    pub fn get_exact_or_insert(&mut self, lang: Lang, new: impl FnOnce() -> V) -> &mut V {
812        if let Some(i) = self.exact_i(&lang) {
813            return &mut self.inner[i].1;
814        }
815        let i = self.inner.len();
816        self.inner.push((lang, new()));
817        &mut self.inner[i].1
818    }
819
820    /// Insert the value with the exact match of `lang`.
821    ///
822    /// Returns the previous exact match.
823    pub fn insert(&mut self, lang: Lang, value: V) -> Option<V> {
824        if let Some(i) = self.exact_i(&lang) {
825            Some(mem::replace(&mut self.inner[i].1, value))
826        } else {
827            self.inner.push((lang, value));
828            None
829        }
830    }
831
832    /// Remove the exact match of `lang`.
833    pub fn remove(&mut self, lang: &Lang) -> Option<V> {
834        if let Some(i) = self.exact_i(lang) {
835            Some(self.inner.swap_remove(i).1)
836        } else {
837            None
838        }
839    }
840
841    /// Remove all exact and partial matches of `lang`.
842    ///
843    /// Returns a count of items removed.
844    pub fn remove_all(&mut self, lang: &Lang) -> usize {
845        let mut count = 0;
846        self.inner.retain(|(key, _)| {
847            let rmv = lang.matches(key, true, false);
848            if rmv {
849                count += 1
850            }
851            !rmv
852        });
853        count
854    }
855
856    /// Remove the last inserted lang.
857    pub fn pop(&mut self) -> Option<(Lang, V)> {
858        self.inner.pop()
859    }
860
861    /// Returns if the map is empty.
862    pub fn is_empty(&self) -> bool {
863        self.inner.is_empty()
864    }
865
866    /// Returns the number of languages in the map.
867    pub fn len(&self) -> usize {
868        self.inner.len()
869    }
870
871    /// Remove all entries.
872    pub fn clear(&mut self) {
873        self.inner.clear()
874    }
875
876    /// Iterator over lang keys.
877    pub fn keys(&self) -> impl std::iter::ExactSizeIterator<Item = &Lang> {
878        self.inner.iter().map(|(k, _)| k)
879    }
880
881    /// Iterator over values.
882    pub fn values(&self) -> impl std::iter::ExactSizeIterator<Item = &V> {
883        self.inner.iter().map(|(_, v)| v)
884    }
885
886    /// Iterator over values.
887    pub fn values_mut(&mut self) -> impl std::iter::ExactSizeIterator<Item = &mut V> {
888        self.inner.iter_mut().map(|(_, v)| v)
889    }
890
891    /// Into iterator of values.
892    pub fn into_values(self) -> impl std::iter::ExactSizeIterator<Item = V> {
893        self.inner.into_iter().map(|(_, v)| v)
894    }
895
896    /// Iterate over key-value pairs.
897    pub fn iter(&self) -> impl std::iter::ExactSizeIterator<Item = (&Lang, &V)> {
898        self.inner.iter().map(|(k, v)| (k, v))
899    }
900
901    /// Iterate over key-value pairs with mutable values.
902    pub fn iter_mut(&mut self) -> impl std::iter::ExactSizeIterator<Item = (&Lang, &mut V)> {
903        self.inner.iter_mut().map(|(k, v)| (&*k, v))
904    }
905}
906impl<V> LangMap<HashMap<LangFilePath, V>> {
907    /// Returns the match for `lang` and `file`.
908    pub fn get_file(&self, lang: &Lang, file: &LangFilePath) -> Option<&V> {
909        let files = self.get(lang)?;
910        if let Some(exact) = files.get(file) {
911            return Some(exact);
912        }
913        Self::best_file(files, file).map(|(_, v)| v)
914    }
915
916    /// Returns the best match to `lang` and `file` currently in the map.
917    pub fn best_file_match(&self, lang: &Lang, file: &LangFilePath) -> Option<&LangFilePath> {
918        let files = self.get(lang)?;
919        if let Some((exact, _)) = files.get_key_value(file) {
920            return Some(exact);
921        }
922        Self::best_file(files, file).map(|(k, _)| k)
923    }
924
925    fn best_file<'a>(files: &'a HashMap<LangFilePath, V>, file: &LangFilePath) -> Option<(&'a LangFilePath, &'a V)> {
926        let mut best = None;
927        let mut best_dist = u64::MAX;
928        for (k, v) in files {
929            if let Some(d) = k.matches(file)
930                && d < best_dist
931            {
932                best = Some((k, v));
933                best_dist = d;
934            }
935        }
936        best
937    }
938}
939impl<V> IntoIterator for LangMap<V> {
940    type Item = (Lang, V);
941
942    type IntoIter = std::vec::IntoIter<(Lang, V)>;
943
944    fn into_iter(self) -> Self::IntoIter {
945        self.inner.into_iter()
946    }
947}
948impl<V: PartialEq> PartialEq for LangMap<V> {
949    fn eq(&self, other: &Self) -> bool {
950        if self.len() != other.len() {
951            return false;
952        }
953        for (k, v) in &self.inner {
954            if other.get_exact(k) != Some(v) {
955                return false;
956            }
957        }
958        true
959    }
960}
961impl<V: Eq> Eq for LangMap<V> {}
962
963/// Errors found parsing a fluent resource file.
964#[derive(Clone, Debug)]
965pub struct FluentParserErrors(pub Vec<fluent_syntax::parser::ParserError>);
966impl fmt::Display for FluentParserErrors {
967    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
968        let mut sep = "";
969        for e in &self.0 {
970            write!(f, "{sep}{e}")?;
971            sep = "\n";
972        }
973        Ok(())
974    }
975}
976impl std::error::Error for FluentParserErrors {
977    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
978        if self.0.len() == 1 { Some(&self.0[0]) } else { None }
979    }
980}
981
982/// Localization resource file path in the localization directory.
983///
984/// In the default directory layout, localization dependencies are collected using `cargo zng l10n`
985/// and copied to `l10n/{lang}/deps/{name}/{version}/`, and localization for the app ([`is_current_app`])
986/// is placed in `l10n/{lang}/`.
987///
988/// [`is_current_app`]: Self::is_current_app
989#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
990#[non_exhaustive]
991pub struct LangFilePath {
992    /// Package name.
993    pub pkg_name: Txt,
994    /// Package version.
995    pub pkg_version: Version,
996    /// The localization file name, without extension.
997    pub file: Txt,
998}
999impl Ord for LangFilePath {
1000    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
1001        let self_pkg = self.actual_pkg_data();
1002        let other_pkg = other.actual_pkg_data();
1003        match self_pkg.0.cmp(other_pkg.0) {
1004            core::cmp::Ordering::Equal => {}
1005            ord => return ord,
1006        }
1007        match self_pkg.1.cmp(other_pkg.1) {
1008            core::cmp::Ordering::Equal => {}
1009            ord => return ord,
1010        }
1011        self.file().cmp(&other.file())
1012    }
1013}
1014impl PartialOrd for LangFilePath {
1015    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
1016        Some(self.cmp(other))
1017    }
1018}
1019impl std::hash::Hash for LangFilePath {
1020    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
1021        self.actual_pkg_data().hash(state);
1022        self.file().hash(state);
1023    }
1024}
1025impl Eq for LangFilePath {}
1026impl PartialEq for LangFilePath {
1027    fn eq(&self, other: &Self) -> bool {
1028        self.actual_pkg_data() == other.actual_pkg_data() && self.file() == other.file()
1029    }
1030}
1031impl LangFilePath {
1032    /// New from package name, version and file.
1033    pub fn new(pkg_name: impl Into<Txt>, pkg_version: Version, file: impl Into<Txt>) -> Self {
1034        let r = Self {
1035            pkg_name: pkg_name.into(),
1036            pkg_version,
1037            file: file.into(),
1038        };
1039        // these non-standard names are matched on fallback, but they can cause duplicate caching
1040        debug_assert!(
1041            r.file
1042                .rsplit_once('.')
1043                .map(|(_, ext)| !ext.eq_ignore_ascii_case("ftl"))
1044                .unwrap_or(true),
1045            "file `{}` must not have extension",
1046            r.file
1047        );
1048        debug_assert!(r.file != "_", "file `_` should be an empty string");
1049        r
1050    }
1051
1052    /// Gets a file in the current app.
1053    ///
1054    /// This value indicates that the localization resources are directly on the `l10n/{lang?}/` directories, not
1055    /// in the dependencies directories.
1056    ///
1057    /// See [`zng_env::about()`] for more details.
1058    pub fn current_app(file: impl Into<Txt>) -> LangFilePath {
1059        let about = zng_env::about();
1060        Self::new(about.pkg_name.clone(), about.version.clone(), file.into())
1061    }
1062
1063    /// Gets if this file is in the [`current_app`] resources, or the `pkg_name` is empty or the `pkg_version.pre` is `#.#.#-local`.
1064    ///
1065    /// [`current_app`]: Self::current_app
1066    pub fn is_current_app(&self) -> bool {
1067        self.is_current_app_no_check() || {
1068            let about = zng_env::about();
1069            self.pkg_name == about.pkg_name && self.pkg_version == about.version
1070        }
1071    }
1072
1073    fn is_current_app_no_check(&self) -> bool {
1074        self.pkg_name.is_empty() || self.pkg_version.pre.as_str() == "local"
1075    }
1076
1077    fn actual_pkg_data(&self) -> (&Txt, &Version) {
1078        if self.is_current_app_no_check() {
1079            let about = zng_env::about();
1080            (&about.pkg_name, &about.version)
1081        } else {
1082            (&self.pkg_name, &self.pkg_version)
1083        }
1084    }
1085
1086    /// Gets the normalized package name.
1087    ///
1088    /// This is the app package name if [`is_current_app`], otherwise is just the `pkg_name` value.
1089    ///
1090    /// [`is_current_app`]: Self::is_current_app
1091    pub fn pkg_name(&self) -> Txt {
1092        self.actual_pkg_data().0.clone()
1093    }
1094
1095    /// Gets the normalized package version.
1096    ///
1097    /// This is the app version if [`is_current_app`], otherwise is just the `pkg_version` value.
1098    ///
1099    /// [`is_current_app`]: Self::is_current_app
1100    pub fn pkg_version(&self) -> Version {
1101        self.actual_pkg_data().1.clone()
1102    }
1103
1104    /// Gets the normalized file name.
1105    ///
1106    /// This `"_"` for empty file or the file.
1107    pub fn file(&self) -> Txt {
1108        if self.file.is_empty() {
1109            Txt::from_char('_')
1110        } else {
1111            self.file.clone()
1112        }
1113    }
1114
1115    /// Get the file path, relative to the localization dir.
1116    ///
1117    /// * Empty file name is the same as `_`.
1118    /// * If package [`is_current_app`] gets `{lang}/{file}.ftl`.
1119    /// * Else if is another package gets `{lang}/deps/{pkg_name}/{pkg_version}/{file}.ftl`.
1120    ///
1121    /// [`is_current_app`]: Self::is_current_app
1122    pub fn to_path(&self, lang: &Lang) -> PathBuf {
1123        let mut file = self.file.as_str();
1124        if file.is_empty() {
1125            file = "_";
1126        }
1127        if self.is_current_app() {
1128            format!("{lang}/{file}.ftl")
1129        } else {
1130            format!("{lang}/deps/{}/{}/{file}.ftl", self.pkg_name, self.pkg_version)
1131        }
1132        .into()
1133    }
1134
1135    /// Gets a value that indicates if the resources represented by `self` can be used for `search`.
1136    ///
1137    /// The number indicates the quality of the match:
1138    ///
1139    /// * `0` is an exact match.
1140    /// * `b1` is a match with only version `build` differences.
1141    /// * `b10` is a match with only version `pre` differences.
1142    /// * `(0..u16::MAX) << 16` is a match with only `patch` differences and the absolute distance.
1143    /// * `(0..u16::MAX) << 16 * 2` is a match with `minor` differences and the absolute distance.
1144    /// * `(0..u16::MAX) << 16 * 3` is a match with `major` differences and the absolute distance.
1145    /// * `None`` is a `pkg_name` mismatch.
1146    pub fn matches(&self, search: &Self) -> Option<u64> {
1147        let (self_name, self_version) = self.actual_pkg_data();
1148        let (search_name, search_version) = search.actual_pkg_data();
1149
1150        if self_name != search_name {
1151            return None;
1152        }
1153
1154        if self.file != search.file {
1155            let file_a = self.file.rsplit_once('.').map(|t| t.0).unwrap_or(self.file.as_str());
1156            let file_b = search.file.rsplit_once('.').map(|t| t.0).unwrap_or(search.file.as_str());
1157            if file_a != file_b {
1158                let is_empty_a = file_a == "_" || file_a.is_empty();
1159                let is_empty_b = file_b == "_" || file_b.is_empty();
1160                if !(is_empty_a && is_empty_b) {
1161                    return None;
1162                }
1163            }
1164            tracing::warn!(
1165                "fallback matching `{}` with `{}`, file was not expected to have extension",
1166                self.file,
1167                search.file
1168            )
1169        }
1170
1171        fn dist(a: u64, b: u64, shift: u64) -> u64 {
1172            let (l, s) = match a.cmp(&b) {
1173                std::cmp::Ordering::Equal => return 0,
1174                std::cmp::Ordering::Less => (b, a),
1175                std::cmp::Ordering::Greater => (a, b),
1176            };
1177
1178            (l - s).min(u16::MAX as u64) << (16 * shift)
1179        }
1180
1181        let mut d = 0;
1182        if self_version.build != search_version.build {
1183            d = 1;
1184        }
1185        if self_version.pre != search_version.pre {
1186            d |= 0b10;
1187        }
1188
1189        d |= dist(self_version.patch, search_version.patch, 1);
1190        d |= dist(self_version.minor, search_version.minor, 2);
1191        d |= dist(self_version.major, search_version.major, 3);
1192
1193        Some(d)
1194    }
1195}
1196impl_from_and_into_var! {
1197    fn from(file: Txt) -> LangFilePath {
1198        LangFilePath::current_app(file)
1199    }
1200
1201    fn from(file: &'static str) -> LangFilePath {
1202        LangFilePath::current_app(file)
1203    }
1204
1205    fn from(file: String) -> LangFilePath {
1206        LangFilePath::current_app(file)
1207    }
1208}
1209
1210#[cfg(test)]
1211mod tests {
1212    use super::*;
1213
1214    #[test]
1215    fn file_matches() {
1216        fn check(a: &str, b: &str, c: &str) {
1217            let ap = LangFilePath::new("name", a.parse().unwrap(), "file");
1218            let bp = LangFilePath::new("name", b.parse().unwrap(), "file");
1219            let cp = LangFilePath::new("name", c.parse().unwrap(), "file");
1220
1221            let ab = ap.matches(&bp);
1222            let ac = ap.matches(&cp);
1223
1224            assert!(ab < ac, "expected {a}.matches({b}) < {a}.matches({c})")
1225        }
1226
1227        check("0.0.0", "0.0.1", "0.1.0");
1228        check("0.0.1", "0.1.0", "1.0.0");
1229        check("0.0.0-pre", "0.0.0-pre+build", "0.0.0-other+build");
1230        check("0.0.0+build", "0.0.0+build", "0.0.0+other");
1231        check("0.0.1", "0.0.2", "0.0.3");
1232        check("0.1.0", "0.2.0", "0.3.0");
1233        check("1.0.0", "2.0.0", "3.0.0");
1234        check("1.0.0", "1.1.0", "2.0.0");
1235    }
1236
1237    #[test]
1238    fn file_name_mismatches() {
1239        let ap = LangFilePath::new("name", "1.0.0".parse().unwrap(), "file-a");
1240        let bp = LangFilePath::new("name", "1.0.0".parse().unwrap(), "file-b");
1241
1242        assert!(ap.matches(&bp).is_none());
1243    }
1244}