Skip to main content

day_localization/
lib.rs

1// Copyright © The Daybrite Project
2// SPDX-License-Identifier: MPL-2.0
3
4//! day-localization: the core localization engine (DESIGN.md §12), low enough in the crate graph that even
5//! the central crates (day-pieces' dialogs, menu-role labels) can localize their own UI strings.
6//!
7//! Two tiers of global Fluent bundles:
8//! - **core**: a built-in catalog (`catalog/*.ftl`) of the standard strings the framework itself
9//!   emits (dialog OK/Cancel, standard menu commands), shipped in several languages. Always present.
10//! - **app**: the locales an app registers with [`install`]. These take precedence over core, so an
11//!   app can override any `day_*` string, and core is the fallback for keys the app didn't define.
12//!
13//! Private [`Catalog`] namespaces live alongside these tiers. They share the locale signal,
14//! but never merge their messages into app/core or each other.
15//!
16//! The current locale is a [`Signal`], so every `format`/binding re-runs on a locale switch. The
17//! piece-facing reactive `tr()` wrapper lives one layer up in `day-localization-ui` (which re-exports this).
18
19use std::cell::RefCell;
20use std::collections::HashMap;
21
22day_reactive::tls_root! {
23    collate: crate::collate::TlsGroupSlots,
24    datetime: crate::datetime::TlsGroupSlots,
25    decimal: crate::decimal::TlsGroupSlots,
26    root: crate::TlsGroupSlots,
27}
28
29use day_reactive::Signal;
30use fluent_bundle::{FluentArgs, FluentBundle, FluentResource, FluentValue};
31use unic_langid::LanguageIdentifier;
32
33mod collate;
34mod datetime;
35mod decimal;
36mod number;
37pub use number::NumberFormat;
38mod intl;
39mod search;
40
41pub use collate::{compare, compare_in, sort_localized};
42pub use datetime::{DateFields, format_date, format_date_in};
43pub use decimal::{format_decimal, format_decimal_in, format_percent, format_percent_in};
44pub use search::{matches_search, matches_search_in};
45
46/// The built-in core catalog: standard UI strings the framework needs, per language. Apps override
47/// individual keys by defining them in their own catalog (see [`install`]).
48const CORE_CATALOG: &[(&str, &str)] = &[
49    ("en", include_str!("../catalog/en.ftl")),
50    ("fr", include_str!("../catalog/fr.ftl")),
51    ("es", include_str!("../catalog/es.ftl")),
52    ("de", include_str!("../catalog/de.ftl")),
53    ("ja", include_str!("../catalog/ja.ftl")),
54    ("zh", include_str!("../catalog/zh.ftl")),
55    ("ar", include_str!("../catalog/ar.ftl")),
56];
57
58// `key::<name>` and the `__core_strings!` accessors, generated from catalog/en.ftl by build.rs.
59include!(concat!(env!("OUT_DIR"), "/core_strings.rs"));
60
61/// An embedded, private Fluent namespace. Generated by `day_build::generate_locales`.
62/// Catalog identity is its static address, so identical keys in different crates never collide.
63/// `name` is only the qualified lookup name used by scripting (`crate::file::key`).
64pub struct Catalog {
65    pub name: &'static str,
66    pub default: &'static str,
67    pub locales: &'static [(&'static str, &'static str)],
68}
69
70struct ScopedBundles {
71    catalog: &'static Catalog,
72    bundles: HashMap<String, FluentBundle<FluentResource>>,
73}
74
75struct State {
76    scoped: HashMap<usize, ScopedBundles>,
77    /// App-registered bundles (from `install`), keyed by locale; take precedence over `core`.
78    app: HashMap<String, FluentBundle<FluentResource>>,
79    /// Built-in core catalog bundles, keyed by locale; the fallback for `day_*` keys.
80    core: HashMap<String, FluentBundle<FluentResource>>,
81    default: String,
82    locale: Signal<String>,
83}
84
85day_reactive::tls_slots! {
86    root;
87    static STATE: RefCell<Option<State>> = const { RefCell::new(None) };
88
89    /// The host's ordered locale preference, set by the platform entry before `install` runs:
90    /// the entry point for hosts with no process environment (web-dom seeds it from the page's
91    /// `?locale=`, native backends from the OS). The `DAY_LOCALE` environment variable, where one
92    /// exists, still wins.
93    static LAUNCH_LOCALES: RefCell<Vec<String>> = const { RefCell::new(Vec::new()) };
94
95}
96
97fn build_bundles(locales: &[(&str, &str)]) -> HashMap<String, FluentBundle<FluentResource>> {
98    let mut bundles = HashMap::new();
99    for (name, src) in locales {
100        let langid: LanguageIdentifier = name.parse().unwrap_or_else(|_| "en".parse().unwrap());
101        let mut bundle = FluentBundle::new(vec![langid]);
102        // icu4x-backed NUMBER()/DATETIME() + the bundle-wide number formatter (src/intl.rs).
103        intl::register(&mut bundle);
104        // Overriding the same key across resources (app > core when merged) is expected; Fluent
105        // errors on duplicate adds, so we keep app and core in separate bundles and pick at lookup.
106        match FluentResource::try_new((*src).to_string()) {
107            Ok(res) => {
108                let _ = bundle.add_resource(res);
109            }
110            Err((res, errs)) => {
111                log::warn!("day-localization: {name}: {} syntax error(s)", errs.len());
112                let _ = bundle.add_resource(res);
113            }
114        }
115        bundles.insert((*name).to_string(), bundle);
116    }
117    bundles
118}
119
120/// Lazily create the state with the core catalog loaded, so core strings (and `format_in`) work even
121/// before an app calls [`install`]. The initial locale honors `DAY_LOCALE`.
122fn ensure_state() {
123    STATE.with(|s| {
124        if s.borrow().is_some() {
125            return;
126        }
127        let initial = std::env::var("DAY_LOCALE")
128            .ok()
129            .map(normalize)
130            .unwrap_or_else(|| "en".to_string());
131        *s.borrow_mut() = Some(State {
132            app: HashMap::new(),
133            scoped: HashMap::new(),
134            core: build_bundles(CORE_CATALOG),
135            default: "en".to_string(),
136            // ROOT-scoped: this is a process-global signal, but `ensure_state` runs on the
137            // First touch of any l10n API, which can be inside a transient scope (a cover's
138            // presented content calling `t()` was the first observed case; the signal then
139            // died with that cover and every later read panicked). Globals must never
140            // inherit a caller's scope.
141            locale: Signal::global(initial),
142        });
143    });
144}
145
146/// Record the host's launch locale before [`install`] runs. Platform glue only; apps pick
147/// locales with `set_locale`. No-op once `install` has resolved the initial locale.
148pub fn set_launch_locale(locale: &str) {
149    set_launch_locales(&[locale.to_string()]);
150}
151
152/// Append the host's ordered preference after anything already recorded.
153///
154/// The two sources arrive in that order and both matter: a host override (web-dom's `?locale=`,
155/// set by the page glue) is a deliberate choice and stays first; the OS preference the backend
156/// reports is the fallback behind it. Duplicates are dropped, so a repeat call cannot reorder.
157pub fn add_launch_locales(locales: &[String]) {
158    LAUNCH_LOCALES.with(|l| {
159        let mut l = l.borrow_mut();
160        for loc in locales {
161            if !l.contains(loc) {
162                l.push(loc.clone());
163            }
164        }
165    });
166}
167
168/// Record the host's ordered locale preference (`["fr-CA", "fr", "en"]`) before [`install`] runs.
169///
170/// An OS answers with a list, not a locale, and the app's catalogs are not registered yet, so the
171/// list is kept whole and [`install`] picks the first entry it can serve. Taking only the
172/// first would drop a user whose phone reads "Canadian French, then French" for an app that ships
173/// `fr` but not `fr-CA`.
174pub fn set_launch_locales(locales: &[String]) {
175    LAUNCH_LOCALES.with(|l| *l.borrow_mut() = locales.to_vec());
176}
177
178/// Register an app's locales from `.ftl` sources and set the current locale from (1) the `DAY_LOCALE`
179/// launch override, (2) the platform's [`set_launch_locale`] hint, (3) `default`. Call once, before
180/// building the root piece. The built-in core catalog is preserved (and remains the fallback); the
181/// app's strings take precedence over it, and re-registering reuses the existing locale [`Signal`]
182/// so bindings created earlier keep working.
183pub fn install(default: &str, locales: &[(&str, &str)]) {
184    ensure_state();
185    STATE.with(|s| {
186        let mut st = s.borrow_mut();
187        let st = st.as_mut().unwrap();
188        st.app = build_bundles(locales);
189        st.default = default.to_string();
190        // Accept the first candidate that resolves (exactly, sans `-u-…` extension, or by
191        // language half, `fr-FR` → `fr`), mirroring `message_from`'s lookup, so a regional or
192        // extension-carrying candidate isn't silently dropped to the default.
193        // The app's catalogs decide the app's language. The core catalog ships more languages
194        // than most apps translate into, and letting it answer would hand a German phone an app
195        // whose own text is English and whose framework strings are German, a mix no user asked
196        // for. Core decides only when the app registered nothing at all.
197        let serves = |l: &str| {
198            let lang = l.split('-').next().unwrap_or(l);
199            let keys = [l, base_locale(l), lang];
200            let catalogs = if st.app.is_empty() { &st.core } else { &st.app };
201            keys.iter().any(|k| catalogs.contains_key(*k)) || is_pseudo(l)
202        };
203        let candidates = std::env::var("DAY_LOCALE")
204            .ok()
205            .map(|l| vec![l])
206            .unwrap_or_else(|| LAUNCH_LOCALES.with(|l| l.borrow().clone()));
207        let initial = candidates
208            .into_iter()
209            .map(normalize)
210            .find(|l| serves(l))
211            .unwrap_or_else(|| default.to_string());
212        st.locale.set(initial);
213    });
214}
215
216fn normalize(l: String) -> String {
217    // fr_FR / fr-FR → try exact, else the language half.
218    l.replace('_', "-")
219}
220
221/// The locale string without any `-u-…` Unicode extension: `"zh-u-co-stroke"` → `"zh"`. Bundle
222/// lookup uses the base (translations don't vary by collation/numbering preferences); the
223/// collator ([`compare_in`]) keeps the full string so extensions still select tailorings.
224fn base_locale(l: &str) -> &str {
225    l.split("-u-").next().unwrap_or(l)
226}
227
228/// The current locale (a tracked read inside bindings).
229pub fn locale() -> Signal<String> {
230    ensure_state();
231    STATE.with(|s| s.borrow().as_ref().unwrap().locale)
232}
233
234/// Switch the locale at runtime; every `tr`/binding re-runs.
235pub fn set_locale(l: &str) {
236    locale().set(normalize(l.to_string()));
237}
238
239#[derive(Clone)]
240pub enum FArg {
241    Str(String),
242    Num(f64),
243    SigStr(Signal<String>),
244    SigI64(Signal<i64>),
245    SigF64(Signal<f64>),
246}
247
248impl FArg {
249    /// Tracked resolution (signal args subscribe the binding).
250    fn resolve(&self) -> FluentValue<'static> {
251        match self {
252            FArg::Str(s) => FluentValue::from(s.clone()),
253            FArg::Num(n) => FluentValue::from(*n),
254            FArg::SigStr(s) => FluentValue::from(s.get()),
255            FArg::SigI64(s) => FluentValue::from(s.get()),
256            FArg::SigF64(s) => FluentValue::from(s.get()),
257        }
258    }
259}
260
261/// Disjoint-marker conversion for `.arg` values (the same E0119 dodge as `IntoText`).
262pub trait IntoFArg<M> {
263    fn into_farg(self) -> FArg;
264}
265pub struct ValM;
266pub struct SigM;
267impl IntoFArg<ValM> for &str {
268    fn into_farg(self) -> FArg {
269        FArg::Str(self.to_owned())
270    }
271}
272impl IntoFArg<ValM> for String {
273    fn into_farg(self) -> FArg {
274        FArg::Str(self)
275    }
276}
277impl IntoFArg<ValM> for i64 {
278    fn into_farg(self) -> FArg {
279        FArg::Num(self as f64)
280    }
281}
282impl IntoFArg<ValM> for f64 {
283    fn into_farg(self) -> FArg {
284        FArg::Num(self)
285    }
286}
287impl IntoFArg<SigM> for Signal<String> {
288    fn into_farg(self) -> FArg {
289        FArg::SigStr(self)
290    }
291}
292impl IntoFArg<SigM> for Signal<i64> {
293    fn into_farg(self) -> FArg {
294        FArg::SigI64(self)
295    }
296}
297impl IntoFArg<SigM> for Signal<f64> {
298    fn into_farg(self) -> FArg {
299        FArg::SigF64(self)
300    }
301}
302
303/// Marker for [`IntoFArg`] values that carry a **number**, required for a Fluent variable used as a
304/// plural / `select` selector, where CLDR plural rules select on a number (so a string can't be
305/// passed there by mistake). Implemented for the numeric `IntoFArg` types only (`i64`, `f64`, and
306/// their `Signal`s); the generated `res::str::<key>(…)` functions (§18.5) type such parameters as
307/// `impl IntoNumberFArg` instead of `impl IntoFArg`.
308pub trait IntoNumberFArg<M>: IntoFArg<M> {}
309impl IntoNumberFArg<ValM> for i64 {}
310impl IntoNumberFArg<ValM> for f64 {}
311impl IntoNumberFArg<SigM> for Signal<i64> {}
312impl IntoNumberFArg<SigM> for Signal<f64> {}
313
314/// Resolve `key` in the given locale from one bundle map (exact locale, then the language half).
315fn message_from(
316    map: &HashMap<String, FluentBundle<FluentResource>>,
317    locale_name: &str,
318    key: &str,
319    args: &FluentArgs,
320) -> Option<String> {
321    let bundle = map
322        .get(locale_name)
323        .or_else(|| map.get(base_locale(locale_name)))
324        .or_else(|| {
325            let lang = locale_name.split('-').next().unwrap_or(locale_name);
326            map.get(lang)
327        })
328        .or_else(|| {
329            // Apple uses script tags while stores commonly use region tags.
330            let base = base_locale(locale_name);
331            let alias = match base {
332                "zh-Hans" | "zh-Hans-CN" | "zh-SG" => "zh-CN",
333                "zh-Hant" | "zh-Hant-TW" => "zh-TW",
334                _ => return None,
335            };
336            map.get(alias)
337        })?;
338    // `message.attribute` reaches a Fluent attribute (a message id itself can never contain
339    // a dot, so the split is unambiguous). This is how localized keyboard-shortcut keys resolve
340    // (`menu_group.key`, docs/localization.md): the attribute lives beside its command's
341    // label, and a locale that omits it falls through this chain to the default's.
342    let (msg_key, attr) = match key.split_once('.') {
343        Some((m, a)) => (m, Some(a)),
344        None => (key, None),
345    };
346    let msg = bundle.get_message(msg_key)?;
347    let pattern = match attr {
348        Some(a) => msg.get_attribute(a)?.value(),
349        None => msg.value()?,
350    };
351    let mut errs = Vec::new();
352    Some(
353        bundle
354            .format_pattern(pattern, Some(args), &mut errs)
355            .into_owned(),
356    )
357}
358
359/// Make a catalog available for qualified scripting lookups. Generated accessors call this
360/// automatically. Registration never changes the app locale or its default catalog.
361pub fn register_catalog(catalog: &'static Catalog) {
362    ensure_state();
363    STATE.with(|s| {
364        s.borrow_mut()
365            .as_mut()
366            .unwrap()
367            .scoped
368            .entry(catalog as *const Catalog as usize)
369            .or_insert_with(|| ScopedBundles {
370                catalog,
371                bundles: build_bundles(catalog.locales),
372            });
373    });
374}
375
376/// Format within one private namespace; missing keys fall back only to this catalog's default.
377/// App/core catalogs cannot accidentally supply a similarly named key from another component.
378pub fn format_catalog(
379    catalog: &'static Catalog,
380    locale_name: &str,
381    key: &str,
382    args: &[(String, FArg)],
383) -> String {
384    register_catalog(catalog);
385    STATE.with(|s| {
386        let st = s.borrow();
387        let scope = &st.as_ref().unwrap().scoped[&(catalog as *const Catalog as usize)];
388        resolve_scoped(scope, locale_name, key, args)
389    })
390}
391
392fn resolve_scoped(
393    scope: &ScopedBundles,
394    locale_name: &str,
395    key: &str,
396    args: &[(String, FArg)],
397) -> String {
398    let mut fargs = FluentArgs::new();
399    for (k, v) in args {
400        fargs.set(k.clone(), v.resolve());
401    }
402    let base = locale_name.strip_suffix(PSEUDO_SUFFIX);
403    let lookup = base.unwrap_or(locale_name);
404    match message_from(&scope.bundles, lookup, key, &fargs)
405        .or_else(|| message_from(&scope.bundles, scope.catalog.default, key, &fargs))
406    {
407        Some(out) if base.is_some() => pseudolocalize(&out),
408        Some(out) => out,
409        None => format!("⟨{}::{key}⟩", scope.catalog.name),
410    }
411}
412
413/// Resolve `key`: app bundle for the locale, then app-default, then the built-in core catalog for the
414/// locale, then core English. A pseudo-locale (`en-XA`, `fr-XA`: any locale with the `-XA` suffix)
415/// resolves against the locale under the suffix and accents the result. Signal args are tracked
416/// reads.
417pub fn format_in(locale_name: &str, key: &str, args: &[(String, FArg)]) -> String {
418    ensure_state();
419    STATE.with(|s| {
420        let st = s.borrow();
421        let st = st.as_ref().unwrap();
422        // Qualified keys are explicit: never search unrelated catalogs by a bare key.
423        if let Some((name, local_key)) = key.rsplit_once("::") {
424            let mut matches = st.scoped.values().filter(|s| s.catalog.name == name);
425            if let Some(scope) = matches.next()
426                && matches.next().is_none()
427            {
428                return resolve_scoped(scope, locale_name, local_key, args);
429            }
430            return format!("⟨{key}⟩");
431        }
432        let mut fargs = FluentArgs::new();
433        for (k, v) in args {
434            fargs.set(k.clone(), v.resolve());
435        }
436        let base = locale_name.strip_suffix(PSEUDO_SUFFIX);
437        let pseudo = base.is_some();
438        let lookup = base.unwrap_or(locale_name);
439        let out = message_from(&st.app, lookup, key, &fargs)
440            .or_else(|| message_from(&st.app, &st.default, key, &fargs))
441            .or_else(|| message_from(&st.core, lookup, key, &fargs))
442            .or_else(|| message_from(&st.core, "en", key, &fargs));
443        match out {
444            Some(out) => {
445                if pseudo {
446                    pseudolocalize(&out)
447                } else {
448                    out
449                }
450            }
451            None => format!("⟨{key}⟩"),
452        }
453    })
454}
455
456/// Resolve `key` in the current locale (no args): the one-shot form the framework's own strings use
457/// (dialog buttons, menu-role labels), which are resolved once at present/build time. The key is
458/// always a generated name, `t(key::day_cancel)`, never a string literal.
459pub fn t(key: &str) -> String {
460    format_in(&locale().get(), key, &[])
461}
462
463/// The suffix that turns any locale into its pseudo-locale (`fr` → `fr-XA`): the strings of the
464/// locale underneath, accented and expanded (docs/localization.md).
465const PSEUDO_SUFFIX: &str = "-XA";
466
467/// Whether `l` is a pseudo-locale.
468fn is_pseudo(l: &str) -> bool {
469    l.ends_with(PSEUDO_SUFFIX)
470}
471
472/// Accent + expansion transform for layout-stress testing (the `-XA` pseudo-locales).
473fn pseudolocalize(s: &str) -> String {
474    let mut out: String = s
475        .chars()
476        .map(|c| match c {
477            'a' => 'á',
478            'e' => 'é',
479            'i' => 'í',
480            'o' => 'ó',
481            'u' => 'ú',
482            'y' => 'ý',
483            'A' => 'Á',
484            'E' => 'É',
485            'I' => 'Í',
486            'O' => 'Ó',
487            'U' => 'Ú',
488            'Y' => 'Ý',
489            other => other,
490        })
491        .collect();
492    out.push_str(" ・ロング");
493    out
494}
495
496/// Strip Fluent's FSI/PDI isolation marks (dayscript text comparison, §14.3).
497pub fn strip_isolates(s: &str) -> String {
498    s.chars()
499        .filter(|c| !matches!(c, '\u{2068}' | '\u{2069}'))
500        .collect()
501}
502
503#[cfg(test)]
504mod launch_locale_tests {
505    use super::*;
506
507    /// The device answers with a list and its first entry is often regional. Both facts have to
508    /// survive: `fr-CA` must reach an app that ships only `fr`, and a language the app does not
509    /// have at all must fall through to the next preference rather than to the default.
510    #[test]
511    fn install_picks_the_first_preference_it_can_serve() {
512        set_launch_locales(&[
513            "de-DE".to_string(), // not shipped
514            "fr-CA".to_string(), // shipped as `fr`
515            "en".to_string(),
516        ]);
517        install(
518            "en",
519            &[("en", "hello = Hello\n"), ("fr", "hello = Bonjour\n")],
520        );
521        assert_eq!(locale().get(), "fr-CA");
522        assert_eq!(
523            t("hello"),
524            "Bonjour",
525            "and it resolves through the language half"
526        );
527    }
528
529    /// An explicit host override stays in front of whatever the OS reports.
530    #[test]
531    fn an_explicit_override_outranks_the_device() {
532        set_launch_locales(&[]);
533        set_launch_locale("fr");
534        add_launch_locales(&["en-US".to_string()]);
535        install(
536            "en",
537            &[("en", "hello = Hello\n"), ("fr", "hello = Bonjour\n")],
538        );
539        assert_eq!(t("hello"), "Bonjour");
540    }
541}
542
543#[cfg(test)]
544mod tests {
545    use super::*;
546
547    #[test]
548    fn core_catalog_resolves_without_install_and_across_locales() {
549        // Works before any `install`; the core catalog self-initializes.
550        assert_eq!(
551            strip_isolates(&format_in("en", key::day_cancel, &[])),
552            "Cancel"
553        );
554        assert_eq!(
555            strip_isolates(&format_in("fr", key::day_cancel, &[])),
556            "Annuler"
557        );
558        assert_eq!(
559            strip_isolates(&format_in("de", key::day_cancel, &[])),
560            "Abbrechen"
561        );
562        assert_eq!(strip_isolates(&format_in("ja", key::day_ok, &[])), "OK");
563        // Unknown locale → English core fallback.
564        assert_eq!(strip_isolates(&format_in("xx", key::day_copy, &[])), "Copy");
565        // Unknown key → visible marker, never a panic.
566        assert_eq!(format_in("en", "nope", &[]), "⟨nope⟩");
567    }
568
569    #[test]
570    fn shortcut_key_attributes_resolve_and_fall_back_across_locales() {
571        install(
572            "en",
573            &[
574                ("en", "menu_group = Group\n    .key = g\n"),
575                // fr omits the attribute, so the shortcut stays the default locale's, which is
576                // the industry posture (letters stay stable across languages)…
577                ("fr", "menu_group = Grouper\n"),
578                // …while a locale that needs to differ declares it.
579                ("de", "menu_group = Gruppieren\n    .key = r\n"),
580            ],
581        );
582        assert_eq!(strip_isolates(&format_in("en", "menu_group.key", &[])), "g");
583        assert_eq!(
584            strip_isolates(&format_in("fr", "menu_group.key", &[])),
585            "g",
586            "an omitted attribute falls back to the default locale's"
587        );
588        assert_eq!(
589            strip_isolates(&format_in("de", "menu_group.key", &[])),
590            "r",
591            "a locale may deliberately override"
592        );
593        assert_eq!(
594            strip_isolates(&format_in("fr", "menu_group", &[])),
595            "Grouper",
596            "the message value is untouched"
597        );
598        assert_eq!(format_in("en", "menu_group.nope", &[]), "⟨menu_group.nope⟩");
599        install("en", &[]); // leave the thread's shared state clean for other tests
600    }
601
602    #[test]
603    fn menu_role_labels_and_interpolated_app_commands_localize() {
604        let app = |lang, key, val: &str| {
605            let out = format_in(lang, key, &[("app".to_string(), FArg::Str("Day".into()))]);
606            assert_eq!(strip_isolates(&out), val, "{lang}/{key}");
607        };
608        // Standard menu-command labels (used by day-pieces `lower_menu` for role items).
609        assert_eq!(
610            strip_isolates(&format_in("fr", key::day_cut, &[])),
611            "Couper"
612        );
613        assert_eq!(
614            strip_isolates(&format_in("de", key::day_paste, &[])),
615            "Einfügen"
616        );
617        assert_eq!(
618            strip_isolates(&format_in("es", key::day_select_all, &[])),
619            "Seleccionar todo"
620        );
621        // Interpolated App-menu commands with per-language word order (AppKit About/Quit).
622        app("en", key::day_quit_app, "Quit Day");
623        app("fr", key::day_quit_app, "Quitter Day");
624        app("de", key::day_quit_app, "Day beenden");
625        app("ja", key::day_quit_app, "Dayを終了");
626        app("fr", key::day_about_app, "À propos de Day");
627    }
628
629    #[test]
630    fn app_bundle_overrides_core_but_core_is_the_fallback() {
631        install("en", &[("en", "day_cancel = Dismiss\ngreeting = Hi")]);
632        // App key wins for its own strings.
633        assert_eq!(strip_isolates(&format_in("en", "greeting", &[])), "Hi");
634        // App override of a core key wins.
635        assert_eq!(
636            strip_isolates(&format_in("en", key::day_cancel, &[])),
637            "Dismiss"
638        );
639        // A core key the app didn't define still resolves from the built-in catalog.
640        assert_eq!(strip_isolates(&format_in("en", key::day_ok, &[])), "OK");
641    }
642}
643
644#[cfg(test)]
645mod scoped_tests {
646    use super::*;
647    static A: Catalog = Catalog {
648        name: "game_a",
649        default: "en",
650        locales: &[
651            (
652                "en",
653                "game_title = Alpha\nonly_a = Private\ngreeting = Hello { $name }\nmenu = Menu\n    .key = a\n",
654            ),
655            (
656                "fr",
657                "game_title = Alphabète\ngreeting = Bonjour { $name }\n",
658            ),
659            ("zh-CN", "game_title = 阿尔法\n"),
660        ],
661    };
662    static B: Catalog = Catalog {
663        name: "game_b",
664        default: "en",
665        locales: &[("en", "game_title = Beta\n")],
666    };
667
668    #[test]
669    fn private_keys_do_not_leak_or_depend_on_registration_order() {
670        install(
671            "en",
672            &[("en", "game_title = Application\nonly_a = App impostor\n")],
673        );
674        for catalog in [&B, &A, &B, &A] {
675            let expected = if std::ptr::eq(catalog, &A) {
676                "Alpha"
677            } else {
678                "Beta"
679            };
680            assert_eq!(format_catalog(catalog, "en", "game_title", &[]), expected);
681        }
682        assert_eq!(format_in("en", "game_title", &[]), "Application");
683        assert_eq!(format_catalog(&B, "en", "only_a", &[]), "⟨game_b::only_a⟩");
684        assert_eq!(
685            format_catalog(&B, "en", key::day_ok, &[]),
686            "⟨game_b::day_ok⟩"
687        );
688        install("en", &[]);
689        assert_eq!(format_catalog(&A, "en", "game_title", &[]), "Alpha");
690    }
691
692    #[test]
693    fn scope_fallback_attributes_pseudo_and_regional_locales() {
694        assert_eq!(
695            format_catalog(&A, "fr-CA-u-nu-latn", "game_title", &[]),
696            "Alphabète"
697        );
698        assert_eq!(format_catalog(&A, "fr", "only_a", &[]), "Private");
699        assert_eq!(format_catalog(&A, "xx", "game_title", &[]), "Alpha");
700        assert_eq!(format_catalog(&A, "fr", "menu.key", &[]), "a");
701        assert_eq!(
702            format_catalog(&A, "fr-XA", "game_title", &[]),
703            pseudolocalize("Alphabète")
704        );
705        for locale in ["zh-CN", "zh-Hans", "zh-Hans-CN", "zh-SG"] {
706            assert_eq!(format_catalog(&A, locale, "game_title", &[]), "阿尔法");
707        }
708    }
709
710    #[test]
711    fn qualified_scripts_are_explicit_and_ambiguous_names_fail_closed() {
712        register_catalog(&A);
713        register_catalog(&B);
714        assert_eq!(format_in("en", "game_a::game_title", &[]), "Alpha");
715        assert_eq!(format_in("en", "game_b::game_title", &[]), "Beta");
716        assert_eq!(
717            format_in("en", "absent::game_title", &[]),
718            "⟨absent::game_title⟩"
719        );
720        static DUPLICATE: Catalog = Catalog {
721            name: "game_a",
722            default: "en",
723            locales: &[("en", "game_title = Other version\n")],
724        };
725        register_catalog(&DUPLICATE);
726        assert_eq!(
727            format_in("en", "game_a::game_title", &[]),
728            "⟨game_a::game_title⟩"
729        );
730        assert_eq!(format_catalog(&A, "en", "game_title", &[]), "Alpha");
731        assert_eq!(
732            format_catalog(&DUPLICATE, "en", "game_title", &[]),
733            "Other version"
734        );
735    }
736
737    #[test]
738    fn shared_locale_and_signal_arguments_react_without_reinstalling_catalogs() {
739        use std::rc::Rc;
740        set_locale("en");
741        let name = Signal::new("Ada".to_string());
742        let observed = Rc::new(RefCell::new(Vec::new()));
743        let values = observed.clone();
744        let scope = day_reactive::Scope::child();
745        scope.enter(|| {
746            day_reactive::Effect::new(move || {
747                values.borrow_mut().push(strip_isolates(&format_catalog(
748                    &A,
749                    &locale().get(),
750                    "greeting",
751                    &[("name".into(), FArg::SigStr(name))],
752                )));
753            });
754        });
755        day_reactive::batch(|| set_locale("fr"));
756        day_reactive::batch(|| name.set("Grace".to_string()));
757        assert_eq!(
758            *observed.borrow(),
759            ["Hello Ada", "Bonjour Ada", "Bonjour Grace"]
760        );
761        scope.dispose();
762    }
763}