winload 0.1.9-beta.2

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 — 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_title" => "Override header title: no value shows winload <version>; empty string keeps default device header",
        "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 (█▓░· 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" => "🖧 Device",
        "incoming" => "Incoming",
        "incoming_emoji" => "⬇️📥 Incoming",
        "outgoing" => "Outgoing",
        "outgoing_emoji" => "⬆️📤 Outgoing",
        "stat_curr" => "Curr",
        "stat_avg" => "Avg",
        "stat_min" => "Min",
        "stat_max" => "Max",
        "stat_ttl" => "Ttl",
        "stat_curr_emoji" => "⚡ Curr",
        "stat_avg_emoji" => "📊  Avg",
        "stat_min_emoji" => "📏  Min",
        "stat_max_emoji" => "🚀  Max",
        "stat_ttl_emoji" => "📦  Ttl",
        "help_bar" => " ←/→ Switch Device | F3 Info | q Quit",
        "help_bar_emoji" => " ⬅️/➡️ Switch Device | 🔧 F3 Info | 🚪 q Quit",
        "help_bar_win" => " ←/→ Switch Device | F3 Info | q Quit | Loopback: --npcap",
        "help_bar_win_emoji" => " ⬅️/➡️ Switch Device | 🔧 F3 Info | 🚪 q Quit | 💡 Loopback: --npcap",
        "f3_help_bar" => " F3 Return | ←/→ Switch Device | q Quit",
        "f3_help_bar_emoji" => " 🔧 F3 Return | ⬅️/➡️ Switch | 🚪 q Quit",
        // F3 debug overlay
        "f3_title" => "═══ winload Debug Info (F3) ═══",
        "terminal_too_small" => "Terminal too small!",
        "terminal_too_small_emoji" => "😭 Terminal too small! 📌",
        "loopback_warning" => " ⚠ Loopback: use --npcap (npcap.com)",
        // -- F3 debug overlay --
        "debug_section_params" => "═══ Parameters ═══",
        "debug_section_yaxis" => "═══ Y-axis Scaling ═══",
        "debug_section_device" => "═══ Device ═══",
        "debug_section_colors" => "═══ Colors ═══",
        "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" => "",
        "arrow_down" => "",
        _ => "",
    }
}

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

fn t_zh_cn(key: &str) -> &'static str {
    match key {
        // -- CLI help --
        "description" => "网络负载监控工具 — 仿 Linux nload 的终端网络流量监控工具",
        "help_interval" => "刷新间隔(毫秒)",
        "help_average" => "平均值计算窗口(秒)",
        "help_device" => "默认网卡名称(支持部分匹配)",
        "help_title" => "覆盖顶部标题:不带值时显示 winload <版本号>;空字符串保持默认设备标题",
        "help_debug_info" => "打印网卡调试信息并退出",
        "help_emoji" => "在 TUI 和输出中启用 emoji 装饰",
        "help_unicode" => "使用 Unicode 块字符绘制图形(█▓░· 代替 #|..)",
        "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" => "🖧 设备",
        "incoming" => "入站",
        "incoming_emoji" => "⬇️📥 入站",
        "outgoing" => "出站",
        "outgoing_emoji" => "⬆️📤 出站",
        "stat_curr" => "当前",
        "stat_avg" => "平均",
        "stat_min" => "最小",
        "stat_max" => "最大",
        "stat_ttl" => "总计",
        "stat_curr_emoji" => "⚡ 当前",
        "stat_avg_emoji" => "📊 平均",
        "stat_min_emoji" => "📏 最小",
        "stat_max_emoji" => "🚀 最大",
        "stat_ttl_emoji" => "📦 总计",
        "help_bar" => " ←/→ 切换设备 | F3 信息 | q 退出",
        "help_bar_emoji" => " ⬅️/➡️ 切换设备 | 🔧 F3 信息 | 🚪 q 退出",
        "help_bar_win" => " ←/→ 切换设备 | F3 信息 | q 退出 | 回环: --npcap",
        "help_bar_win_emoji" => " ⬅️/➡️ 切换设备 | 🔧 F3 信息 | 🚪 q 退出 | 💡 回环: --npcap",
        "f3_help_bar" => " F3 返回 | ←/→ 切换设备 | q 退出",
        "f3_help_bar_emoji" => " 🔧 F3 返回 | ⬅️/➡️ 切换 | 🚪 q 退出",
        // F3 debug overlay
        "f3_title" => "═══ winload 调试信息 (F3) ═══",
        "terminal_too_small" => "终端窗口太小!",
        "terminal_too_small_emoji" => "😭 终端窗口太小!📌",
        "loopback_warning" => " ⚠ 回环设备:请使用 --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" => "網路負載監控工具 — 仿 Linux nload 的終端網路流量監控工具",
        "help_interval" => "重新整理間隔(毫秒)",
        "help_average" => "平均值計算視窗(秒)",
        "help_device" => "預設網路卡名稱(支援部分匹配)",
        "help_title" => "覆蓋頂部標題:不帶值時顯示 winload <版本號>;空字串保持預設裝置標題",
        "help_debug_info" => "列印網路卡除錯資訊並退出",
        "help_emoji" => "在 TUI 和輸出中啟用 emoji 裝飾",
        "help_unicode" => "使用 Unicode 區塊字元繪製圖形(█▓░· 取代 #|..)",
        "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" => "🖧 裝置",
        "incoming" => "入站",
        "incoming_emoji" => "⬇️📥 入站",
        "outgoing" => "出站",
        "outgoing_emoji" => "⬆️📤 出站",
        "stat_curr" => "目前",
        "stat_avg" => "平均",
        "stat_min" => "最小",
        "stat_max" => "最大",
        "stat_ttl" => "總計",
        "stat_curr_emoji" => "⚡ 目前",
        "stat_avg_emoji" => "📊 平均",
        "stat_min_emoji" => "📏 最小",
        "stat_max_emoji" => "🚀 最大",
        "stat_ttl_emoji" => "📦 總計",
        "help_bar" => " ←/→ 切換裝置 | F3 資訊 | q 退出",
        "help_bar_emoji" => " ⬅️/➡️ 切換裝置 | 🔧 F3 資訊 | 🚪 q 退出",
        "help_bar_win" => " ←/→ 切換裝置 | F3 資訊 | q 退出 | 回環: --npcap",
        "help_bar_win_emoji" => " ⬅️/➡️ 切換裝置 | 🔧 F3 資訊 | 🚪 q 退出 | 💡 回環: --npcap",
        "f3_help_bar" => " F3 返回 | ←/→ 切換裝置 | q 退出",
        "f3_help_bar_emoji" => " 🔧 F3 返回 | ⬅️/➡️ 切換 | 🚪 q 退出",
        // F3 debug overlay
        "f3_title" => "═══ winload 除錯資訊 (F3) ═══",
        "terminal_too_small" => "終端視窗太小!",
        "terminal_too_small_emoji" => "😭 終端視窗太小!📌",
        "loopback_warning" => " ⚠ 回環裝置:請使用 --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),
    }
}