fresh-editor 0.3.5

A lightweight, fast terminal-based text editor with LSP support and TypeScript plugins
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
//! Internationalization (i18n) support for Fresh Editor
//!
//! This module provides locale detection and translation support using rust-i18n.
//! Translations are embedded at compile time from JSON files in the `locales/` directory.
//!
//! # Usage
//!
//! ```rust
//! use rust_i18n::t;
//!
//! // Simple translation
//! let msg = t!("search.no_text");
//!
//! // Translation with interpolation
//! let msg = t!("file.saved_as", path = "/path/to/file");
//! ```

use once_cell::sync::Lazy;
use std::collections::HashMap;
use std::sync::RwLock;

pub mod runtime_backend;

/// Type alias for the nested plugin strings map.
/// Structure: plugin_name -> locale -> key -> translated_string
type PluginStringsMap = HashMap<String, HashMap<String, HashMap<String, String>>>;

static PLUGIN_STRINGS: Lazy<RwLock<PluginStringsMap>> = Lazy::new(|| RwLock::new(HashMap::new()));

/// Register strings for a plugin.
/// format: { "en": { "key": "value" }, "es": { "key": "value" } }
pub fn register_plugin_strings(
    plugin_name: &str,
    strings: HashMap<String, HashMap<String, String>>,
) {
    let mut all_strings = PLUGIN_STRINGS.write().unwrap();
    all_strings.insert(plugin_name.to_string(), strings);
}

/// Translate a string for a plugin using the current locale.
pub fn translate_plugin_string(
    plugin_name: &str,
    key: &str,
    args: &HashMap<String, String>,
) -> String {
    let locale = current_locale();
    let all_strings = PLUGIN_STRINGS.read().unwrap();

    let plugin_map: &HashMap<String, HashMap<String, String>> = match all_strings.get(plugin_name) {
        Some(m) => m,
        None => {
            tracing::debug!(
                "translate_plugin_string: plugin '{}' not found (available: {:?}), returning key '{}'",
                plugin_name,
                all_strings.keys().collect::<Vec<_>>(),
                key
            );
            return key.to_string();
        }
    };

    // Try current locale, then fallback to English
    let lang_map: Option<&HashMap<String, String>> =
        plugin_map.get(&locale).or_else(|| plugin_map.get("en"));

    let template: &String = match lang_map.and_then(|m| m.get(key)) {
        Some(t) => t,
        None => {
            tracing::debug!(
                "translate_plugin_string: key '{}' not found for plugin '{}' (locale='{}', available keys: {:?})",
                key,
                plugin_name,
                locale,
                plugin_map.get(&locale).or_else(|| plugin_map.get("en")).map(|m| m.keys().take(5).collect::<Vec<_>>())
            );
            return key.to_string();
        }
    };

    // Simple interpolation: %{variable}
    let mut result = template.clone();
    for (k, v) in args {
        result = result.replace(&format!("%{{{}}}", k), v);
    }
    result
}

/// Unregister strings for a plugin.
pub fn unregister_plugin_strings(plugin_name: &str) {
    let mut all_strings = PLUGIN_STRINGS.write().unwrap();
    all_strings.remove(plugin_name);
}

/// Initialize i18n with the user's locale preference.
///
/// This should be called early in application startup. It detects the system
/// locale from environment variables and sets it as the active locale.
///
/// # Locale Detection Order
///
/// 1. `LC_ALL` environment variable
/// 2. `LC_MESSAGES` environment variable
/// 3. `LANG` environment variable
/// 4. Falls back to "en" (English) if none are set
pub fn init() {
    let locale = detect_locale().unwrap_or_else(|| "en".to_string());
    rust_i18n::set_locale(&locale);
}

/// Initialize i18n with a specific locale from user configuration.
///
/// If `config_locale` is `Some`, use that locale. Otherwise, detect from environment.
pub fn init_with_config(config_locale: Option<&str>) {
    let locale = if let Some(req_locale) = config_locale {
        // Try to match the requested locale against available ones
        let supported = available_locales();
        let req_lower = req_locale.replace('_', "-").to_lowercase();

        let mut matched = None;
        for &loc in &supported {
            if loc.to_lowercase() == req_lower {
                matched = Some(loc.to_string());
                break;
            }
        }

        matched.unwrap_or_else(|| req_locale.to_string())
    } else {
        detect_locale().unwrap_or_else(|| "en".to_string())
    };

    rust_i18n::set_locale(&locale);
}

/// Detect the user's preferred locale from environment variables.
///
/// Checks `LC_ALL`, `LC_MESSAGES`, and `LANG` in order, parsing the locale
/// string to extract the language code (e.g., "en_US.UTF-8" -> "en").
///
/// This function also attempts to match region-specific locales supported by Fresh,
/// such as "pt-BR" and "zh-CN".
fn detect_locale() -> Option<String> {
    let env_locale = std::env::var("LC_ALL")
        .or_else(|_| std::env::var("LC_MESSAGES"))
        .or_else(|_| std::env::var("LANG"))
        .ok()?;

    if env_locale.is_empty() || env_locale == "C" || env_locale == "POSIX" {
        return None;
    }

    // First, try exact match with supported region-specific locales
    // e.g. "pt_BR.UTF-8" -> "pt-BR"
    let normalized = env_locale.replace('_', "-").to_lowercase();
    let supported = available_locales();

    for &loc in &supported {
        if normalized.starts_with(&loc.to_lowercase()) {
            return Some(loc.to_string());
        }
    }

    // Fall back to primary language code
    // e.g. "en_US.UTF-8" -> "en"
    let lang = env_locale.split(['_', '-', '.']).next()?;
    if lang.is_empty() || lang == "C" || lang == "POSIX" {
        None
    } else {
        Some(lang.to_lowercase())
    }
}

/// Get the currently active locale.
pub fn current_locale() -> String {
    rust_i18n::locale().to_string()
}

/// Set the locale explicitly.
///
/// This can be used to change the locale at runtime, for example from
/// a settings menu or command palette action.
pub fn set_locale(locale: &str) {
    rust_i18n::set_locale(locale);
}

/// Get a list of all available locales.
///
/// These are the locales that have translation files in the `locales/` directory.
pub fn available_locales() -> Vec<&'static str> {
    rust_i18n::available_locales!()
}

/// Get the display name for a locale code.
///
/// Returns a tuple of (English name, Native name) for display in UI.
/// For example: ("German", "Deutsch") for "de".
/// Returns None if the locale is not recognized.
pub fn locale_display_name(locale: &str) -> Option<(&'static str, &'static str)> {
    match locale {
        "cs" => Some(("Czech", "Čeština")),
        "de" => Some(("German", "Deutsch")),
        "en" => Some(("English", "English")),
        "es" => Some(("Spanish", "Español")),
        "fr" => Some(("French", "Français")),
        "it" => Some(("Italian", "Italiano")),
        "ja" => Some(("Japanese", "日本語")),
        "ko" => Some(("Korean", "한국어")),
        "pt-BR" => Some(("Portuguese (Brazil)", "Português (Brasil)")),
        "ru" => Some(("Russian", "Русский")),
        "th" => Some(("Thai", "ไทย")),
        "uk" => Some(("Ukrainian", "Українська")),
        "vi" => Some(("Vietnamese", "Tiếng Việt")),
        "zh-CN" => Some(("Chinese (Simplified)", "简体中文")),
        _ => None,
    }
}

/// Get the translated message for "switched to project".
///
/// This is a helper function for use by the binary crate (main.rs) since
/// the t!() macro doesn't work across crate boundaries.
pub fn switched_to_project_message(path: &str) -> String {
    rust_i18n::t!("file.switched_to_project", path = path).to_string()
}

/// Status line shown after the editor resumes from SIGTSTP / `fg`.
pub fn resumed_after_suspend_message() -> String {
    rust_i18n::t!("status.resumed_after_suspend").to_string()
}

/// Status line shown when the suspend action is invoked on a platform
/// without Unix job control (e.g. Windows).
pub fn suspend_unsupported_message() -> String {
    rust_i18n::t!("status.suspend_unsupported").to_string()
}

/// Translate `key` using the runtime backend, with English fallback.
///
/// Unlike `rust_i18n::t!` (a macro that takes a literal key inside the
/// declaring crate), this helper accepts any runtime string and is
/// usable from `main.rs` and other binary crates.  Missing keys fall
/// back to English; if the key is missing in English too, the key
/// itself is returned (matching `rust_i18n`'s behavior).
pub fn t(key: &str) -> String {
    use rust_i18n::Backend;
    let backend = runtime_backend::RuntimeBackend::new();
    let locale = current_locale();
    if let Some(s) = backend.translate(&locale, key) {
        return s.to_string();
    }
    if let Some(s) = backend.translate("en", key) {
        return s.to_string();
    }
    key.to_string()
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashSet;

    #[test]
    fn test_init_sets_locale() {
        init();
        // Should have some locale set (either detected or fallback)
        let locale = current_locale();
        assert!(!locale.is_empty());
    }

    #[test]
    fn test_set_locale() {
        set_locale("en");
        assert_eq!(current_locale(), "en");
    }

    #[test]
    fn test_locale_changed_interpolation() {
        use rust_i18n::t;
        set_locale("en");

        // Test the locale.changed message interpolation
        // Note: The placeholder is %{locale_name} not %{locale} because "locale"
        // is a reserved parameter in rust_i18n that sets the translation locale.
        let locale_name = "es";
        let msg = t!("locale.changed", locale_name = locale_name).to_string();

        assert_eq!(msg, "Locale changed to es");
    }

    #[test]
    fn test_available_locales_includes_en() {
        let locales = available_locales();
        assert!(
            locales.contains(&"en"),
            "English locale should be available"
        );
    }

    /// Validate that all locale files have the same keys as the English locale.
    /// This ensures translations are complete and no keys are missing.
    #[test]
    fn test_all_locales_have_required_keys() {
        use std::fs;
        use std::path::Path;

        // Read the English locale file as the schema
        let locales_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("locales");
        let en_content =
            fs::read_to_string(locales_dir.join("en.json")).expect("Failed to read en.json");
        let en_json: serde_json::Value =
            serde_json::from_str(&en_content).expect("Failed to parse en.json");

        // Get all keys from English locale (excluding _version)
        let en_keys: HashSet<_> = en_json
            .as_object()
            .expect("en.json should be an object")
            .keys()
            .filter(|k| !k.starts_with('_'))
            .cloned()
            .collect();

        // Check each available locale
        let locales = available_locales();
        assert!(
            locales.len() >= 2,
            "Should have at least 2 locales (en and at least one other)"
        );

        for locale in &locales {
            if *locale == "en" {
                continue; // Skip English, it's the schema
            }

            let locale_file = locales_dir.join(format!("{}.json", locale));
            let content = fs::read_to_string(&locale_file)
                .unwrap_or_else(|_| panic!("Failed to read {}.json", locale));
            let json: serde_json::Value = serde_json::from_str(&content)
                .unwrap_or_else(|_| panic!("Failed to parse {}.json", locale));

            let locale_keys: HashSet<_> = json
                .as_object()
                .unwrap_or_else(|| panic!("{}.json should be an object", locale))
                .keys()
                .filter(|k| !k.starts_with('_'))
                .cloned()
                .collect();

            // Find missing keys
            let missing: Vec<_> = en_keys.difference(&locale_keys).collect();
            if !missing.is_empty() {
                // Sort for consistent error messages
                let mut missing_sorted: Vec<_> = missing.into_iter().collect();
                missing_sorted.sort();
                panic!(
                    "Locale '{}' is missing {} keys: {:?}",
                    locale,
                    missing_sorted.len(),
                    missing_sorted
                );
            }

            // Optionally warn about extra keys (locale has keys not in English)
            let extra: Vec<_> = locale_keys.difference(&en_keys).collect();
            if !extra.is_empty() {
                let mut extra_sorted: Vec<_> = extra.into_iter().collect();
                extra_sorted.sort();
                eprintln!(
                    "Warning: Locale '{}' has {} extra keys not in English: {:?}",
                    locale,
                    extra_sorted.len(),
                    extra_sorted
                );
            }
        }
    }

    #[test]
    fn t_returns_translation_for_active_locale() {
        let saved = current_locale();
        set_locale("ja");
        let s = t("cli.arg.locale");
        // Japanese key should not equal the English value or the raw key
        assert_ne!(s, "cli.arg.locale", "missing key for ja");
        assert!(!s.is_empty());
        set_locale(&saved);
    }

    #[test]
    fn t_falls_back_to_english_when_locale_missing_key() {
        let saved = current_locale();
        // Use a key we know exists in en.json; switch to a locale that
        // (in the test fixture) is missing it — this is a smoke test
        // for the fallback path, so we just ensure no panic and a
        // non-empty result for a known-good key.
        set_locale("en");
        let s = t("cli.arg.locale");
        assert!(!s.is_empty());
        assert_ne!(s, "cli.arg.locale");
        set_locale(&saved);
    }

    #[test]
    fn t_returns_key_for_unknown_lookup() {
        assert_eq!(
            t("cli.does_not_exist.anywhere"),
            "cli.does_not_exist.anywhere"
        );
    }
}