cushy/
localization.rs

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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//! Localization allows user interfaces to be presented in the user's native
//! locale (language and region).
//!
//! Localization in Cushy is powered by [Fluent](https://projectfluent.org/).
//! Fluent offers a variety of features that solve many common localization
//! problems. If you think your application might benefit from having multiple
//! languages, it might save a lot of time to build the application with
//! localization in mind.
//!
//! Thankfully, localization in Cushy is fairly straightforward. Wherever you
//! want to display a localizable message, use the [`Localize`] type or the
//! [`localize!`](crate::localize) macro:
//!
//! ```rust
//! use cushy::localization::Localize;
//! use cushy::localize;
//!
//! let message = Localize::new("hello-world");
//! let message = localize!("hello-world");
//! ```
//!
//! Regardless of which style you prefer, `message` now contains a localizable
//! message. When the application is running, wherever `message` is used, the
//! message will be looked up in the current locale.
//!
//! Localization messages are resolved through the application's
//! [`Cushy`](crate::Cushy) instance.
//! [`Cushy::localizations()`](crate::Cushy::localizations) returns the global
//! [`Localizations`] collection, which [`Localization`]s can be added to.
//! Consider this simple example:
//!
//! ```rust
//! use cushy::localization::Localization;
//! use cushy::{localize, Open, PendingApp};
//!
//! # fn main() {
//! #[cushy::main]
//! fn main(app: &mut PendingApp) -> cushy::Result {
//!     app.cushy()
//!         .localizations()
//!         .add_default(Localization::for_language("en-US", "hello = Hello World!").unwrap());
//!     app.cushy()
//!         .localizations()
//!         .add_default(Localization::for_language("es-MX", "hello = ¡Hola Mundo!").unwrap());
//!     app.cushy()
//!         .localizations()
//!         .add_default(Localization::for_language("fr-FR", "hello = Bonjour monde!").unwrap());
//!
//!     localize!("hello").open(app)?;
//!
//!     Ok(())
//! }
//! # }
//! ```
//!
//! Additionally, Fluent supports providing arguments to localization messages:
//!
//!
//! ```rust
//! use cushy::localization::Localization;
//! use cushy::{localize, Open, PendingApp};
//!
//! # fn main() {
//! #[cushy::main]
//! fn main(app: &mut PendingApp) -> cushy::Result {
//!     app.cushy()
//!         .localizations()
//!         .add_default(Localization::for_language("en-US", "hello-user = Hello {$name}!").unwrap());
//!     app.cushy()
//!         .localizations()
//!         .add_default(Localization::for_language("es-MX", "hello-user = ¡Hola {$name}!").unwrap());
//!     app.cushy()
//!         .localizations()
//!         .add_default(Localization::for_language("fr-FR", "hello-user = Bonjour {$name}!").unwrap());
//!
//!     localize!("hello", "user" => "Ecton").open(app)?;
//!
//!     Ok(())
//! }
//! # }
//! ```
//!
//! # Locale Fallback Behavior
//!
//! Cushy attempts to find an exact match between the current locale and a
//! loaded [`Localization`]. If an exact match is not found, [`fluent_langneg`]
//! is used to try to find a fallback locale. If the message being localized
//! cannot be found in either of these locales, the message is looked up in the
//! *default locale*.
//!
//! Cushy has the concept of a *default locale*. There is no default locale
//! until either [`Localizations::add_default`] or
//! [`Localizations::set_default_locale`] are executed. Once a default locale is
//! established, any messages that cannot be found in the current locale or a
//! fallback locale will be localized using the default locale as a final
//! effort.
//!
//! Using the default locale can be convenient, but it can also make it harder
//! to visually notice when a message is missing from a particular locale. When
//! relying on third parties to provide localizations, it can be beneficial to
//! ensure that a valid message is always shown even if a localized message has
//! not been provided yet.

use core::fmt;
use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt::{Debug, Display, Formatter};
use std::str::FromStr;

use fluent_bundle::concurrent::FluentBundle;
use fluent_bundle::{FluentArgs, FluentMessage, FluentResource, FluentValue};
use intentional::Assert;
use kempt::{map, Map};
use unic_langid::{LanguageIdentifier, LanguageIdentifierError};

use crate::context::{EventContext, GraphicsContext, LayoutContext, Trackable, WidgetContext};
use crate::value::{Dynamic, DynamicRead, Generation, IntoValue, Source, Value};
use crate::widget::{MakeWidgetWithTag, WidgetInstance, WidgetTag};
use crate::widgets::label::DynamicDisplay;
use crate::MaybeLocalized;

impl MaybeLocalized {
    /// Returns the localized version of this string, using `context` to
    /// localize.
    pub fn localize<'a>(&'a self, context: &impl LocalizationContext) -> Cow<'a, str> {
        match self {
            MaybeLocalized::Text(value) => Cow::Borrowed(value),
            MaybeLocalized::Localized(value) => Cow::Owned(value.localize(context)),
        }
    }
}

pub(crate) struct WindowTranslationContext<'a>(pub(crate) &'a Localizations);

/// A context that is used while localizing values.
pub trait LocalizationContext {
    /// Returns the current locale of this context.
    fn locale(&self) -> LanguageIdentifier;
    /// Returns the localizations for this context.
    fn localizations(&self) -> &Localizations;
    /// Invalidates `trackable` when changed.
    ///
    /// Some values are localized outside of the context of a window being
    /// opened: for example, the Window's title. In situations like these,
    /// invalidation is ignored.
    fn invalidate_when_changed(&self, trackable: &impl Trackable);
}

impl LocalizationContext for WidgetContext<'_> {
    fn locale(&self) -> LanguageIdentifier {
        self.locale().get_tracking_invalidate(self)
    }

    fn localizations(&self) -> &Localizations {
        self.localizations()
    }

    fn invalidate_when_changed(&self, trackable: &impl Trackable) {
        trackable.invalidate_when_changed(self);
    }
}

impl LocalizationContext for EventContext<'_> {
    fn locale(&self) -> LanguageIdentifier {
        self.widget.locale().get_tracking_invalidate(&self.widget)
    }

    fn localizations(&self) -> &Localizations {
        self.widget.localizations()
    }

    fn invalidate_when_changed(&self, trackable: &impl Trackable) {
        trackable.invalidate_when_changed(&self.widget);
    }
}

impl LocalizationContext for GraphicsContext<'_, '_, '_, '_> {
    fn locale(&self) -> LanguageIdentifier {
        self.widget.locale().get_tracking_invalidate(&self.widget)
    }

    fn localizations(&self) -> &Localizations {
        self.widget.localizations()
    }

    fn invalidate_when_changed(&self, trackable: &impl Trackable) {
        trackable.invalidate_when_changed(&self.widget);
    }
}

impl LocalizationContext for LayoutContext<'_, '_, '_, '_> {
    fn locale(&self) -> LanguageIdentifier {
        self.widget.locale().get_tracking_invalidate(&self.widget)
    }

    fn localizations(&self) -> &Localizations {
        self.widget.localizations()
    }

    fn invalidate_when_changed(&self, trackable: &impl Trackable) {
        trackable.invalidate_when_changed(&self.widget);
    }
}

impl LocalizationContext for WindowTranslationContext<'_> {
    fn locale(&self) -> LanguageIdentifier {
        LanguageIdentifier::default()
    }

    fn localizations(&self) -> &Localizations {
        self.0
    }

    fn invalidate_when_changed(&self, _trackable: &impl Trackable) {}
}

impl From<Localize> for MaybeLocalized {
    fn from(value: Localize) -> Self {
        Self::Localized(value)
    }
}

impl DynamicDisplay for MaybeLocalized {
    fn fmt(
        &self,
        context: &WidgetContext<'_>,
        f: &mut std::fmt::Formatter<'_>,
    ) -> std::fmt::Result {
        match self {
            MaybeLocalized::Text(text) => Display::fmt(text, f),
            MaybeLocalized::Localized(localize) => DynamicDisplay::fmt(localize, context, f),
        }
    }
}

/// The primary of defining localized message
#[derive(Clone, Debug)]
pub struct Localize {
    key: Cow<'static, str>,
    args: Vec<(String, Value<FluentValue<'static>>)>,
}

impl IntoValue<MaybeLocalized> for Localize {
    fn into_value(self) -> Value<MaybeLocalized> {
        Value::Constant(MaybeLocalized::from(self))
    }
}

impl Localize {
    /// Returns a value that localizes `key` at runtime.
    ///
    /// The `key` should refer to a valid message identifier in the loaded
    /// [`Localizations`] for the application.
    pub fn new(key: impl Into<Cow<'static, str>>) -> Self {
        Self {
            key: key.into(),
            args: Vec::new(),
        }
    }

    /// Returns localized value using `context`.
    pub fn localize(&self, context: &impl LocalizationContext) -> String {
        let mut localized = String::new();
        self.localize_into(context, &mut localized)
            .assert("format success");
        localized
    }

    /// Add a named argument, which can be used with parameterized messages.
    ///
    /// See [Variables](https://projectfluent.org/fluent/guide/variables.html)
    #[must_use]
    pub fn arg(
        mut self,
        key: impl Into<String>,
        value: impl IntoValue<FluentValue<'static>>,
    ) -> Self {
        self.args.push((key.into(), value.into_value()));
        self
    }

    fn get_args(&self, context: &impl LocalizationContext) -> FluentArgs {
        let mut res = FluentArgs::new();
        for (name, arg) in &self.args {
            context.invalidate_when_changed(arg);
            res.set(name.to_owned(), arg.get());
        }
        res
    }

    fn localize_into<W: fmt::Write>(
        &self,
        context: &impl LocalizationContext,
        f: &mut W,
    ) -> fmt::Result {
        let locale = context.locale();

        let localizations = context.localizations();
        let mut state = localizations.state.lock();
        // When localizing, we need mut access to update the FallbackLocales
        // cache. We don't want fallback locale renegotation to cause extra
        // invalidations.
        state.prevent_notifications();

        let Some((bundle, message)) = state.localize(self, &locale) else {
            return f.write_str(&format!("No message. locale: {locale}, key: {}", self.key));
        };

        let Some(value) = message.value() else {
            return f.write_str(&format!("No value. locale: {locale}, key: {}", self.key));
        };

        let mut err = vec![];
        let args = self.get_args(context);
        let res = bundle.format_pattern(value, Some(&args), &mut err);

        if err.is_empty() {
            f.write_str(&res)
        } else {
            f.write_str(&format!(
                "{} {{Error. locale: {}, key: {}, cause: {:?}}}",
                locale, self.key, res, err
            ))
        }
    }
}

/// Returns a message localized in the current locale.
///
/// The first argument to this macro is the unique id/key of the message being
/// localized. After the initial argument, the remaning arguments are expected
/// to be `name => value` pairs.
///
/// ```rust
/// use cushy::localize;
///
/// let message = localize!("welcome-message");
///
/// let message = localize!("welcome-message", "user" => "Ecton");
/// ```
///
/// This macro always returns a [`Localize`].
#[macro_export]
macro_rules! localize {
    ($key:expr) => {
        $crate::localization::Localize::new($key)
    };
    ($key:expr, $($name:expr => $arg:expr),*) => {
        {
            let mut localize = $crate::localization::Localize::new($key);
            $(
                localize = localize.arg($name, $arg);
            )*
            localize
        }
    };
}

macro_rules! impl_into_fluent_value {
        ($($ty:ty)+) => {
            $(impl_into_fluent_value!(. $ty);)+
        };
        (. $ty:ty) => {
            impl IntoValue<FluentValue<'static>> for $ty {
                fn into_value(self) -> Value<FluentValue<'static>> {
                    Value::Constant(FluentValue::from(self))
                }
            }
            impl IntoValue<FluentValue<'static>> for Dynamic<$ty> {
                fn into_value(self) -> Value<FluentValue<'static>> {
                    (&self).into_value()
                }
            }
            impl IntoValue<FluentValue<'static>> for &Dynamic<$ty> {
                fn into_value(self) -> Value<FluentValue<'static>> {
                    Value::Dynamic(self.map_each_into())
                }
            }
        };
    }

impl_into_fluent_value!(i8 i16 i32 i64 i128 isize);
impl_into_fluent_value!(u8 u16 u32 u64 u128 usize);
impl_into_fluent_value!(f32 f64);
impl_into_fluent_value!(String &'static str);

impl DynamicDisplay for Localize {
    fn generation(&self, context: &WidgetContext<'_>) -> Option<Generation> {
        let mut generation = context.localizations().state.generation();
        if let Some(locale_generation) = context.locale().generation() {
            generation += locale_generation;
        }

        Some(
            self.args
                .iter()
                .filter_map(|(_name, value)| value.generation())
                .fold(generation, |generation, value_generation| {
                    generation + value_generation
                }),
        )
    }

    fn fmt(&self, context: &WidgetContext<'_>, f: &mut Formatter<'_>) -> fmt::Result {
        self.localize_into(context, f)
    }
}

impl MakeWidgetWithTag for Localize {
    fn make_with_tag(self, tag: WidgetTag) -> WidgetInstance {
        self.into_label().make_with_tag(tag)
    }
}

/// A localization for a specific locale.
pub struct Localization {
    /// The locale this localization applies to.
    pub locale: LanguageIdentifier,
    /// The Fluent (.ftl) source for this localization.
    pub fluent: String,
}

impl Localization {
    /// Returns a new localization from the given language and Fluent source.
    pub fn new(language: LanguageIdentifier, fluent: impl Into<String>) -> Self {
        Self {
            locale: language,
            fluent: fluent.into(),
        }
    }

    /// Returns a new localization from the given language and Fluent source.
    ///
    /// # Errors
    ///
    /// Returns an error if `language` is not a valid Unicode language
    /// identifier.
    pub fn for_language(
        language: &str,
        fluent: impl Into<String>,
    ) -> Result<Self, LanguageIdentifierError> {
        Ok(Self {
            locale: LanguageIdentifier::from_str(language)?,
            fluent: fluent.into(),
        })
    }
}

/// A locale (language and region)
#[derive(Default, Debug, Clone, PartialEq)]
pub enum Locale {
    /// Detect the locale of the system running the application.
    #[default]
    System,
    /// Use a specific locale with the given id.
    WithId(LanguageIdentifier),
}

/// A collection of localizations to apply to a Cushy application.
#[derive(Clone, Default)]
pub struct Localizations {
    state: Dynamic<TranslationState>,
    locale: Dynamic<Locale>,
}

impl Localizations {
    /// Add a `Fluent` translation file for a given locale.
    ///
    /// Note the `.ftl` file is not immediately parsed.
    pub fn add(&self, translation: Localization) {
        let mut state = self.state.lock();

        state.add(translation);
    }

    /// Add a `Fluent` translation file for a given locale, setting this
    /// translation's locale as the default locale for this application.
    ///
    /// Note the `.ftl` file is not immediately parsed.
    ///
    /// See [`Localizations::set_default_locale`] for more information about
    /// what the default locale is for.
    pub fn add_default(&self, translation: Localization) {
        let mut state = self.state.lock();
        state.default_locale = translation.locale.clone();

        state.add(translation);
    }

    /// Sets the locale to use as a fallback when the currently set or detected
    /// locales cannot localize a given value.
    ///
    /// This allows incompatible languages to be used as a "final" fallback. If
    /// the application is originally developed in the United States in English,
    /// for example, the default locale could be set to `en-US` and any missing
    /// strings from other languages will still be shown using the `en-US`
    /// values.
    pub fn set_default_locale(&self, locale: LanguageIdentifier) {
        self.state.lock().default_locale = locale;
    }

    /// Returns the default locale.
    ///
    /// See [`Localizations::set_default_locale`] for more information about
    /// what the default locale is for.
    #[must_use]
    pub fn default_locale(&self) -> LanguageIdentifier {
        self.state.read().default_locale.clone()
    }

    /// Returns a dynamic that controls the expected locale of the user for the
    /// application.
    ///
    /// This dynamic contains [`Locale::System`] by default.
    ///
    /// Changing the value contained by this dynamaic will update the locale for
    /// the entire application. The [`Localized`](crate::widgets::Localized)
    /// widget can be used to localize a section of an user interface.
    #[must_use]
    pub const fn user_locale(&self) -> &Dynamic<Locale> {
        &self.locale
    }

    #[must_use]
    pub(crate) fn effective_locale(&self, context: &WidgetContext<'_>) -> LanguageIdentifier {
        match self.user_locale().get_tracking_invalidate(context) {
            Locale::System => sys_locale::get_locale()
                .and_then(|locale| LanguageIdentifier::from_str(&locale).ok())
                .unwrap_or_else(|| self.default_locale()),
            Locale::WithId(id) => id,
        }
    }
}

struct TranslationState {
    fallback_locales: FallbackLocales,
    default_locale: LanguageIdentifier,
    all_locales: Vec<LanguageIdentifier>,
    loaded_translations: HashMap<LanguageIdentifier, FluentBundle<FluentResource>>,
}

impl Default for TranslationState {
    fn default() -> Self {
        Self {
            fallback_locales: FallbackLocales::default(),
            default_locale: LanguageIdentifier::default(),
            all_locales: Vec::new(),
            loaded_translations: HashMap::from([(
                LanguageIdentifier::default(),
                FluentBundle::new_concurrent(vec![LanguageIdentifier::default()]),
            )]),
        }
    }
}

impl TranslationState {
    fn add(&mut self, translation: Localization) {
        let res = match FluentResource::try_new(translation.fluent) {
            Ok(res) => res,
            Err((res, errors)) => {
                for err in errors {
                    tracing::error!("error parsing {} localization: {err}", translation.locale);
                }
                res
            }
        };
        let bundle = self
            .loaded_translations
            .entry(translation.locale.clone())
            .or_insert_with(|| FluentBundle::new_concurrent(vec![translation.locale.clone()]));
        if let Err(errors) = bundle.add_resource(res) {
            for err in errors {
                tracing::error!("error adding {} localization: {err}", translation.locale);
            }
        }
        self.fallback_locales.clear();
    }

    #[must_use]
    fn localize<'a>(
        &'a mut self,
        message: &Localize,
        locale: &LanguageIdentifier,
    ) -> Option<(&'a FluentBundle<FluentResource>, FluentMessage<'a>)> {
        self.loaded_translations
            .get(locale)
            .and_then(|bundle| {
                bundle
                    .get_message(&message.key)
                    .map(|message| (bundle, message))
            })
            .or_else(|| {
                self.fallback_locales
                    .fallback_for(locale, &self.all_locales, &self.default_locale)
                    .and_then(|fallback| self.loaded_translations.get(fallback))
                    .and_then(|bundle| {
                        bundle
                            .get_message(&message.key)
                            .map(|message| (bundle, message))
                    })
            })
            .or_else(|| {
                self.loaded_translations
                    .get(&self.default_locale)
                    .and_then(|bundle| {
                        bundle
                            .get_message(&message.key)
                            .map(|message| (bundle, message))
                    })
            })
    }
}

#[derive(Default)]
struct FallbackLocales(Map<LanguageIdentifier, LanguageIdentifier>);

impl FallbackLocales {
    fn clear(&mut self) {
        self.0.clear();
    }

    fn fallback_for<'a>(
        &'a mut self,
        language: &LanguageIdentifier,
        available_locales: &[LanguageIdentifier],
        default_locale: &LanguageIdentifier,
    ) -> Option<&'a LanguageIdentifier> {
        match self.0.entry(language) {
            map::Entry::Occupied(entry) => Some(entry.into_mut()),
            map::Entry::Vacant(vacant) => {
                let fallback = fluent_langneg::negotiate::negotiate_languages(
                    &[language.clone()],
                    available_locales,
                    Some(default_locale),
                    fluent_langneg::NegotiationStrategy::Filtering,
                )
                .into_iter()
                .next()?;

                Some(vacant.insert(fallback.clone()))
            }
        }
    }
}