Skip to content

Commit

Permalink
feat: i18n (WIP 8)
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiapple852 committed Sep 7, 2024
1 parent b71ebf9 commit dd55dd4
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 34 deletions.
56 changes: 55 additions & 1 deletion crates/trippy-tui/locales/app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 失败 :(
Expand Down Expand Up @@ -144,4 +147,55 @@ geoip_multiple_data_for_hop:
zh-CN: 多个 GeoIp 位置
kilometer:
en: km
zh-CN: 公里
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:
99 changes: 69 additions & 30 deletions crates/trippy-tui/src/frontend/render/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -30,19 +32,19 @@ 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),
))
})
.collect();
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))
Expand All @@ -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))
Expand Down Expand Up @@ -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))
Expand All @@ -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<SettingsItem>)> {
fn format_all_settings(app: &TuiApp) -> Vec<(String, String, Vec<SettingsItem>)> {
let tui_settings = format_tui_settings(app);
let trace_settings = format_trace_settings(app);
let dns_settings = format_dns_settings(app);
Expand All @@ -133,26 +135,44 @@ fn format_all_settings(app: &TuiApp) -> Vec<(&'static str, String, Vec<SettingsI
let move_up = app.tui_config.bindings.previous_hop_address.to_string();
vec![
(
"Tui",
String::from("Settings which control how data is displayed in this Tui"),
t!("settings_tab_tui_title").to_string(),
t!("settings_tab_tui_desc").to_string(),
tui_settings,
),
(
"Trace",
String::from("Settings which control the tracing strategy"),
t!("settings_tab_trace_title").to_string(),
t!("settings_tab_trace_desc").to_string(),
trace_settings,
),
(
"Dns",
String::from("Settings which control how DNS lookups are performed"),
t!("settings_tab_dns_title").to_string(),
t!("settings_tab_dns_desc").to_string(),
dns_settings,
),
("GeoIp", String::from("Settings relating to GeoIp"), geoip_settings),
("Bindings", String::from("Tui key bindings"), bindings_settings),
("Theme", String::from("Tui theme colors"), theme_settings),
(
"Columns",
format!("Tui table columns. Press [{toggle_column}] to toggle a column on or off and use the [{move_down}] and [{move_up}] keys to change the column order."),
t!("settings_tab_geoip_title").to_string(),
t!("settings_tab_geoip_desc").to_string(),
geoip_settings,
),
(
t!("settings_tab_bindings_title").to_string(),
t!("settings_tab_bindings_desc").to_string(),
bindings_settings,
),
(
t!("settings_tab_theme_title").to_string(),
t!("settings_tab_theme_desc").to_string(),
theme_settings,
),
(
t!("settings_tab_columns_title").to_string(),
t!(
"settings_tab_columns_desc",
c = toggle_column,
d = move_down,
u = move_up
)
.to_string(),
columns_settings,
),
]
Expand Down Expand Up @@ -205,11 +225,15 @@ fn format_trace_settings(app: &TuiApp) -> Vec<SettingsItem> {
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)),
};
Expand Down Expand Up @@ -297,7 +321,7 @@ fn format_geoip_settings(app: &TuiApp) -> Vec<SettingsItem> {
app.tui_config
.geoip_mmdb_file
.as_deref()
.unwrap_or("none")
.unwrap_or("none") // TODO: i18n
.to_string(),
)]
}
Expand Down Expand Up @@ -495,21 +519,31 @@ fn format_columns_settings(app: &TuiApp) -> Vec<SettingsItem> {
.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),
Expand All @@ -531,6 +565,7 @@ impl SettingsItem {
}
}

// TODO: i18n
/// Format the `DnsResolveMethod`.
fn format_dns_method(resolve_method: ResolveMethod) -> String {
match resolve_method {
Expand All @@ -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(),
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions crates/trippy-tui/src/frontend/tui_app.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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
}
}

Expand Down

0 comments on commit dd55dd4

Please sign in to comment.