winload 0.1.8-rc.18

Network Load Monitor — nload-like TUI tool for Windows/Linux/macOS
//! i18n — Internationalization support
//! Supported languages: en-us, zh-cn, zh-tw

use std::sync::atomic::{AtomicU8, Ordering};

/// Display language
#[derive(Clone, Copy, Debug, PartialEq, Eq, clap::ValueEnum)]
pub enum Lang {
    /// English (United States)
    #[value(name = "en-us")]
    EnUs,
    /// 简体中文
    #[value(name = "zh-cn")]
    ZhCn,
    /// 繁體中文
    #[value(name = "zh-tw")]
    ZhTw,
}

static LANG: AtomicU8 = AtomicU8::new(0);

pub fn set_lang(lang: Lang) {
    LANG.store(lang as u8, Ordering::Relaxed);
}

pub fn get_lang() -> Lang {
    match LANG.load(Ordering::Relaxed) {
        1 => Lang::ZhCn,
        2 => Lang::ZhTw,
        _ => Lang::EnUs,
    }
}

/// Look up a translated string by key. Falls back to en-us.
pub fn t(key: &str) -> &'static str {
    match get_lang() {
        Lang::EnUs => t_en_us(key),
        Lang::ZhCn => t_zh_cn(key),
        Lang::ZhTw => t_zh_tw(key),
    }
}

// ─── English (en-us) ───────────────────────────────────────

fn t_en_us(key: &str) -> &'static str {
    match key {
        // -- CLI help --
        "description" => "Network Load Monitor \u{2014} nload-like TUI tool for Windows/Linux/macOS",
        "help_interval" => "Refresh interval in milliseconds",
        "help_average" => "Average window in seconds",
        "help_device" => "Default device name (partial match)",
        "help_debug_info" => "Print debug info about network interfaces and exit",
        "help_emoji" => "Enable emoji decorations in TUI and output",
        "help_unicode" => "Use Unicode block characters for graph (\u{2588}\u{2593}\u{2591}\u{00b7} instead of #|..)",
        "help_unit" => "Display unit: bit (default) or byte",
        "help_bar_style" => "Bar style for header/label/help: fill (default), color, plain",
        "help_in_color" => "Incoming (download) graph color, hex RGB (e.g. 0x00d7ff). Default: cyan",
        "help_out_color" => "Outgoing (upload) graph color, hex RGB (e.g. 0xffaf00). Default: gold",
        "help_max" => "Fixed graph Y-axis max (e.g. 100M, 1G, 500K). Default: auto-scale",
        "help_no_graph" => "Hide traffic graphs, show only statistics",
        "help_hide_separator" => "Hide separator line (the row of equals signs between header and panels)",
        "help_no_color" => "Disable all TUI colors (monochrome mode). Press 'c' to toggle at runtime",
        "help_npcap" => "[Windows only] Use Npcap to capture loopback traffic (recommended)\nRequires Npcap installed: https://npcap.com/#download",
        "help_smart_max" => "[Rust only] Smart adaptive Y-axis max: decays after traffic spikes for a more dynamic graph (half-life in seconds, default: 10s)",
        "help_lang" => "Display language: en-us (default), zh-cn, zh-tw",
        // -- TUI --
        "device" => "Device",
        "device_emoji" => "\u{1f5a7} Device",
        "incoming" => "Incoming",
        "incoming_emoji" => "\u{2b07}\u{fe0f}\u{1f4e5} Incoming",
        "outgoing" => "Outgoing",
        "outgoing_emoji" => "\u{2b06}\u{fe0f}\u{1f4e4} Outgoing",
        "stat_curr" => "Curr",
        "stat_avg" => "Avg",
        "stat_min" => "Min",
        "stat_max" => "Max",
        "stat_ttl" => "Ttl",
        "stat_curr_emoji" => "\u{26a1} Curr",
        "stat_avg_emoji" => "\u{1f4ca}  Avg",
        "stat_min_emoji" => "\u{1f4cf}  Min",
        "stat_max_emoji" => "\u{1f680}  Max",
        "stat_ttl_emoji" => "\u{1f4e6}  Ttl",
        "help_bar" => " \u{2190}/\u{2192} Switch Device | F3 Info | q Quit",
        "help_bar_emoji" => " \u{2b05}\u{fe0f}/\u{27a1}\u{fe0f} Switch Device | \u{1f527} F3 Info | \u{1f6aa} q Quit",
        "help_bar_win" => " \u{2190}/\u{2192} Switch Device | F3 Info | q Quit | Loopback: --npcap",
        "help_bar_win_emoji" => " \u{2b05}\u{fe0f}/\u{27a1}\u{fe0f} Switch Device | \u{1f527} F3 Info | \u{1f6aa} q Quit | \u{1f4a1} Loopback: --npcap",
        "f3_help_bar" => " F3 Return | \u{2190}/\u{2192} Switch Device | q Quit",
        "f3_help_bar_emoji" => " \u{1f527} F3 Return | \u{2b05}\u{fe0f}/\u{27a1}\u{fe0f} Switch | \u{1f6aa} q Quit",
        // F3 debug overlay
        "f3_title" => "\u{2550}\u{2550}\u{2550} winload Debug Info (F3) \u{2550}\u{2550}\u{2550}",
        "terminal_too_small" => "Terminal too small!",
        "terminal_too_small_emoji" => "\u{1f62d} Terminal too small! \u{1f4cc}",
        "loopback_warning" => " \u{26a0} Loopback: use --npcap (npcap.com)",
        // -- F3 debug overlay --
        "debug_section_params" => "\u{2550}\u{2550}\u{2550} Parameters \u{2550}\u{2550}\u{2550}",
        "debug_section_yaxis" => "\u{2550}\u{2550}\u{2550} Y-axis Scaling \u{2550}\u{2550}\u{2550}",
        "debug_section_device" => "\u{2550}\u{2550}\u{2550} Device \u{2550}\u{2550}\u{2550}",
        "debug_section_colors" => "\u{2550}\u{2550}\u{2550} Colors \u{2550}\u{2550}\u{2550}",
        "debug_version" => "Version:",
        "debug_system" => "System:",
        "debug_language" => "Language:",
        "debug_interval" => "Interval:",
        "debug_average" => "Average:",
        "debug_unit" => "Unit:",
        "debug_bar_style" => "Bar Style:",
        "debug_emoji" => "Emoji:",
        "debug_unicode" => "Unicode:",
        "debug_no_graph" => "No Graph:",
        "debug_no_color" => "No Color:",
        "debug_hide_sep" => "Hide Sep:",
        "debug_yaxis_mode" => "Mode:",
        "debug_in_smooth" => "In smooth:",
        "debug_out_smooth" => "Out smooth:",
        "debug_device_name" => "Name:",
        "debug_device_addr" => "Address:",
        "debug_in_curr" => "In Curr:",
        "debug_out_curr" => "Out Curr:",
        "debug_in_total" => "In Total:",
        "debug_out_total" => "Out Total:",
        "debug_in_peak" => "In Peak:",
        "debug_out_peak" => "Out Peak:",
        "debug_in_color" => "In Color:",
        "debug_out_color" => "Out Color:",
        "tag_fixed" => "fixed",
        "tag_smart_max" => "smart-max",
        "tag_auto" => "auto",
        "yaxis_auto" => "auto (history peak)",
        "yaxis_fixed" => "fixed-max ({val})",
        "yaxis_smart" => "smart-max (half-life: {sec}s)",
        "on" => "on",
        "off" => "off",
        "default_tag" => "(default)",
        "addr_none" => "(none)",
        "arrow_up" => "\u{2191}",
        "arrow_down" => "\u{2193}",
        _ => "",
    }
}

// ─── Simplified Chinese (zh-cn) ────────────────────────────

fn t_zh_cn(key: &str) -> &'static str {
    match key {
        // -- CLI help --
        "description" => "网络负载监控工具 \u{2014} 仿 Linux nload 的终端网络流量监控工具",
        "help_interval" => "刷新间隔(毫秒)",
        "help_average" => "平均值计算窗口(秒)",
        "help_device" => "默认网卡名称(支持部分匹配)",
        "help_debug_info" => "打印网卡调试信息并退出",
        "help_emoji" => "在 TUI 和输出中启用 emoji 装饰",
        "help_unicode" => "使用 Unicode 块字符绘制图形(\u{2588}\u{2593}\u{2591}\u{00b7} 代替 #|..)",
        "help_unit" => "显示单位:bit(默认)或 byte",
        "help_bar_style" => "状态栏/帮助栏样式:fill(默认),color,plain",
        "help_in_color" => "入站(下载)图形颜色,十六进制 RGB(如 0x00d7ff)。默认:青色",
        "help_out_color" => "出站(上传)图形颜色,十六进制 RGB(如 0xffaf00)。默认:金色",
        "help_max" => "固定图形 Y 轴最大值(如 100M、1G、500K)。默认:自动缩放",
        "help_no_graph" => "隐藏流量图形,仅显示统计信息",
        "help_hide_separator" => "隐藏分隔线(标题和面板之间的等号行)",
        "help_no_color" => "禁用所有 TUI 颜色(单色模式)。运行时按 'c' 切换",
        "help_npcap" => "[仅 Windows] 使用 Npcap 捕获回环流量(推荐)\n需要安装 Npcap:https://npcap.com/#download",
        "help_smart_max" => "[仅 Rust] 智能自适应 Y 轴上限:流量尖峰后图形缩放自动回落,波形更生动(半衰期,秒,默认 10 秒)",
        "help_lang" => "显示语言:en-us(默认),zh-cn,zh-tw",
        // -- TUI --
        "device" => "设备",
        "device_emoji" => "\u{1f5a7} 设备",
        "incoming" => "入站",
        "incoming_emoji" => "\u{2b07}\u{fe0f}\u{1f4e5} 入站",
        "outgoing" => "出站",
        "outgoing_emoji" => "\u{2b06}\u{fe0f}\u{1f4e4} 出站",
        "stat_curr" => "当前",
        "stat_avg" => "平均",
        "stat_min" => "最小",
        "stat_max" => "最大",
        "stat_ttl" => "总计",
        "stat_curr_emoji" => "\u{26a1} 当前",
        "stat_avg_emoji" => "\u{1f4ca} 平均",
        "stat_min_emoji" => "\u{1f4cf} 最小",
        "stat_max_emoji" => "\u{1f680} 最大",
        "stat_ttl_emoji" => "\u{1f4e6} 总计",
        "help_bar" => " \u{2190}/\u{2192} \u{5207}\u{6362}\u{8bbe}\u{5907} | F3 \u{4fe1}\u{606f} | q \u{9000}\u{51fa}",
        "help_bar_emoji" => " \u{2b05}\u{fe0f}/\u{27a1}\u{fe0f} \u{5207}\u{6362}\u{8bbe}\u{5907} | \u{1f527} F3 \u{4fe1}\u{606f} | \u{1f6aa} q \u{9000}\u{51fa}",
        "help_bar_win" => " \u{2190}/\u{2192} \u{5207}\u{6362}\u{8bbe}\u{5907} | F3 \u{4fe1}\u{606f} | q \u{9000}\u{51fa} | \u{56de}\u{73af}: --npcap",
        "help_bar_win_emoji" => " \u{2b05}\u{fe0f}/\u{27a1}\u{fe0f} \u{5207}\u{6362}\u{8bbe}\u{5907} | \u{1f527} F3 \u{4fe1}\u{606f} | \u{1f6aa} q \u{9000}\u{51fa} | \u{1f4a1} \u{56de}\u{73af}: --npcap",
        "f3_help_bar" => " F3 \u{8fd4}\u{56de} | \u{2190}/\u{2192} \u{5207}\u{6362}\u{8bbe}\u{5907} | q \u{9000}\u{51fa}",
        "f3_help_bar_emoji" => " \u{1f527} F3 \u{8fd4}\u{56de} | \u{2b05}\u{fe0f}/\u{27a1}\u{fe0f} \u{5207}\u{6362} | \u{1f6aa} q \u{9000}\u{51fa}",
        // F3 debug overlay
        "f3_title" => "═══ winload \u{8c46}\u{82cf}\u{4fe1}\u{606f} (F3) ═══",
        "terminal_too_small" => "\u{7ec8}\u{7aef}\u{7a97}\u{53e3}\u{592a}\u{5c0f}\u{ff01}",
        "terminal_too_small_emoji" => "\u{1f62d} 终端窗口太小!\u{1f4cc}",
        "loopback_warning" => " \u{26a0} 回环设备:请使用 --npcap (npcap.com)",
        // -- F3 debug overlay --
        "debug_section_params" => "═══ 参数 ═══",
        "debug_section_yaxis" => "═══ Y 轴缩放 ═══",
        "debug_section_device" => "═══ 设备 ═══",
        "debug_section_colors" => "═══ 颜色 ═══",
        "debug_version" => "版本:",
        "debug_system" => "系统:",
        "debug_language" => "语言:",
        "debug_interval" => "刷新间隔:",
        "debug_average" => "平均窗口:",
        "debug_unit" => "单位:",
        "debug_bar_style" => "状态栏样式:",
        "debug_emoji" => "表情:",
        "debug_unicode" => "Unicode:",
        "debug_no_graph" => "隐藏图形:",
        "debug_no_color" => "无颜色:",
        "debug_hide_sep" => "隐藏分隔线:",
        "debug_yaxis_mode" => "模式:",
        "debug_in_smooth" => "入站平滑:",
        "debug_out_smooth" => "出站平滑:",
        "debug_device_name" => "名称:",
        "debug_device_addr" => "地址:",
        "debug_in_curr" => "入站当前:",
        "debug_out_curr" => "出站当前:",
        "debug_in_total" => "入站总计:",
        "debug_out_total" => "出站总计:",
        "debug_in_peak" => "入站峰值:",
        "debug_out_peak" => "出站峰值:",
        "debug_in_color" => "入站颜色:",
        "debug_out_color" => "出站颜色:",
        "tag_fixed" => "固定",
        "tag_smart_max" => "智能",
        "tag_auto" => "自动",
        "yaxis_auto" => "自动(历史峰值)",
        "yaxis_fixed" => "固定最大值({val})",
        "yaxis_smart" => "智能自适应(半衰期:{sec}秒)",
        "on" => "",
        "off" => "",
        "default_tag" => "(默认)",
        "addr_none" => "(无)",
        "arrow_up" => "",
        "arrow_down" => "",
        _ => t_en_us(key),
    }
}

// ─── Traditional Chinese (zh-tw) ───────────────────────────

fn t_zh_tw(key: &str) -> &'static str {
    match key {
        // -- CLI help --
        "description" => "網路負載監控工具 \u{2014} 仿 Linux nload 的終端網路流量監控工具",
        "help_interval" => "重新整理間隔(毫秒)",
        "help_average" => "平均值計算視窗(秒)",
        "help_device" => "預設網路卡名稱(支援部分匹配)",
        "help_debug_info" => "列印網路卡除錯資訊並退出",
        "help_emoji" => "在 TUI 和輸出中啟用 emoji 裝飾",
        "help_unicode" => "使用 Unicode 區塊字元繪製圖形(\u{2588}\u{2593}\u{2591}\u{00b7} 取代 #|..)",
        "help_unit" => "顯示單位:bit(預設)或 byte",
        "help_bar_style" => "狀態列/說明列樣式:fill(預設),color,plain",
        "help_in_color" => "入站(下載)圖形顏色,十六進位 RGB(如 0x00d7ff)。預設:青色",
        "help_out_color" => "出站(上傳)圖形顏色,十六進位 RGB(如 0xffaf00)。預設:金色",
        "help_max" => "固定圖形 Y 軸最大值(如 100M、1G、500K)。預設:自動縮放",
        "help_no_graph" => "隱藏流量圖形,僅顯示統計資訊",
        "help_hide_separator" => "隱藏分隔線(標題和面板之間的等號行)",
        "help_no_color" => "停用所有 TUI 顏色(單色模式)。執行時按 'c' 切換",
        "help_npcap" => "[僅 Windows] 使用 Npcap 擷取回環流量(建議)\n需要安裝 Npcap:https://npcap.com/#download",
        "help_smart_max" => "[僅 Rust] 智慧自適應 Y 軸上限:流量尖峰後圖形縮放自動回落,波形更生動(半衰期,秒,預設 10 秒)",
        "help_lang" => "顯示語言:en-us(預設),zh-cn,zh-tw",
        // -- TUI --
        "device" => "裝置",
        "device_emoji" => "\u{1f5a7} 裝置",
        "incoming" => "入站",
        "incoming_emoji" => "\u{2b07}\u{fe0f}\u{1f4e5} 入站",
        "outgoing" => "出站",
        "outgoing_emoji" => "\u{2b06}\u{fe0f}\u{1f4e4} 出站",
        "stat_curr" => "目前",
        "stat_avg" => "平均",
        "stat_min" => "最小",
        "stat_max" => "最大",
        "stat_ttl" => "總計",
        "stat_curr_emoji" => "\u{26a1} 目前",
        "stat_avg_emoji" => "\u{1f4ca} 平均",
        "stat_min_emoji" => "\u{1f4cf} 最小",
        "stat_max_emoji" => "\u{1f680} 最大",
        "stat_ttl_emoji" => "\u{1f4e6} 總計",
        "help_bar" => " \u{2190}/\u{2192} \u{5207}\u{63db}\u{88dd}\u{7f6e} | F3 \u{8cc7}\u{8a0a} | q \u{9000}\u{51fa}",
        "help_bar_emoji" => " \u{2b05}\u{fe0f}/\u{27a1}\u{fe0f} \u{5207}\u{63db}\u{88dd}\u{7f6e} | \u{1f527} F3 \u{8cc7}\u{8a0a} | \u{1f6aa} q \u{9000}\u{51fa}",
        "help_bar_win" => " \u{2190}/\u{2192} \u{5207}\u{63db}\u{88dd}\u{7f6e} | F3 \u{8cc7}\u{8a0a} | q \u{9000}\u{51fa} | \u{56de}\u{74b0}: --npcap",
        "help_bar_win_emoji" => " \u{2b05}\u{fe0f}/\u{27a1}\u{fe0f} \u{5207}\u{63db}\u{88dd}\u{7f6e} | \u{1f527} F3 \u{8cc7}\u{8a0a} | \u{1f6aa} q \u{9000}\u{51fa} | \u{1f4a1} \u{56de}\u{74b0}: --npcap",
        "f3_help_bar" => " F3 \u{8fd4}\u{56de} | \u{2190}/\u{2192} \u{5207}\u{63db}\u{88dd}\u{7f6e} | q \u{9000}\u{51fa}",
        "f3_help_bar_emoji" => " \u{1f527} F3 \u{8fd4}\u{56de} | \u{2b05}\u{fe0f}/\u{27a1}\u{fe0f} \u{5207}\u{63db} | \u{1f6aa} q \u{9000}\u{51fa}",
        // F3 debug overlay
        "f3_title" => "═══ winload \u{8a0a}\u{8cca}\u{8a18}\u{60c5} (F3) ═══",
        "terminal_too_small" => "\u{7d42}\u{7aef}\u{8996}\u{7a97}\u{592a}\u{5c0f}\u{ff01}",
        "terminal_too_small_emoji" => "\u{1f62d} 終端視窗太小!\u{1f4cc}",
        "loopback_warning" => " \u{26a0} 回環裝置:請使用 --npcap (npcap.com)",
        // -- F3 debug overlay --
        "debug_section_params" => "═══ 參數 ═══",
        "debug_section_yaxis" => "═══ Y 軸縮放 ═══",
        "debug_section_device" => "═══ 裝置 ═══",
        "debug_section_colors" => "═══ 顏色 ═══",
        "debug_version" => "版本:",
        "debug_system" => "系統:",
        "debug_language" => "語言:",
        "debug_interval" => "刷新間隔:",
        "debug_average" => "平均窗口:",
        "debug_unit" => "單位:",
        "debug_bar_style" => "狀態列樣式:",
        "debug_emoji" => "表情:",
        "debug_unicode" => "Unicode:",
        "debug_no_graph" => "隱藏圖形:",
        "debug_no_color" => "無顏色:",
        "debug_hide_sep" => "隱藏分隔線:",
        "debug_yaxis_mode" => "模式:",
        "debug_in_smooth" => "入站平滑:",
        "debug_out_smooth" => "出站平滑:",
        "debug_device_name" => "名稱:",
        "debug_device_addr" => "地址:",
        "debug_in_curr" => "入站目前:",
        "debug_out_curr" => "出站目前:",
        "debug_in_total" => "入站總計:",
        "debug_out_total" => "出站總計:",
        "debug_in_peak" => "入站峰值:",
        "debug_out_peak" => "出站峰值:",
        "debug_in_color" => "入站顏色:",
        "debug_out_color" => "出站顏色:",
        "tag_fixed" => "固定",
        "tag_smart_max" => "智能",
        "tag_auto" => "自動",
        "yaxis_auto" => "自動(歷史峰值)",
        "yaxis_fixed" => "固定最大值({val})",
        "yaxis_smart" => "智能自適應(半衰期:{sec}秒)",
        "on" => "",
        "off" => "",
        "default_tag" => "(預設)",
        "addr_none" => "(無)",
        "arrow_up" => "",
        "arrow_down" => "",
        _ => t_en_us(key),
    }
}