From dd55dd4f29af9152f6a3364c4838d0a512518e0b Mon Sep 17 00:00:00 2001 From: FujiApple Date: Sat, 7 Sep 2024 18:03:45 +0800 Subject: [PATCH] feat: i18n (WIP 8) --- crates/trippy-tui/locales/app.yml | 56 ++++++++++- .../src/frontend/render/settings.rs | 99 +++++++++++++------ crates/trippy-tui/src/frontend/tui_app.rs | 6 +- 3 files changed, 127 insertions(+), 34 deletions(-) diff --git a/crates/trippy-tui/locales/app.yml b/crates/trippy-tui/locales/app.yml index cb57aaf7..7cd2cda9 100644 --- a/crates/trippy-tui/locales/app.yml +++ b/crates/trippy-tui/locales/app.yml @@ -97,6 +97,9 @@ title_map: title_help: en: Help zh-CN: 帮助 +title_settings: + en: Settings + zh-CN: 设置 bsod_failed: en: Trippy Failed :( zh-CN: Trippy 失败 :( @@ -144,4 +147,55 @@ geoip_multiple_data_for_hop: zh-CN: 多个 GeoIp 位置 kilometer: en: km - zh-CN: 公里 \ No newline at end of file + zh-CN: 公里 +settings_info: + en: Info + zh-CN: 信息 +settings_tab_tui_title: + en: Tui + zh-CN: 终端用户界面 +settings_tab_trace_title: + en: Trace + zh-CN: 跟踪 +settings_tab_dns_title: + en: DNS + zh-CN: 域名系统 +settings_tab_geoip_title: + en: GeoIp + zh-CN: 地理位置 IP +settings_tab_bindings_title: + en: Bindings + zh-CN: 绑定 +settings_tab_theme_title: + en: Theme + zh-CN: 主题 +settings_tab_columns_title: + en: Columns + zh-CN: 列 +settings_tab_tui_desc: + en: Settings which control how data is displayed in this Tui + zh-CN: 终端用户界面设置 +settings_tab_trace_desc: + en: Settings which control the tracing strategy + zh-CN: 跟踪设置 +settings_tab_dns_desc: + en: Settings which control how DNS lookups are performed + zh-CN: 域名系统设置 +settings_tab_geoip_desc: + en: Settings relating to GeoIp + zh-CN: 地理位置 IP 设置 +settings_tab_bindings_desc: + en: Tui key bindings + zh-CN: 终端用户界面键绑定 +settings_tab_theme_desc: + en: Tui theme colors + zh-CN: 终端用户界面主题颜色 +settings_tab_columns_desc: + en: Tui table columns. Press [%{c}] to toggle a column on or off and use the [%{d}] and [%{u}] keys to change the column order. + zh-CN: 终端用户界面表格列。按 [%{c}] 切换列的显示和隐藏,使用 [%{d}] 和 [%{u}] 键更改列的顺序。 +settings_table_header_setting: + en: Setting + zh-CN: 设置 +settings_table_header_value: + en: Value + zh-CN: 值 \ No newline at end of file diff --git a/crates/trippy-tui/src/frontend/render/settings.rs b/crates/trippy-tui/src/frontend/render/settings.rs index b307aa7c..6bde5391 100644 --- a/crates/trippy-tui/src/frontend/render/settings.rs +++ b/crates/trippy-tui/src/frontend/render/settings.rs @@ -10,6 +10,8 @@ use ratatui::widgets::{ Block, BorderType, Borders, Cell, Clear, Paragraph, Row, Table, Tabs, Wrap, }; use ratatui::Frame; +use rust_i18n::t; +use std::string::ToString; use trippy_core::PortDirection; use trippy_dns::ResolveMethod; @@ -30,11 +32,11 @@ pub fn render(f: &mut Frame<'_>, app: &mut TuiApp) { /// Render settings tabs. fn render_settings_tabs(f: &mut Frame<'_>, app: &TuiApp, rect: Rect) { - let titles: Vec<_> = SETTINGS_TABS - .iter() + let titles: Vec<_> = settings_tabs() + .into_iter() .map(|(title, _)| { Line::from(Span::styled( - *title, + title, Style::default().fg(app.tui_config.theme.settings_tab_text), )) }) @@ -42,7 +44,7 @@ fn render_settings_tabs(f: &mut Frame<'_>, app: &TuiApp, rect: Rect) { let tabs = Tabs::new(titles) .block( Block::default() - .title(" Settings ") + .title(format!(" {} ", t!("title_settings"))) .title_alignment(Alignment::Center) .borders(Borders::ALL) .style(Style::default().bg(app.tui_config.theme.settings_dialog_bg)) @@ -62,8 +64,8 @@ fn render_settings_table( name: &str, items: &[SettingsItem], ) { - let header_cells = SETTINGS_TABLE_HEADER.iter().map(|h| { - Cell::from(*h).style(Style::default().fg(app.tui_config.theme.settings_table_header_text)) + let header_cells = settings_table_header().into_iter().map(|h| { + Cell::from(h).style(Style::default().fg(app.tui_config.theme.settings_table_header_text)) }); let header = Row::new(header_cells) .style(Style::default().bg(app.tui_config.theme.settings_table_header_bg)) @@ -109,7 +111,7 @@ fn render_settings_info(f: &mut Frame<'_>, app: &TuiApp, rect: Rect, info: &str) .wrap(Wrap::default()) .block( Block::default() - .title(" Info ") + .title(format!(" {} ", t!("settings_info"))) .title_alignment(Alignment::Center) .borders(Borders::ALL) .style(Style::default().bg(app.tui_config.theme.settings_dialog_bg)) @@ -120,7 +122,7 @@ fn render_settings_info(f: &mut Frame<'_>, app: &TuiApp, rect: Rect, info: &str) } /// Format all settings. -fn format_all_settings(app: &TuiApp) -> Vec<(&'static str, String, Vec)> { +fn format_all_settings(app: &TuiApp) -> Vec<(String, String, Vec)> { let tui_settings = format_tui_settings(app); let trace_settings = format_trace_settings(app); let dns_settings = format_dns_settings(app); @@ -133,26 +135,44 @@ fn format_all_settings(app: &TuiApp) -> Vec<(&'static str, String, Vec Vec { let interface = if let Some(iface) = cfg.data.interface() { iface.to_string() } else { + // TODO: i18n "auto".to_string() }; let (src_port, dst_port) = match cfg.data.port_direction() { + // TODO: i18n PortDirection::None => ("n/a".to_string(), "n/a".to_string()), + // TODO: i18n PortDirection::FixedDest(dst) => ("auto".to_string(), format!("{}", dst.0)), + // TODO: i18n PortDirection::FixedSrc(src) => (format!("{}", src.0), "auto".to_string()), PortDirection::FixedBoth(src, dst) => (format!("{}", src.0), format!("{}", dst.0)), }; @@ -297,7 +321,7 @@ fn format_geoip_settings(app: &TuiApp) -> Vec { app.tui_config .geoip_mmdb_file .as_deref() - .unwrap_or("none") + .unwrap_or("none") // TODO: i18n .to_string(), )] } @@ -495,21 +519,31 @@ fn format_columns_settings(app: &TuiApp) -> Vec { .collect() } +// TODO is wrong! pub const SETTINGS_TAB_COLUMNS: usize = 6; +// TODO refactor this rubbish /// The name and number of items for each tabs in the setting dialog. -pub const SETTINGS_TABS: [(&str, usize); 7] = [ - ("Tui", 9), - ("Trace", 17), - ("Dns", 5), - ("GeoIp", 1), - ("Bindings", 36), - ("Theme", 31), - ("Columns", 0), -]; +pub fn settings_tabs() -> [(String, usize); 7] { + [ + (t!("settings_tab_tui_title").to_string(), 9), + (t!("settings_tab_trace_title").to_string(), 17), + (t!("settings_tab_dns_title").to_string(), 5), + (t!("settings_tab_geoip_title").to_string(), 1), + (t!("settings_tab_bindings_title").to_string(), 36), + (t!("settings_tab_theme_title").to_string(), 31), + (t!("settings_tab_columns_title").to_string(), 0), + ] +} +// TODO rework /// The settings table header. -const SETTINGS_TABLE_HEADER: [&str; 2] = ["Setting", "Value"]; +pub fn settings_table_header() -> [String; 2] { + [ + t!("settings_table_header_setting").to_string(), + t!("settings_table_header_value").to_string(), + ] +} const SETTINGS_TABLE_WIDTH: [Constraint; 3] = [ Constraint::Length(3), @@ -531,6 +565,7 @@ impl SettingsItem { } } +// TODO: i18n /// Format the `DnsResolveMethod`. fn format_dns_method(resolve_method: ResolveMethod) -> String { match resolve_method { @@ -541,6 +576,7 @@ fn format_dns_method(resolve_method: ResolveMethod) -> String { } } +// TODO: i18n fn format_extension_mode(icmp_extension_mode: IcmpExtensionMode) -> String { match icmp_extension_mode { IcmpExtensionMode::Off => "off".to_string(), @@ -550,6 +586,7 @@ fn format_extension_mode(icmp_extension_mode: IcmpExtensionMode) -> String { } } +// TODO: i18n /// Format the `AsMode`. fn format_as_mode(as_mode: AsMode) -> String { match as_mode { @@ -562,6 +599,7 @@ fn format_as_mode(as_mode: AsMode) -> String { } } +// TODO: i18n /// Format the `AddressMode`. fn format_address_mode(address_mode: AddressMode) -> String { match address_mode { @@ -571,6 +609,7 @@ fn format_address_mode(address_mode: AddressMode) -> String { } } +// TODO: i18n /// Format the `GeoIpMode`. fn format_geoip_mode(geoip_mode: GeoIpMode) -> String { match geoip_mode { diff --git a/crates/trippy-tui/src/frontend/tui_app.rs b/crates/trippy-tui/src/frontend/tui_app.rs index 72e1a270..17b59afc 100644 --- a/crates/trippy-tui/src/frontend/tui_app.rs +++ b/crates/trippy-tui/src/frontend/tui_app.rs @@ -1,6 +1,6 @@ use crate::app::TraceInfo; use crate::frontend::config::TuiConfig; -use crate::frontend::render::settings::{SETTINGS_TABS, SETTINGS_TAB_COLUMNS}; +use crate::frontend::render::settings::{settings_tabs, SETTINGS_TAB_COLUMNS}; use crate::geoip::GeoIpLookup; use itertools::Itertools; use ratatui::widgets::TableState; @@ -238,7 +238,7 @@ impl TuiApp { } pub fn next_settings_tab(&mut self) { - if self.settings_tab_selected < SETTINGS_TABS.len() - 1 { + if self.settings_tab_selected < settings_tabs().len() - 1 { self.settings_tab_selected += 1; } self.setting_table_state.select(Some(0)); @@ -286,7 +286,7 @@ impl TuiApp { if self.settings_tab_selected == SETTINGS_TAB_COLUMNS { self.tui_config.tui_columns.all_columns_count() } else { - SETTINGS_TABS[self.settings_tab_selected].1 + settings_tabs()[self.settings_tab_selected].1 } }