woocraft 0.4.5

GPUI components lib for Woocraft design system.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use std::{
  collections::HashMap,
  ops::Deref,
  sync::{LazyLock, RwLock},
};

pub const SUPPORTED_LOCALES: [&str; 4] = ["zh-hans", "zh-hant", "en-us", "ja-jp"];
pub const WOOCRAFT_I18N_DOMAIN: &str = "tech.woooo.woocraft";

type LocaleTranslations = HashMap<String, String>;
type CustomLocaleStore = HashMap<String, LocaleTranslations>;

static CUSTOM_LOCALES: LazyLock<RwLock<CustomLocaleStore>> =
  LazyLock::new(|| RwLock::new(HashMap::new()));

fn is_woocraft_domain_key(key: &str) -> bool {
  key == WOOCRAFT_I18N_DOMAIN
    || key
      .strip_prefix(WOOCRAFT_I18N_DOMAIN)
      .is_some_and(|rest| rest.starts_with('.'))
}

pub fn woocraft_key(key: impl AsRef<str>) -> String {
  let key = key.as_ref();
  if is_woocraft_domain_key(key) {
    key.to_string()
  } else {
    format!("{WOOCRAFT_I18N_DOMAIN}.{key}")
  }
}

fn normalize_known_locale(locale: &str) -> Option<&'static str> {
  if locale == "zh"
    || locale.starts_with("zh-hans")
    || locale.starts_with("zh-cn")
    || locale.starts_with("zh-sg")
  {
    Some("zh-hans")
  } else if locale.starts_with("zh-hant")
    || locale.starts_with("zh-tw")
    || locale.starts_with("zh-hk")
    || locale.starts_with("zh-mo")
  {
    Some("zh-hant")
  } else if locale == "ja"
    || locale == "jp"
    || locale.starts_with("ja-")
    || locale.starts_with("jp-")
  {
    Some("ja-jp")
  } else if locale == "en" || locale.starts_with("en-us") {
    Some("en-us")
  } else {
    None
  }
}

pub fn normalize_locale(locale: &str) -> String {
  let mut normalized = locale.trim().to_ascii_lowercase().replace('_', "-");

  if let Some((prefix, _)) = normalized.split_once('.') {
    normalized = prefix.to_string();
  }

  if let Some((prefix, _)) = normalized.split_once('@') {
    normalized = prefix.to_string();
  }

  if normalized.is_empty() {
    return "en-us".to_string();
  }

  if let Some(mapped) = normalize_known_locale(&normalized) {
    return mapped.to_string();
  }

  normalized
}

pub fn init() {
  let locale = std::env::var("LC_ALL")
    .ok()
    .or_else(|| std::env::var("LANG").ok())
    .unwrap_or_else(|| "en-us".to_string());

  set_locale(&locale);
}

#[inline]
pub fn locale() -> impl Deref<Target = str> {
  rust_i18n::locale()
}

#[inline]
pub fn set_locale(locale: &str) {
  let locale = normalize_locale(locale);
  rust_i18n::set_locale(&locale);
}

pub fn available_locales() -> Vec<String> {
  let mut locales = SUPPORTED_LOCALES
    .iter()
    .map(|locale| locale.to_string())
    .collect::<Vec<_>>();

  for locale in rust_i18n::available_locales!() {
    let locale = normalize_locale(locale);
    if !locales.iter().any(|existing| existing == &locale) {
      locales.push(locale);
    }
  }

  let custom_locales = CUSTOM_LOCALES
    .read()
    .unwrap_or_else(|poisoned| poisoned.into_inner());
  let mut custom_locales = custom_locales.keys().cloned().collect::<Vec<_>>();
  custom_locales.sort();

  for locale in custom_locales {
    if !locales.iter().any(|existing| existing == &locale) {
      locales.push(locale);
    }
  }

  locales
}

pub fn load_locale(locale: impl AsRef<str>, translations: HashMap<String, String>) {
  let locale = normalize_locale(locale.as_ref());
  let translations = translations
    .into_iter()
    .filter(|(key, _)| !is_woocraft_domain_key(key))
    .collect();

  CUSTOM_LOCALES
    .write()
    .unwrap_or_else(|poisoned| poisoned.into_inner())
    .insert(locale, translations);
}

pub fn extend_locale<I, K, V>(locale: impl AsRef<str>, translations: I)
where
  I: IntoIterator<Item = (K, V)>,
  K: Into<String>,
  V: Into<String>, {
  let locale = normalize_locale(locale.as_ref());
  let mut custom_locales = CUSTOM_LOCALES
    .write()
    .unwrap_or_else(|poisoned| poisoned.into_inner());
  let locale_translations = custom_locales.entry(locale).or_default();

  for (key, value) in translations {
    let key = key.into();
    if !is_woocraft_domain_key(&key) {
      locale_translations.insert(key, value.into());
    }
  }
}

pub fn try_translate_woocraft_in_locale(
  locale: impl AsRef<str>, key: impl AsRef<str>,
) -> Option<String> {
  let locale = normalize_locale(locale.as_ref());
  let key = woocraft_key(key);
  lookup_rust_i18n_translation_merged(&locale, &key)
}

pub fn try_translate_woocraft(key: impl AsRef<str>) -> Option<String> {
  let locale = locale();
  try_translate_woocraft_in_locale(&*locale, key)
}

pub fn translate_woocraft_in_locale(locale: impl AsRef<str>, key: impl AsRef<str>) -> String {
  let locale = normalize_locale(locale.as_ref());
  let key = woocraft_key(key);

  lookup_rust_i18n_translation_merged(&locale, &key)
    .unwrap_or_else(|| crate::_rust_i18n_translate(&locale, &key).into_owned())
}

pub fn translate_woocraft(key: impl AsRef<str>) -> String {
  let locale = locale();
  translate_woocraft_in_locale(&*locale, key)
}

pub fn try_translate_in_locale(locale: impl AsRef<str>, key: impl AsRef<str>) -> Option<String> {
  let locale = normalize_locale(locale.as_ref());
  let key = key.as_ref();

  // First, try to find the translation in the custom locale chain
  if let Some(value) = lookup_custom_translation_merged(&locale, key) {
    return Some(value);
  }

  // If not found in custom or rust_i18n fallbacks, try rust_i18n directly
  crate::_rust_i18n_try_translate(&locale, key).map(|value| value.into_owned())
}

pub fn try_translate(key: impl AsRef<str>) -> Option<String> {
  let locale = locale();
  try_translate_in_locale(&*locale, key)
}

pub fn translate_in_locale(locale: impl AsRef<str>, key: impl AsRef<str>) -> String {
  let locale = normalize_locale(locale.as_ref());
  let key = key.as_ref();

  // First, try to find the translation in the custom locale chain with rust_i18n
  // fallback
  if let Some(value) = lookup_custom_translation_merged(&locale, key) {
    return value;
  }

  // This should not be reached, as rust_i18n should always return something (the
  // key itself)
  crate::_rust_i18n_translate(&locale, key).into_owned()
}

pub fn translate(key: impl AsRef<str>) -> String {
  let locale = locale();
  translate_in_locale(&*locale, key)
}

pub fn locale_display_name(locale: impl AsRef<str>) -> String {
  let locale = normalize_locale(locale.as_ref());
  let builtin_key = woocraft_key("i18n.name");

  // First try the built-in woocraft domain key
  if let Some(name) = lookup_rust_i18n_translation_merged(&locale, &builtin_key) {
    return name;
  }

  // Then try user-defined locale display names in custom locales
  if let Some(name) = lookup_custom_translation_merged(&locale, "i18n.name") {
    return name;
  }

  // If the locale has custom translations registered, use the locale code as
  // fallback
  let custom_locales = CUSTOM_LOCALES
    .read()
    .unwrap_or_else(|poisoned| poisoned.into_inner());
  if custom_locales.contains_key(&locale) {
    return locale;
  }
  drop(custom_locales);

  // Last resort: just return the locale code
  locale
}

/// Look up a translation with proper merging of custom and rust_i18n
/// translations.
///
/// This function implements a merged lookup strategy:
/// 1. Check custom locale chain first
/// 2. If not found, check rust_i18n for the same locale
/// 3. If found in rust_i18n, return it
/// 4. If not found, continue with fallback locale from rust_i18n
/// 5. This ensures that incomplete user translations don't hide built-in
///    translations
fn lookup_custom_translation_merged(locale: &str, key: &str) -> Option<String> {
  if is_woocraft_domain_key(key) {
    return lookup_rust_i18n_translation_merged(locale, key);
  }

  let custom_locales = CUSTOM_LOCALES
    .read()
    .unwrap_or_else(|poisoned| poisoned.into_inner());
  let mut current_locale = Some(locale.to_string());

  while let Some(locale_str) = current_locale {
    // First check if this locale has custom translations
    if let Some(translations) = custom_locales.get(&locale_str)
      && let Some(value) = translations.get(key)
    {
      return Some(value.clone());
    }

    if let Some(value) = crate::_rust_i18n_try_translate(&locale_str, key) {
      return Some(value.into_owned());
    }

    current_locale = crate::_rust_i18n_lookup_fallback(&locale_str).map(|s| s.to_string());
  }

  None
}

fn lookup_rust_i18n_translation_merged(locale: &str, key: &str) -> Option<String> {
  let mut current_locale = Some(locale.to_string());

  while let Some(locale_str) = current_locale {
    if let Some(value) = crate::_rust_i18n_try_translate(&locale_str, key) {
      return Some(value.into_owned());
    }

    current_locale = crate::_rust_i18n_lookup_fallback(&locale_str).map(|s| s.to_string());
  }

  None
}
#[cfg(test)]
mod tests {
  use std::sync::{LazyLock, Mutex, MutexGuard};

  use super::*;

  static TEST_LOCALE_LOCK: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));

  struct LocaleTestGuard {
    _guard: MutexGuard<'static, ()>,
  }

  impl LocaleTestGuard {
    fn new() -> Self {
      let guard = TEST_LOCALE_LOCK
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
      clear_custom_locales();
      Self { _guard: guard }
    }
  }

  impl Drop for LocaleTestGuard {
    fn drop(&mut self) {
      clear_custom_locales();
    }
  }

  fn clear_custom_locales() {
    CUSTOM_LOCALES
      .write()
      .unwrap_or_else(|poisoned| poisoned.into_inner())
      .clear();
  }

  #[test]
  fn test_incomplete_custom_translation_merges_with_builtin() {
    let _guard = LocaleTestGuard::new();

    // Load only a partial Chinese (Simplified) translation
    let mut partial_translations = HashMap::new();
    partial_translations.insert("custom_key".to_string(), "自定义翻译".to_string());
    // Note: NOT adding built-in domain key to simulate incomplete translation

    load_locale("zh-hans", partial_translations);

    // Should return the custom translation for custom_key
    assert_eq!(translate_in_locale("zh-hans", "custom_key"), "自定义翻译");

    // Should Fall back to built-in translation for keys not in custom translation
    // (i18n.name exists in the built-in translation)
    let display_name = locale_display_name("zh-hans");
    assert!(!display_name.is_empty());
    // The display name should be a proper name, not "i18n.name" (the key itself)
    assert_ne!(
      display_name, "i18n.name",
      "Should not return the key itself when merging with built-in translations"
    );
  }

  #[test]
  fn test_custom_translation_priority_over_builtin() {
    let _guard = LocaleTestGuard::new();

    let mut custom_translations = HashMap::new();
    custom_translations.insert("i18n.name".to_string(), "我的自定义语言名".to_string());
    custom_translations.insert("some_key".to_string(), "自定义值".to_string());

    load_locale("test-locale", custom_translations);

    // Custom translation should take priority
    assert_eq!(
      translate_in_locale("test-locale", "i18n.name"),
      "我的自定义语言名"
    );
    assert_eq!(translate_in_locale("test-locale", "some_key"), "自定义值");
  }

  #[test]
  fn test_custom_locale_cannot_override_woocraft_domain_key() {
    let _guard = LocaleTestGuard::new();

    let mut custom_translations = HashMap::new();
    custom_translations.insert(
      woocraft_key("common.loading"),
      "This should be ignored".to_string(),
    );

    load_locale("en-us", custom_translations);

    assert_eq!(
      translate_woocraft_in_locale("en-us", "common.loading"),
      "Loading..."
    );
  }

  #[test]
  fn test_extend_locale_preserves_builtin_translations() {
    let _guard = LocaleTestGuard::new();

    // Clear and extend with just a few keys
    let mut partial_translations = HashMap::new();
    partial_translations.insert("extended_key".to_string(), "扩展翻译".to_string());

    extend_locale("zh-hans", partial_translations);

    // Custom extended translation should be available
    assert_eq!(translate_in_locale("zh-hans", "extended_key"), "扩展翻译");
  }
}