1use 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
46const 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
58include!(concat!(env!("OUT_DIR"), "/core_strings.rs"));
60
61pub 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: HashMap<String, FluentBundle<FluentResource>>,
79 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 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 intl::register(&mut bundle);
104 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
120fn 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 locale: Signal::global(initial),
141 });
142 });
143}
144
145pub fn set_launch_locale(locale: &str) {
148 set_launch_locales(&[locale.to_string()]);
149}
150
151pub 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
167pub fn set_launch_locales(locales: &[String]) {
174 LAUNCH_LOCALES.with(|l| *l.borrow_mut() = locales.to_vec());
175}
176
177#[must_use]
185pub fn launch_locales() -> Vec<String> {
186 LAUNCH_LOCALES.with(|l| l.borrow().iter().map(|l| normalize(l)).collect())
187}
188
189#[expect(
195 clippy::missing_panics_doc,
196 reason = "`ensure_state` has filled the state the `unwrap` reads, so it cannot fail"
197)]
198pub fn install(default: &str, locales: &[(&str, &str)]) {
199 ensure_state();
200 STATE.with(|s| {
201 let mut st = s.borrow_mut();
202 let st = st.as_mut().unwrap();
203 st.app = build_bundles(locales);
204 st.default = default.to_string();
205 let serves = |l: &str| {
213 let lang = l.split('-').next().unwrap_or(l);
214 let keys = [l, base_locale(l), lang];
215 let catalogs = if st.app.is_empty() { &st.core } else { &st.app };
216 keys.iter().any(|k| catalogs.contains_key(*k)) || is_pseudo(l)
217 };
218 let candidates = std::env::var("DAY_LOCALE")
219 .ok()
220 .map_or_else(|| LAUNCH_LOCALES.with(|l| l.borrow().clone()), |l| vec![l]);
221 let initial = candidates
222 .into_iter()
223 .map(|l| normalize(&l))
224 .find(|l| serves(l))
225 .unwrap_or_else(|| default.to_string());
226 st.locale.set(initial);
227 });
228}
229
230fn normalize(l: &str) -> String {
231 l.replace('_', "-")
233}
234
235fn base_locale(l: &str) -> &str {
239 l.split("-u-").next().unwrap_or(l)
240}
241
242#[must_use]
244#[expect(
245 clippy::missing_panics_doc,
246 reason = "`ensure_state` has filled the state the `unwrap` reads, so it cannot fail"
247)]
248pub fn locale() -> Signal<String> {
249 ensure_state();
250 STATE.with(|s| s.borrow().as_ref().unwrap().locale)
251}
252
253pub fn set_locale(l: &str) {
255 locale().set(normalize(l));
256}
257
258#[derive(Clone)]
259pub enum FArg {
260 Str(String),
261 Num(f64),
262 SigStr(Signal<String>),
263 SigI64(Signal<i64>),
264 SigF64(Signal<f64>),
265}
266
267impl FArg {
268 fn resolve(&self) -> FluentValue<'static> {
270 match self {
271 FArg::Str(s) => FluentValue::from(s.clone()),
272 FArg::Num(n) => FluentValue::from(*n),
273 FArg::SigStr(s) => FluentValue::from(s.get()),
274 FArg::SigI64(s) => FluentValue::from(s.get()),
275 FArg::SigF64(s) => FluentValue::from(s.get()),
276 }
277 }
278}
279
280pub trait IntoFArg<M> {
282 fn into_farg(self) -> FArg;
283}
284pub struct ValM;
285pub struct SigM;
286impl IntoFArg<ValM> for &str {
287 fn into_farg(self) -> FArg {
288 FArg::Str(self.to_owned())
289 }
290}
291impl IntoFArg<ValM> for String {
292 fn into_farg(self) -> FArg {
293 FArg::Str(self)
294 }
295}
296impl IntoFArg<ValM> for i64 {
297 fn into_farg(self) -> FArg {
298 FArg::Num(self as f64)
299 }
300}
301impl IntoFArg<ValM> for f64 {
302 fn into_farg(self) -> FArg {
303 FArg::Num(self)
304 }
305}
306impl IntoFArg<SigM> for Signal<String> {
307 fn into_farg(self) -> FArg {
308 FArg::SigStr(self)
309 }
310}
311impl IntoFArg<SigM> for Signal<i64> {
312 fn into_farg(self) -> FArg {
313 FArg::SigI64(self)
314 }
315}
316impl IntoFArg<SigM> for Signal<f64> {
317 fn into_farg(self) -> FArg {
318 FArg::SigF64(self)
319 }
320}
321
322pub trait IntoNumberFArg<M>: IntoFArg<M> {}
328impl IntoNumberFArg<ValM> for i64 {}
329impl IntoNumberFArg<ValM> for f64 {}
330impl IntoNumberFArg<SigM> for Signal<i64> {}
331impl IntoNumberFArg<SigM> for Signal<f64> {}
332
333fn message_from(
335 map: &HashMap<String, FluentBundle<FluentResource>>,
336 locale_name: &str,
337 key: &str,
338 args: &FluentArgs,
339) -> Option<String> {
340 let bundle = map
341 .get(locale_name)
342 .or_else(|| map.get(base_locale(locale_name)))
343 .or_else(|| {
344 let lang = locale_name.split('-').next().unwrap_or(locale_name);
345 map.get(lang)
346 })
347 .or_else(|| {
348 let base = base_locale(locale_name);
350 let alias = match base {
351 "zh-Hans" | "zh-Hans-CN" | "zh-SG" => "zh-CN",
352 "zh-Hant" | "zh-Hant-TW" => "zh-TW",
353 _ => return None,
354 };
355 map.get(alias)
356 })?;
357 let (msg_key, attr) = match key.split_once('.') {
362 Some((m, a)) => (m, Some(a)),
363 None => (key, None),
364 };
365 let msg = bundle.get_message(msg_key)?;
366 let pattern = match attr {
367 Some(a) => msg.get_attribute(a)?.value(),
368 None => msg.value()?,
369 };
370 let mut errs = Vec::new();
371 Some(
372 bundle
373 .format_pattern(pattern, Some(args), &mut errs)
374 .into_owned(),
375 )
376}
377
378#[expect(
381 clippy::missing_panics_doc,
382 reason = "`ensure_state` has filled the state the `unwrap` reads, so it cannot fail"
383)]
384pub fn register_catalog(catalog: &'static Catalog) {
385 ensure_state();
386 STATE.with(|s| {
387 s.borrow_mut()
388 .as_mut()
389 .unwrap()
390 .scoped
391 .entry(std::ptr::from_ref::<Catalog>(catalog) as usize)
392 .or_insert_with(|| ScopedBundles {
393 catalog,
394 bundles: build_bundles(catalog.locales),
395 });
396 });
397}
398
399#[must_use]
402#[expect(
403 clippy::missing_panics_doc,
404 reason = "`register_catalog` has filled the state and the scope the lookups read"
405)]
406pub fn format_catalog(
407 catalog: &'static Catalog,
408 locale_name: &str,
409 key: &str,
410 args: &[(String, FArg)],
411) -> String {
412 register_catalog(catalog);
413 STATE.with(|s| {
414 let st = s.borrow();
415 let scope =
416 &st.as_ref().unwrap().scoped[&(std::ptr::from_ref::<Catalog>(catalog) as usize)];
417 resolve_scoped(scope, locale_name, key, args)
418 })
419}
420
421fn resolve_scoped(
422 scope: &ScopedBundles,
423 locale_name: &str,
424 key: &str,
425 args: &[(String, FArg)],
426) -> String {
427 let mut fargs = FluentArgs::new();
428 for (k, v) in args {
429 fargs.set(k.clone(), v.resolve());
430 }
431 let base = locale_name.strip_suffix(PSEUDO_SUFFIX);
432 let lookup = base.unwrap_or(locale_name);
433 match message_from(&scope.bundles, lookup, key, &fargs)
434 .or_else(|| message_from(&scope.bundles, scope.catalog.default, key, &fargs))
435 {
436 Some(out) if base.is_some() => pseudolocalize(&out),
437 Some(out) => out,
438 None => format!("⟨{}::{key}⟩", scope.catalog.name),
439 }
440}
441
442#[must_use]
447#[expect(
448 clippy::missing_panics_doc,
449 reason = "`ensure_state` has filled the state the `unwrap` reads, so it cannot fail"
450)]
451pub fn format_in(locale_name: &str, key: &str, args: &[(String, FArg)]) -> String {
452 ensure_state();
453 STATE.with(|s| {
454 let st = s.borrow();
455 let st = st.as_ref().unwrap();
456 if let Some((name, local_key)) = key.rsplit_once("::") {
458 let mut matches = st.scoped.values().filter(|s| s.catalog.name == name);
459 if let Some(scope) = matches.next()
460 && matches.next().is_none()
461 {
462 return resolve_scoped(scope, locale_name, local_key, args);
463 }
464 return format!("⟨{key}⟩");
465 }
466 let mut fargs = FluentArgs::new();
467 for (k, v) in args {
468 fargs.set(k.clone(), v.resolve());
469 }
470 let base = locale_name.strip_suffix(PSEUDO_SUFFIX);
471 let pseudo = base.is_some();
472 let lookup = base.unwrap_or(locale_name);
473 let out = message_from(&st.app, lookup, key, &fargs)
474 .or_else(|| message_from(&st.app, &st.default, key, &fargs))
475 .or_else(|| message_from(&st.core, lookup, key, &fargs))
476 .or_else(|| message_from(&st.core, "en", key, &fargs));
477 match out {
478 Some(out) => {
479 if pseudo {
480 pseudolocalize(&out)
481 } else {
482 out
483 }
484 }
485 None => format!("⟨{key}⟩"),
486 }
487 })
488}
489
490#[must_use]
494pub fn t(key: &str) -> String {
495 format_in(&locale().get(), key, &[])
496}
497
498const PSEUDO_SUFFIX: &str = "-XA";
501
502fn is_pseudo(l: &str) -> bool {
504 l.ends_with(PSEUDO_SUFFIX)
505}
506
507fn pseudolocalize(s: &str) -> String {
509 let mut out: String = s
510 .chars()
511 .map(|c| match c {
512 'a' => 'á',
513 'e' => 'é',
514 'i' => 'í',
515 'o' => 'ó',
516 'u' => 'ú',
517 'y' => 'ý',
518 'A' => 'Á',
519 'E' => 'É',
520 'I' => 'Í',
521 'O' => 'Ó',
522 'U' => 'Ú',
523 'Y' => 'Ý',
524 other => other,
525 })
526 .collect();
527 out.push_str(" ・ロング");
528 out
529}
530
531#[must_use]
533pub fn strip_isolates(s: &str) -> String {
534 s.chars()
535 .filter(|c| !matches!(c, '\u{2068}' | '\u{2069}'))
536 .collect()
537}
538
539#[cfg(test)]
540mod launch_locale_tests {
541 use super::*;
542
543 #[test]
547 fn install_picks_the_first_preference_it_can_serve() {
548 set_launch_locales(&[
549 "de-DE".to_string(), "fr-CA".to_string(), "en".to_string(),
552 ]);
553 install(
554 "en",
555 &[("en", "hello = Hello\n"), ("fr", "hello = Bonjour\n")],
556 );
557 assert_eq!(locale().get(), "fr-CA");
558 assert_eq!(
559 t("hello"),
560 "Bonjour",
561 "and it resolves through the language half"
562 );
563 }
564
565 #[test]
567 fn an_explicit_override_outranks_the_device() {
568 set_launch_locales(&[]);
569 set_launch_locale("fr");
570 add_launch_locales(&["en-US".to_string()]);
571 install(
572 "en",
573 &[("en", "hello = Hello\n"), ("fr", "hello = Bonjour\n")],
574 );
575 assert_eq!(t("hello"), "Bonjour");
576 }
577}
578
579#[cfg(test)]
580mod tests {
581 use super::*;
582
583 #[test]
584 fn core_catalog_resolves_without_install_and_across_locales() {
585 assert_eq!(
587 strip_isolates(&format_in("en", key::day_cancel, &[])),
588 "Cancel"
589 );
590 assert_eq!(
591 strip_isolates(&format_in("fr", key::day_cancel, &[])),
592 "Annuler"
593 );
594 assert_eq!(
595 strip_isolates(&format_in("de", key::day_cancel, &[])),
596 "Abbrechen"
597 );
598 assert_eq!(strip_isolates(&format_in("ja", key::day_ok, &[])), "OK");
599 assert_eq!(strip_isolates(&format_in("xx", key::day_copy, &[])), "Copy");
601 assert_eq!(format_in("en", "nope", &[]), "⟨nope⟩");
603 }
604
605 #[test]
606 fn shortcut_key_attributes_resolve_and_fall_back_across_locales() {
607 install(
608 "en",
609 &[
610 ("en", "menu_group = Group\n .key = g\n"),
611 ("fr", "menu_group = Grouper\n"),
614 ("de", "menu_group = Gruppieren\n .key = r\n"),
616 ],
617 );
618 assert_eq!(strip_isolates(&format_in("en", "menu_group.key", &[])), "g");
619 assert_eq!(
620 strip_isolates(&format_in("fr", "menu_group.key", &[])),
621 "g",
622 "an omitted attribute falls back to the default locale's"
623 );
624 assert_eq!(
625 strip_isolates(&format_in("de", "menu_group.key", &[])),
626 "r",
627 "a locale may deliberately override"
628 );
629 assert_eq!(
630 strip_isolates(&format_in("fr", "menu_group", &[])),
631 "Grouper",
632 "the message value is untouched"
633 );
634 assert_eq!(format_in("en", "menu_group.nope", &[]), "⟨menu_group.nope⟩");
635 install("en", &[]); }
637
638 #[test]
639 fn menu_role_labels_and_interpolated_app_commands_localize() {
640 let app = |lang, key, val: &str| {
641 let out = format_in(lang, key, &[("app".to_string(), FArg::Str("Day".into()))]);
642 assert_eq!(strip_isolates(&out), val, "{lang}/{key}");
643 };
644 assert_eq!(
646 strip_isolates(&format_in("fr", key::day_cut, &[])),
647 "Couper"
648 );
649 assert_eq!(
650 strip_isolates(&format_in("de", key::day_paste, &[])),
651 "Einfügen"
652 );
653 assert_eq!(
654 strip_isolates(&format_in("es", key::day_select_all, &[])),
655 "Seleccionar todo"
656 );
657 app("en", key::day_quit_app, "Quit Day");
659 app("fr", key::day_quit_app, "Quitter Day");
660 app("de", key::day_quit_app, "Day beenden");
661 app("ja", key::day_quit_app, "Dayを終了");
662 app("fr", key::day_about_app, "À propos de Day");
663 }
664
665 #[test]
666 fn app_bundle_overrides_core_but_core_is_the_fallback() {
667 install("en", &[("en", "day_cancel = Dismiss\ngreeting = Hi")]);
668 assert_eq!(strip_isolates(&format_in("en", "greeting", &[])), "Hi");
670 assert_eq!(
672 strip_isolates(&format_in("en", key::day_cancel, &[])),
673 "Dismiss"
674 );
675 assert_eq!(strip_isolates(&format_in("en", key::day_ok, &[])), "OK");
677 }
678}
679
680#[cfg(test)]
681mod scoped_tests {
682 use super::*;
683 static A: Catalog = Catalog {
684 name: "game_a",
685 default: "en",
686 locales: &[
687 (
688 "en",
689 "game_title = Alpha\nonly_a = Private\ngreeting = Hello { $name }\nmenu = Menu\n .key = a\n",
690 ),
691 (
692 "fr",
693 "game_title = Alphabète\ngreeting = Bonjour { $name }\n",
694 ),
695 ("zh-CN", "game_title = 阿尔法\n"),
696 ],
697 };
698 static B: Catalog = Catalog {
699 name: "game_b",
700 default: "en",
701 locales: &[("en", "game_title = Beta\n")],
702 };
703
704 #[test]
705 fn private_keys_do_not_leak_or_depend_on_registration_order() {
706 install(
707 "en",
708 &[("en", "game_title = Application\nonly_a = App impostor\n")],
709 );
710 for catalog in [&B, &A, &B, &A] {
711 let expected = if std::ptr::eq(catalog, &raw const A) {
712 "Alpha"
713 } else {
714 "Beta"
715 };
716 assert_eq!(format_catalog(catalog, "en", "game_title", &[]), expected);
717 }
718 assert_eq!(format_in("en", "game_title", &[]), "Application");
719 assert_eq!(format_catalog(&B, "en", "only_a", &[]), "⟨game_b::only_a⟩");
720 assert_eq!(
721 format_catalog(&B, "en", key::day_ok, &[]),
722 "⟨game_b::day_ok⟩"
723 );
724 install("en", &[]);
725 assert_eq!(format_catalog(&A, "en", "game_title", &[]), "Alpha");
726 }
727
728 #[test]
729 fn scope_fallback_attributes_pseudo_and_regional_locales() {
730 assert_eq!(
731 format_catalog(&A, "fr-CA-u-nu-latn", "game_title", &[]),
732 "Alphabète"
733 );
734 assert_eq!(format_catalog(&A, "fr", "only_a", &[]), "Private");
735 assert_eq!(format_catalog(&A, "xx", "game_title", &[]), "Alpha");
736 assert_eq!(format_catalog(&A, "fr", "menu.key", &[]), "a");
737 assert_eq!(
738 format_catalog(&A, "fr-XA", "game_title", &[]),
739 pseudolocalize("Alphabète")
740 );
741 for locale in ["zh-CN", "zh-Hans", "zh-Hans-CN", "zh-SG"] {
742 assert_eq!(format_catalog(&A, locale, "game_title", &[]), "阿尔法");
743 }
744 }
745
746 #[test]
747 fn qualified_scripts_are_explicit_and_ambiguous_names_fail_closed() {
748 static DUPLICATE: Catalog = Catalog {
749 name: "game_a",
750 default: "en",
751 locales: &[("en", "game_title = Other version\n")],
752 };
753 register_catalog(&A);
754 register_catalog(&B);
755 assert_eq!(format_in("en", "game_a::game_title", &[]), "Alpha");
756 assert_eq!(format_in("en", "game_b::game_title", &[]), "Beta");
757 assert_eq!(
758 format_in("en", "absent::game_title", &[]),
759 "⟨absent::game_title⟩"
760 );
761 register_catalog(&DUPLICATE);
762 assert_eq!(
763 format_in("en", "game_a::game_title", &[]),
764 "⟨game_a::game_title⟩"
765 );
766 assert_eq!(format_catalog(&A, "en", "game_title", &[]), "Alpha");
767 assert_eq!(
768 format_catalog(&DUPLICATE, "en", "game_title", &[]),
769 "Other version"
770 );
771 }
772
773 #[test]
774 fn shared_locale_and_signal_arguments_react_without_reinstalling_catalogs() {
775 use std::rc::Rc;
776 set_locale("en");
777 let name = Signal::new("Ada".to_string());
778 let observed = Rc::new(RefCell::new(Vec::new()));
779 let values = observed.clone();
780 let scope = day_reactive::Scope::child();
781 scope.enter(|| {
782 day_reactive::Effect::new(move || {
783 values.borrow_mut().push(strip_isolates(&format_catalog(
784 &A,
785 &locale().get(),
786 "greeting",
787 &[("name".into(), FArg::SigStr(name))],
788 )));
789 });
790 });
791 day_reactive::batch(|| set_locale("fr"));
792 day_reactive::batch(|| name.set("Grace".to_string()));
793 assert_eq!(
794 *observed.borrow(),
795 ["Hello Ada", "Bonjour Ada", "Bonjour Grace"]
796 );
797 scope.dispose();
798 }
799}