euv_ui/hook/i18n/struct.rs
1use super::*;
2
3/// The aggregate i18n state.
4///
5/// Constructed once per app via `App::use_i18n()`, threaded
6/// through any code that needs to render translated text.
7/// Cheap to `Clone` (the internal signal is
8/// `Copy`-by-pointer).
9///
10/// # Storage
11///
12/// Messages are stored in a `Signal<HashMap<String,
13/// HashMap<String, String>>>` keyed by `locale → (key →
14/// message)`. The map is intentionally two-level: it
15/// matches how translation files are typically organized
16/// (`en.json` is one flat key/value map, `zh-CN.json` is
17/// another, etc.) and it lets `add_messages(locale,
18/// &[(k, v), ...])` build the inner map by direct
19/// insertion without the user having to allocate one
20/// `HashMap` per locale.
21/// `I18n` is `Copy` because every field is a `Signal`, which is
22/// already `Copy` — the registry hands out cheap `usize`
23/// addresses for any `T: Clone + PartialEq + 'static`.
24impl Copy for I18n {}
25
26#[derive(Clone, Data, New)]
27pub struct I18n {
28 /// The currently-active locale tag. Setting this
29 /// via `set_locale` triggers a reactive update that
30 /// re-evaluates any reactive `t(...)` read.
31 pub(crate) locale: Signal<String>,
32 /// The locale to fall back to when a key is missing
33 /// in the active locale. Defaults to `"en"`.
34 pub(crate) fallback_locale: Signal<String>,
35 /// The full translation table. The outer key is the
36 /// locale tag, the inner key is the message key, the
37 /// inner value is the (optionally placeholder-
38 /// containing) message.
39 pub(crate) messages: Signal<HashMap<String, HashMap<String, String>>>,
40}