1pub mod en;
22pub mod ru;
23pub mod zh;
24
25use std::sync::atomic::{AtomicU8, Ordering};
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31pub enum Lang {
32 En = 0,
33 Ru = 1,
34 Zh = 2,
35}
36
37impl Lang {
38 pub fn next(self) -> Self {
40 match self {
41 Self::En => Self::Ru,
42 Self::Ru => Self::Zh,
43 Self::Zh => Self::En,
44 }
45 }
46
47 pub fn label(self) -> &'static str {
49 match self {
50 Self::En => "EN",
51 Self::Ru => "RU",
52 Self::Zh => "ZH",
53 }
54 }
55
56 fn from_u8(v: u8) -> Self {
57 match v {
58 1 => Self::Ru,
59 2 => Self::Zh,
60 _ => Self::En,
61 }
62 }
63}
64
65static LANG: AtomicU8 = AtomicU8::new(0); pub fn set_lang(lang: Lang) {
69 LANG.store(lang as u8, Ordering::Relaxed);
70}
71
72pub fn lang() -> Lang {
74 Lang::from_u8(LANG.load(Ordering::Relaxed))
75}
76
77pub fn strings() -> &'static Strings {
80 match lang() {
81 Lang::En => &en::STRINGS,
82 Lang::Ru => &ru::STRINGS,
83 Lang::Zh => &zh::STRINGS,
84 }
85}
86
87pub fn detect_locale() -> Lang {
89 if let Ok(val) = std::env::var("PRT_LANG") {
90 return parse_lang(&val);
91 }
92
93 if let Some(locale) = sys_locale::get_locale() {
94 let lower = locale.to_lowercase();
95 if lower.starts_with("ru") {
96 return Lang::Ru;
97 }
98 if lower.starts_with("zh") {
99 return Lang::Zh;
100 }
101 }
102
103 Lang::En
104}
105
106pub fn parse_lang(s: &str) -> Lang {
109 match s.to_lowercase().as_str() {
110 "ru" | "russian" => Lang::Ru,
111 "zh" | "cn" | "chinese" => Lang::Zh,
112 _ => Lang::En,
113 }
114}
115
116pub struct Strings {
122 pub app_name: &'static str,
123
124 pub connections: &'static str,
126 pub no_root_warning: &'static str,
127 pub sudo_ok: &'static str,
128 pub filter_label: &'static str,
129 pub search_mode: &'static str,
130
131 pub tab_tree: &'static str,
133 pub tab_network: &'static str,
134 pub tab_connection: &'static str,
135 pub no_selected_process: &'static str,
136
137 pub view_chart: &'static str,
139 pub view_topology: &'static str,
140 pub view_process: &'static str,
141 pub view_namespaces: &'static str,
142
143 pub process_not_found: &'static str,
145
146 pub iface_address: &'static str,
148 pub iface_interface: &'static str,
149 pub iface_protocol: &'static str,
150 pub iface_bind: &'static str,
151 pub iface_localhost_only: &'static str,
152 pub iface_all_interfaces: &'static str,
153 pub iface_specific: &'static str,
154 pub iface_loopback: &'static str,
155 pub iface_all: &'static str,
156
157 pub conn_local: &'static str,
159 pub conn_remote: &'static str,
160 pub conn_state: &'static str,
161 pub conn_process: &'static str,
162 pub conn_cmdline: &'static str,
163
164 pub help_text: &'static str,
166 pub kill_cancel: &'static str,
167 pub copied: &'static str,
168 pub refreshed: &'static str,
169 pub clipboard_unavailable: &'static str,
170 pub scan_error: &'static str,
171 pub cancelled: &'static str,
172 pub lang_switched: &'static str,
173
174 pub sudo_prompt_title: &'static str,
176 pub sudo_password_label: &'static str,
177 pub sudo_confirm_hint: &'static str,
178 pub sudo_failed: &'static str,
179 pub sudo_wrong_password: &'static str,
180 pub sudo_elevated: &'static str,
181
182 pub hint_help: &'static str,
184 pub hint_search: &'static str,
185 pub hint_kill: &'static str,
186 pub hint_sudo: &'static str,
187 pub hint_quit: &'static str,
188 pub hint_lang: &'static str,
189
190 pub hint_back: &'static str,
192 pub hint_details: &'static str,
193 pub hint_views: &'static str,
194 pub hint_sort: &'static str,
195 pub hint_copy: &'static str,
196 pub hint_block: &'static str,
197 pub hint_trace: &'static str,
198 pub hint_navigate: &'static str,
199 pub hint_tabs: &'static str,
200
201 pub forward_prompt_title: &'static str,
203 pub forward_host_label: &'static str,
204 pub forward_confirm_hint: &'static str,
205 pub hint_forward: &'static str,
206
207 pub view_ssh_hosts: &'static str,
209 pub view_tunnels: &'static str,
210
211 pub ssh_col_alias: &'static str,
213 pub ssh_col_target: &'static str,
214 pub ssh_col_source: &'static str,
215 pub ssh_hosts_empty: &'static str,
216 pub ssh_hosts_reloaded: &'static str,
217
218 pub tunnel_col_name: &'static str,
220 pub tunnel_col_kind: &'static str,
221 pub tunnel_col_local: &'static str,
222 pub tunnel_col_remote: &'static str,
223 pub tunnel_col_host: &'static str,
224 pub tunnel_col_status: &'static str,
225 pub tunnel_status_alive: &'static str,
226 pub tunnel_status_dead: &'static str,
227 pub tunnels_empty: &'static str,
228 pub tunnels_saved: &'static str,
229 pub tunnel_killed: &'static str,
230 pub tunnel_restarted: &'static str,
231 pub tunnel_create_failed: &'static str,
232 pub tunnel_kind_local: &'static str,
233 pub tunnel_kind_dynamic: &'static str,
234
235 pub tunnel_form_title: &'static str,
237 pub tunnel_form_kind: &'static str,
238 pub tunnel_form_local_port: &'static str,
239 pub tunnel_form_remote_host: &'static str,
240 pub tunnel_form_remote_port: &'static str,
241 pub tunnel_form_host_alias: &'static str,
242 pub tunnel_form_hint: &'static str,
243 pub tunnel_form_invalid: &'static str,
244
245 pub hint_ssh_hosts: &'static str,
247 pub hint_tunnels: &'static str,
248 pub hint_new_tunnel: &'static str,
249 pub hint_kill_tunnel: &'static str,
250 pub hint_restart_tunnel: &'static str,
251 pub hint_save_tunnels: &'static str,
252 pub hint_reload: &'static str,
253 pub hint_open_tunnel: &'static str,
254
255 pub help_title: &'static str,
257}
258
259impl Strings {
260 pub fn fmt_connections(&self, n: usize) -> String {
261 format!("{n} {}", self.connections)
262 }
263
264 pub fn fmt_kill_confirm(&self, name: &str, pid: u32) -> String {
265 match lang() {
266 Lang::En => format!("Kill {name} (pid {pid})?"),
267 Lang::Ru => format!("Завершить {name} (pid {pid})?"),
268 Lang::Zh => format!("终止 {name} (pid {pid})?"),
269 }
270 }
271
272 pub fn fmt_kill_sent(&self, sig: &str, name: &str, pid: u32) -> String {
273 match lang() {
274 Lang::En => format!("sent {sig} to {name} (pid {pid})"),
275 Lang::Ru => format!("отправлен {sig} → {name} (pid {pid})"),
276 Lang::Zh => format!("已发送 {sig} → {name} (pid {pid})"),
277 }
278 }
279
280 pub fn fmt_kill_failed(&self, err: &str) -> String {
281 match lang() {
282 Lang::En => format!("kill failed: {err}"),
283 Lang::Ru => format!("ошибка завершения: {err}"),
284 Lang::Zh => format!("终止失败: {err}"),
285 }
286 }
287
288 pub fn fmt_scan_error(&self, err: &str) -> String {
289 format!("{}: {err}", self.scan_error)
290 }
291
292 pub fn fmt_all_ports(&self, n: usize) -> String {
293 match lang() {
294 Lang::En => format!("--- All ports of process ({n}) ---"),
295 Lang::Ru => format!("--- Все порты процесса ({n}) ---"),
296 Lang::Zh => format!("--- 进程所有端口 ({n}) ---"),
297 }
298 }
299
300 pub fn fmt_sudo_error(&self, err: &str) -> String {
301 match lang() {
302 Lang::En => format!("sudo: {err}"),
303 Lang::Ru => format!("sudo ошибка: {err}"),
304 Lang::Zh => format!("sudo 错误: {err}"),
305 }
306 }
307}
308
309#[cfg(test)]
310mod tests {
311 use super::*;
312
313 #[test]
314 fn lang_next_cycles_all() {
315 let cases = [
316 (Lang::En, Lang::Ru),
317 (Lang::Ru, Lang::Zh),
318 (Lang::Zh, Lang::En),
319 ];
320 for (from, expected) in cases {
321 assert_eq!(from.next(), expected, "{:?}.next()", from);
322 }
323 }
324
325 #[test]
326 fn lang_next_full_cycle() {
327 let start = Lang::En;
328 let after_3 = start.next().next().next();
329 assert_eq!(after_3, start);
330 }
331
332 #[test]
333 fn lang_label() {
334 let cases = [(Lang::En, "EN"), (Lang::Ru, "RU"), (Lang::Zh, "ZH")];
335 for (lang, expected) in cases {
336 assert_eq!(lang.label(), expected);
337 }
338 }
339
340 #[test]
341 fn lang_from_u8_table() {
342 let cases = [
343 (0, Lang::En),
344 (1, Lang::Ru),
345 (2, Lang::Zh),
346 (99, Lang::En),
347 (255, Lang::En),
348 ];
349 for (val, expected) in cases {
350 assert_eq!(Lang::from_u8(val), expected, "from_u8({val})");
351 }
352 }
353
354 #[test]
355 fn parse_lang_table() {
356 let cases = [
357 ("en", Lang::En),
358 ("ru", Lang::Ru),
359 ("russian", Lang::Ru),
360 ("zh", Lang::Zh),
361 ("cn", Lang::Zh),
362 ("chinese", Lang::Zh),
363 ("EN", Lang::En),
364 ("RU", Lang::Ru),
365 ("ZH", Lang::Zh),
366 ("unknown", Lang::En),
367 ("", Lang::En),
368 ("fr", Lang::En),
369 ];
370 for (input, expected) in cases {
371 assert_eq!(parse_lang(input), expected, "parse_lang({input:?})");
372 }
373 }
374
375 #[test]
376 fn set_and_get_lang() {
377 set_lang(Lang::Ru);
378 assert_eq!(lang(), Lang::Ru);
379 set_lang(Lang::Zh);
380 assert_eq!(lang(), Lang::Zh);
381 set_lang(Lang::En);
382 assert_eq!(lang(), Lang::En);
383 }
384
385 #[test]
386 fn strings_returns_correct_lang() {
387 set_lang(Lang::En);
388 assert_eq!(strings().app_name, "PRT");
389 set_lang(Lang::Ru);
390 assert_eq!(strings().app_name, "PRT");
391 assert_eq!(strings().hint_quit, "выход");
393 set_lang(Lang::En);
394 assert_eq!(strings().hint_quit, "quit");
395 }
396
397 #[test]
398 fn strings_all_languages_have_non_empty_fields() {
399 for l in [Lang::En, Lang::Ru, Lang::Zh] {
400 set_lang(l);
401 let s = strings();
402 assert!(!s.app_name.is_empty(), "{:?} app_name empty", l);
403 assert!(!s.connections.is_empty(), "{:?} connections empty", l);
404 assert!(!s.help_text.is_empty(), "{:?} help_text empty", l);
405 assert!(!s.hint_help.is_empty(), "{:?} hint_help empty", l);
406 assert!(!s.hint_lang.is_empty(), "{:?} hint_lang empty", l);
407 assert!(!s.lang_switched.is_empty(), "{:?} lang_switched empty", l);
408 }
409 set_lang(Lang::En); }
411
412 #[test]
413 fn fmt_connections_contains_count() {
414 set_lang(Lang::En);
415 let s = strings();
416 assert!(s.fmt_connections(42).contains("42"));
417 }
418
419 #[test]
420 fn fmt_kill_confirm_contains_name_and_pid() {
421 for l in [Lang::En, Lang::Ru, Lang::Zh] {
422 set_lang(l);
423 let s = strings();
424 let msg = s.fmt_kill_confirm("nginx", 1234);
425 assert!(msg.contains("nginx"), "{:?}: {msg}", l);
426 assert!(msg.contains("1234"), "{:?}: {msg}", l);
427 }
428 set_lang(Lang::En);
429 }
430}