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