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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
extern crate gettextrs;
extern crate rocket;
extern crate serde_json;
extern crate tera;
use gettextrs::*;
use rocket::{Data, Request, Rocket, fairing::{Fairing, Info, Kind}};
use std::{
collections::HashMap,
env,
fs,
io::{BufRead, BufReader},
path::{Path, PathBuf},
process::Command
};
use tera::{Tera, Error as TeraError};
const ACCEPT_LANG: &'static str = "Accept-Language";
pub struct I18n {
domain: &'static str
}
impl I18n {
pub fn new(domain: &'static str) -> I18n {
I18n {
domain: domain
}
}
}
impl Fairing for I18n {
fn info(&self) -> Info {
Info {
name: "Gettext I18n",
kind: Kind::Attach | Kind::Request
}
}
fn on_attach(&self, rocket: Rocket) -> Result<Rocket, Rocket> {
update_po(self.domain);
compile_po(self.domain);
bindtextdomain(self.domain, fs::canonicalize(&PathBuf::from("./translations/")).unwrap().to_str().unwrap());
textdomain(self.domain);
Ok(rocket)
}
fn on_request(&self, request: &mut Request, _: &Data) {
let lang = request
.headers()
.get_one(ACCEPT_LANG)
.unwrap_or("en")
.split(",")
.nth(0)
.unwrap_or("en");
env::set_var("LANGUAGE", lang);
setlocale(LocaleCategory::LcAll, "");
}
}
fn update_po(domain: &str) {
let pot_path = Path::new("po").join(format!("{}.pot", domain));
for lang in get_locales() {
let po_path = Path::new("po").join(format!("{}.po", lang.clone()));
if po_path.exists() && po_path.is_file() {
println!("Updating {}", lang.clone());
Command::new("msgmerge")
.arg("-U")
.arg(po_path.to_str().unwrap())
.arg(pot_path.to_str().unwrap())
.spawn()
.expect("Couldn't update PO file");
} else {
println!("Creating {}", lang.clone());
Command::new("msginit")
.arg(format!("--input={}", pot_path.to_str().unwrap()))
.arg(format!("--output-file={}", po_path.to_str().unwrap()))
.arg("-l")
.arg(lang)
.arg("--no-translator")
.spawn()
.expect("Couldn't init PO file");
}
}
}
fn compile_po(domain: &str) {
for lang in get_locales() {
let po_path = Path::new("po").join(format!("{}.po", lang.clone()));
let mo_dir = Path::new("translations")
.join(lang.clone())
.join("LC_MESSAGES");
fs::create_dir_all(mo_dir.clone()).expect("Couldn't create MO directory");
let mo_path = mo_dir.join(format!("{}.mo", domain));
Command::new("msgfmt")
.arg(format!("--output-file={}", mo_path.to_str().unwrap()))
.arg(po_path)
.spawn()
.expect("Couldn't compile translations");
}
}
fn get_locales() -> Vec<String> {
let linguas_file = fs::File::open(Path::new("po").join("LINGUAS")).expect("Couldn't find po/LINGUAS file");
let linguas = BufReader::new(&linguas_file);
linguas.lines().map(Result::unwrap).collect()
}
fn tera_gettext(msg: serde_json::Value, ctx: HashMap<String, serde_json::Value>) -> Result<serde_json::Value, TeraError> {
let trans = gettext(msg.as_str().unwrap());
Ok(serde_json::Value::String(Tera::one_off(trans.as_ref(), &ctx, false).unwrap_or(String::from(""))))
}
fn tera_ngettext(msg: serde_json::Value, ctx: HashMap<String, serde_json::Value>) -> Result<serde_json::Value, TeraError> {
let trans = ngettext(
ctx.get("singular").unwrap().as_str().unwrap(),
msg.as_str().unwrap(),
ctx.get("count").unwrap().as_u64().unwrap() as u32
);
Ok(serde_json::Value::String(Tera::one_off(trans.as_ref(), &ctx, false).unwrap_or(String::from(""))))
}
pub fn tera(t: &mut Tera) {
t.register_filter("_", tera_gettext);
t.register_filter("_n", tera_ngettext);
}