-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.rs
60 lines (50 loc) · 1.77 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
const I18N_DIR: &str = "i18n";
// https://github.com/rust-lang/cargo/issues/5457
const TARGET_DIR: &str = "target";
fn main() {
generate_translation_files();
}
fn generate_translation_files() {
let mut dest_path = PathBuf::from(TARGET_DIR);
dest_path.push("locale");
println!("Creating directory: {:?}", &dest_path);
let _ = fs::remove_dir_all(&dest_path);
fs::create_dir_all(&dest_path).expect("Could not create translations directory");
println!("cargo:rerun-if-changed={I18N_DIR}");
let existing_iter = fs::read_dir(I18N_DIR)
.unwrap()
.filter(|x| x.as_ref().unwrap().path().extension().unwrap() == "po");
for existing_file in existing_iter {
let file = existing_file.unwrap();
generate_mo(&file.path(), &dest_path);
}
}
fn generate_mo(src_path: &Path, dest_path: &Path) {
let mut file_dest_path = dest_path.to_path_buf();
file_dest_path.push(src_path.file_stem().unwrap());
file_dest_path.push("LC_MESSAGES");
fs::create_dir_all(&file_dest_path).expect("Could not create translations directory");
file_dest_path.push("tundra.mo");
print!(
"Generating .mo file for {:?} to {} ",
&src_path.display(),
file_dest_path.display(),
);
let res = Command::new("msgfmt")
.arg(format!("--output-file={}", file_dest_path.display()))
.arg(src_path.to_str().unwrap())
.output()
.expect("Could not execute msgfmt");
if res.status.success() {
println!("success");
} else {
panic!(
"msgfmt exited with a non-zero status code {}: {:?}",
res.status, res
);
}
println!("cargo:rerun-if-changed={}", src_path.display());
}