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
//! Localization support for Gooey using [Project Fluent](https://projectfluent.org).

#![forbid(unsafe_code)]
#![warn(
    clippy::cargo,
    missing_docs,
    clippy::pedantic,
    future_incompatible,
    rust_2018_idioms
)]
#![allow(clippy::if_not_else, clippy::module_name_repetitions)]
#![cfg_attr(doc, warn(rustdoc::all))]

use std::{borrow::Cow, collections::HashMap, fmt::Debug};

pub use fluent;
use fluent::{
    bundle::FluentBundle,
    types::{FluentNumber, FluentNumberOptions},
    FluentArgs, FluentResource, FluentValue,
};
use fluent_langneg::NegotiationStrategy;
use gooey_core::{
    unic_langid::LanguageIdentifier, LocalizationParameter, LocalizationParameters, Localizer,
};
use intl_memoizer::concurrent::IntlLangMemoizer;

/// A [`Localizer`] for Gooey that utilizes [`fluent`].
///
/// During construction, [`FluentLocalizer`] gathers a unique list of primary
/// [`LanguageIdentifier`]s for each of the [`FluentBundle`]s provided. When
/// localizing, this list is filtered using
/// [`fluent-langneg`](https://crates.io/crates/fluent-langneg), and resources
/// are searched through all matching bundles in the order determined by
/// `fluent-langneg`.
///
/// This type supports inserting more than one bundle for a given
/// [`LanguageIdentifier`].
#[must_use]
pub struct FluentLocalizer {
    default_language: LanguageIdentifier,
    languages: Vec<LanguageIdentifier>,
    bundles: HashMap<LanguageIdentifier, Vec<FluentBundle<FluentResource, IntlLangMemoizer>>>,
}

impl FluentLocalizer {
    /// Creates a new instance that localizes using `bundles`.
    pub fn new(
        default_language: LanguageIdentifier,
        bundles: Vec<FluentBundle<FluentResource, IntlLangMemoizer>>,
    ) -> Self {
        let mut bundle_map = HashMap::<
            LanguageIdentifier,
            Vec<FluentBundle<FluentResource, IntlLangMemoizer>>,
        >::new();
        let mut languages = Vec::new();
        for mut bundle in bundles {
            // TODO BiDi support. For now, this causes rendering issues.
            bundle.set_use_isolating(false);
            let primary_locale = bundle
                .locales
                .first()
                .expect("All bundles need at least one locale");
            if let Some(existing) = bundle_map.get_mut(primary_locale) {
                existing.push(bundle);
            } else {
                languages.push(primary_locale.clone());
                bundle_map.insert(primary_locale.clone(), vec![bundle]);
            }
        }
        Self {
            default_language,
            bundles: bundle_map,
            languages,
        }
    }
}

impl Debug for FluentLocalizer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FluentLocalizer").finish_non_exhaustive()
    }
}

impl Localizer for FluentLocalizer {
    fn localize<'a>(
        &self,
        key: &str,
        parameters: Option<LocalizationParameters<'a>>,
        language: &LanguageIdentifier,
    ) -> String {
        for language in fluent_langneg::negotiate_languages(
            &[language],
            &self.languages,
            Some(&self.default_language),
            NegotiationStrategy::Filtering,
        ) {
            if let Some(language_bundles) = self.bundles.get(language) {
                for bundle in language_bundles {
                    if let Some(message) = bundle.get_message(key) {
                        let mut errors = Vec::new();
                        if let Some(localized) = message.value().map(|pattern| {
                            let args = parameters.as_ref().map(|parameters| {
                                let mut args = FluentArgs::new();
                                for (key, value) in parameters.iter() {
                                    args.set(
                                        key,
                                        match value {
                                            LocalizationParameter::String(value) => {
                                                FluentValue::String(Cow::Borrowed(value))
                                            }
                                            LocalizationParameter::Numeric(value) => {
                                                FluentValue::Number(FluentNumber::new(
                                                    *value,
                                                    FluentNumberOptions::default(),
                                                ))
                                            }
                                        },
                                    );
                                }
                                args
                            });
                            bundle
                                .format_pattern(pattern, args.as_ref(), &mut errors)
                                .to_string()
                        }) {
                            return localized;
                        }
                        log::error!(
                            "Error localizing '{}' in language {}: {:?}",
                            key,
                            language,
                            errors
                        );
                    }
                }
            }
        }

        String::from(key)
    }
}