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#[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 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 l.replace('_', "-")
221}
222
223fn base_locale(l: &str) -> &str {
227 l.split("-u-").next().unwrap_or(l)
228}
229
230#[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
241pub 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 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
268pub 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
310pub 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
321fn 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 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 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#[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#[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#[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 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#[must_use]
482pub fn t(key: &str) -> String {
483 format_in(&locale().get(), key, &[])
484}
485
486const PSEUDO_SUFFIX: &str = "-XA";
489
490fn is_pseudo(l: &str) -> bool {
492 l.ends_with(PSEUDO_SUFFIX)
493}
494
495fn 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#[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 #[test]
535 fn install_picks_the_first_preference_it_can_serve() {
536 set_launch_locales(&[
537 "de-DE".to_string(), "fr-CA".to_string(), "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 #[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 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 assert_eq!(strip_isolates(&format_in("xx", key::day_copy, &[])), "Copy");
589 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", "menu_group = Grouper\n"),
602 ("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", &[]); }
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 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 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 assert_eq!(strip_isolates(&format_in("en", "greeting", &[])), "Hi");
658 assert_eq!(
660 strip_isolates(&format_in("en", key::day_cancel, &[])),
661 "Dismiss"
662 );
663 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}