lingxia_platform/i18n.rs
1//! Localized strings for platform-owned UI.
2//!
3//! `lingxia-platform` sits *below* the i18n string table (which lives in
4//! `lingxia-logic`, and `lingxia-logic` depends on this crate), so it cannot
5//! call the table directly. Instead the logic layer installs a translator
6//! hook here once at startup; platform scenes look strings up by stable key,
7//! falling back to their bundled English literal before runtime initialization.
8
9use std::sync::OnceLock;
10
11type Localizer = Box<dyn Fn(&str) -> Option<String> + Send + Sync>;
12
13static LOCALIZER: OnceLock<Localizer> = OnceLock::new();
14
15/// Installs the locale-aware lookup owned by the logic layer. Later calls are
16/// ignored; scenes never supply localized strings per invocation.
17pub fn set_localizer(localizer: impl Fn(&str) -> Option<String> + Send + Sync + 'static) {
18 let _ = LOCALIZER.set(Box::new(localizer));
19}
20
21/// Resolves `key`, falling back when runtime localization is unavailable.
22pub fn text(key: &str, fallback: &str) -> String {
23 LOCALIZER
24 .get()
25 .and_then(|translate| translate(key))
26 .filter(|value| !value.trim().is_empty())
27 .unwrap_or_else(|| fallback.to_string())
28}