sysd_manager_translating/
lib.rs1use std::{fs, io, process::Command};
2pub mod error;
3
4pub const MAIN_PROG: &str = "sysd-manager";
5pub const PO_DIR: &str = "./po";
6
7pub fn xgettext(potfiles_file_path: &str, output_pot_file: &str) {
10 let mut command = Command::new("xgettext");
11
12 for preset in glib_preset() {
13 command.arg(preset);
14 }
15
16 let output = command
17 .arg(format!("--files-from={potfiles_file_path}"))
18 .arg(format!("--output={output_pot_file}"))
19 .arg("--verbose")
20 .output()
21 .unwrap();
22
23 display_output("XGETTEXT", output);
24}
25
26fn display_output(id: &str, output: std::process::Output) {
27 println!("{id}: {:?}", output.status);
28 println!("{id}: {}", String::from_utf8_lossy(&output.stdout));
29 if !output.status.success() {
30 eprintln!("{id}: {}", String::from_utf8_lossy(&output.stderr));
31 }
32}
33
34pub fn msginit(input_pot_file: &str, output_file: &str, lang: &str) {
37 let mut command = Command::new("msginit");
38
39 let output = command
40 .arg(format!("--input={input_pot_file}"))
41 .arg(format!("--output-file={output_file}"))
42 .arg(format!("--locale={lang}"))
43 .output()
44 .expect("command msginit ok");
45
46 display_output("MSGINIT", output);
47}
48
49pub fn msgmerge(input_pot_file: &str, output_file: &str) {
55 let mut command = Command::new("msgmerge");
56
57 let output = command
58 .arg("-o")
59 .arg(output_file)
60 .arg(output_file)
61 .arg(input_pot_file)
62 .arg("--verbose")
63 .output()
64 .unwrap();
65
66 display_output("MSGMERGE", output);
67}
68
69pub fn msgfmt(po_file: &str, lang: &str, domain_name: &str) -> io::Result<()> {
73 let mut command = Command::new("msgfmt");
74
75 let out_dir = format!("target/locale/{lang}/LC_MESSAGES");
76
77 fs::create_dir_all(&out_dir)?;
78
79 let output = command
80 .arg("--check")
81 .arg("--statistics")
82 .arg("--verbose")
83 .arg("-o")
84 .arg(format!("{out_dir}/{domain_name}.mo"))
85 .arg(po_file)
86 .output()
87 .unwrap();
88
89 display_output("MSGFMT", output);
90
91 Ok(())
92}
93
94fn glib_preset() -> Vec<&'static str> {
95 let v = vec![
96 "--from-code=UTF-8",
97 "--add-comments",
98 "--keyword=_",
100 "--keyword=N_",
101 "--keyword=C_:1c,2",
102 "--keyword=NC_:1c,2",
103 "--keyword=g_dcgettext:2",
104 "--keyword=g_dngettext:2,3",
105 "--keyword=g_dpgettext2:2c,3",
106 "--flag=N_:1:pass-c-format",
107 "--flag=C_:2:pass-c-format",
108 "--flag=NC_:2:pass-c-format",
109 "--flag=g_dngettext:2:pass-c-format",
110 "--flag=g_strdup_printf:1:c-format",
111 "--flag=g_string_printf:2:c-format",
112 "--flag=g_string_append_printf:2:c-format",
113 "--flag=g_error_new:3:c-format",
114 "--flag=g_set_error:4:c-format",
115 "--flag=g_markup_printf_escaped:1:c-format",
116 "--flag=g_log:3:c-format",
117 "--flag=g_print:1:c-format",
118 "--flag=g_printerr:1:c-format",
119 "--flag=g_printf:1:c-format",
120 "--flag=g_fprintf:2:c-format",
121 "--flag=g_sprintf:2:c-format",
122 "--flag=g_snprintf:3:c-format",
123 ];
124 v
125}