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(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 locale: Signal::global(initial),
142 });
143 });
144}
145
146pub fn set_launch_locale(locale: &str) {
149 set_launch_locales(&[locale.to_string()]);
150}
151
152pub 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
168pub fn set_launch_locales(locales: &[String]) {
175 LAUNCH_LOCALES.with(|l| *l.borrow_mut() = locales.to_vec());
176}
177
178pub 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 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 l.replace('_', "-")
219}
220
221fn base_locale(l: &str) -> &str {
225 l.split("-u-").next().unwrap_or(l)
226}
227
228pub fn locale() -> Signal<String> {
230 ensure_state();
231 STATE.with(|s| s.borrow().as_ref().unwrap().locale)
232}
233
234pub 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 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
261pub 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
303pub 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
314fn 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 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 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
359pub 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
376pub 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
413pub 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 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
456pub fn t(key: &str) -> String {
460 format_in(&locale().get(), key, &[])
461}
462
463const PSEUDO_SUFFIX: &str = "-XA";
466
467fn is_pseudo(l: &str) -> bool {
469 l.ends_with(PSEUDO_SUFFIX)
470}
471
472fn 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
496pub 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 #[test]
511 fn install_picks_the_first_preference_it_can_serve() {
512 set_launch_locales(&[
513 "de-DE".to_string(), "fr-CA".to_string(), "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 #[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 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 assert_eq!(strip_isolates(&format_in("xx", key::day_copy, &[])), "Copy");
565 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", "menu_group = Grouper\n"),
578 ("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", &[]); }
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 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 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 assert_eq!(strip_isolates(&format_in("en", "greeting", &[])), "Hi");
634 assert_eq!(
636 strip_isolates(&format_in("en", key::day_cancel, &[])),
637 "Dismiss"
638 );
639 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}