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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use std::io::BufRead;
pub fn init(domain: &str, callback: impl Fn(String)) {
let textdomain = match std::env::var("TEXT_DOMAIN") {
Ok(path) => gettextrs::TextDomain::new(domain)
.skip_system_data_paths()
.push(&path)
.init(),
Err(_) => gettextrs::TextDomain::new(domain).init(),
};
match textdomain {
Ok(locale) => match locale {
Some(locale) => callback(locale),
None => eprintln!("Warning: No locale was set! Probably /usr/share/locale/*/LC_MESSAGES does not contain a .mo file.")
},
Err(e) => match e {
gettextrs::TextDomainError::InvalidLocale(locale) => eprintln!("Warning: Invalid locale {:?}", locale),
gettextrs::TextDomainError::TranslationNotFound(locale) => match locale.as_str() {
"en" => callback("en_US.UTF-8".to_string()),
_ => eprintln!("Warning: Could not find messages for locale {:?}", locale)
},
}
};
}
pub fn build_script(domain: &str) {
println!("cargo:rerun-if-changed=src");
let potfiles = format!("po/POTFILES.in");
let target_pot_dir = format!("target/po");
if let Err(e) = std::fs::create_dir_all(target_pot_dir) {
println!("cargo:warning={:?}", e);
}
for line in read_lines("po/LINGUAS").unwrap() {
if let Ok(line) = line {
if !line.starts_with("#") {
let locale = line;
let po_file = format!("po/{}.po", locale);
let pot_file = format!("target/po/{}.pot", locale);
let target_mo_dir = format!("target/locale/{}/LC_MESSAGES", locale);
let target_mo = format!("target/locale/{}/LC_MESSAGES/{}.mo", locale, domain);
if let Err(e) = std::fs::create_dir_all(target_mo_dir) {
println!("cargo:warning={:?}", e);
}
if let Err(e) = std::process::Command::new("xgettext")
.arg("-f")
.arg(&potfiles)
.arg("-o")
.arg(&pot_file)
.output()
{
println!("cargo:warning={:?}", e);
}
if let Err(e) = std::process::Command::new("msgmerge")
.arg(&po_file)
.arg(&pot_file)
.arg("-U")
.output()
{
println!("cargo:warning={:?}", e);
}
if let Err(e) = std::process::Command::new("msgfmt")
.arg("-o")
.arg(&target_mo)
.arg(&po_file)
.output()
{
println!("cargo:warning={:?}", e);
}
}
}
}
}
fn read_lines<P>(filename: P) -> std::io::Result<std::io::Lines<std::io::BufReader<std::fs::File>>>
where
P: AsRef<std::path::Path>,
{
let file = std::fs::File::open(filename)?;
Ok(std::io::BufReader::new(file).lines())
}
#[macro_export]
macro_rules! t {
($s:expr, $($arg:expr),*) => {{
let mut s = $s;
{
let mut i = 0;
$(
s = s.replace(&format!("{{{}}}", i), &format!("{}", $arg));
i += 1;
)*
}
s
}};
}