From 7313f60d1472ea13a98c352fa246da227f13da85 Mon Sep 17 00:00:00 2001 From: Bat Date: Fri, 15 Jun 2018 18:43:24 +0100 Subject: [PATCH] Compile .po files Since cargo is usually not run as root, we can't install .mo files to the standard location (/usr/local/share/locale/), so we install them to ./translations and load them from there. --- build.rs | 31 ++++++++++++++++++++------- src/i18n.rs | 4 +++- translations/en/LC_MESSAGES/plume.mo | Bin 0 -> 459 bytes translations/fr/LC_MESSAGES/plume.mo | Bin 0 -> 394 bytes 4 files changed, 26 insertions(+), 9 deletions(-) create mode 100644 translations/en/LC_MESSAGES/plume.mo create mode 100644 translations/fr/LC_MESSAGES/plume.mo diff --git a/build.rs b/build.rs index 2599769..f85f7ce 100644 --- a/build.rs +++ b/build.rs @@ -1,4 +1,4 @@ -use std::fs::File; +use std::fs::{create_dir_all, File}; use std::io::{BufReader, prelude::*}; use std::path::Path; use std::process::Command; @@ -6,16 +6,12 @@ use std::process::Command; fn main() { update_po(); compile_po(); - install_po(); } fn update_po() { let pot_path = Path::new("po").join("plume.pot"); - let linguas_file = File::open(Path::new("po").join("LINGUAS")).expect("Couldn't find po/LINGUAS file"); - let linguas = BufReader::new(&linguas_file); - for lang in linguas.lines() { - let lang = lang.unwrap(); + 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()); @@ -40,6 +36,25 @@ fn update_po() { } } -fn compile_po() {} +fn compile_po() { + 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"); + create_dir_all(mo_dir.clone()).expect("Couldn't create MO directory"); + let mo_path = mo_dir.join("plume.mo"); -fn install_po() {} + 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 { + let linguas_file = 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() +} diff --git a/src/i18n.rs b/src/i18n.rs index 6a65559..96e73df 100644 --- a/src/i18n.rs +++ b/src/i18n.rs @@ -1,5 +1,7 @@ use gettextrs::*; use rocket::{Data, Request, Rocket, fairing::{Fairing, Info, Kind}}; +use std::fs; +use std::path::PathBuf; const ACCEPT_LANG: &'static str = "Accept-Language"; @@ -24,7 +26,7 @@ impl Fairing for I18n { } fn on_attach(&self, rocket: Rocket) -> Result { - bindtextdomain(self.domain, "/usr/local/share/locale"); + bindtextdomain(self.domain, fs::canonicalize(&PathBuf::from("./translations")).unwrap().to_str().unwrap()); textdomain(self.domain); Ok(rocket) } diff --git a/translations/en/LC_MESSAGES/plume.mo b/translations/en/LC_MESSAGES/plume.mo new file mode 100644 index 0000000000000000000000000000000000000000..27705383d0b74ed386957ce20f0a6a15851e3d46 GIT binary patch literal 459 zcmZvY!Ab)$5QbOL%Yqlr9^yezG#?FGvS|PG9SBJZystB*+Wi{ZKQ)#ijf`U4td(}yvcX|jxnyUI^5QJ1exj_ zh*splda=6xujCw3(REt1`1R(Tw=zQ%D7qVI-VM-#)9mE38=$dngOSO=u@6J2H? zVO3i!R~KkkS;;e?3Gkeep30=K^WS4eKuyN?{%|ntt$!5O0>7)31BFj53+&m!!kre9 z3)Lp+oEr-+8Yg|)TKlS&p25(bO7%>rjFHwSpgxBL)xGe3) zC;jAqpT0Za@9D|Uh;&4p5$D7)aYYQuh>{-fX&pR!mEL_~9bwhL8~UJ~6}&Tji)@3P z^AU=mbOTSl3Q#%0S@}}I+@p+Sg=ZO2@Ntr6kW3-H<>^#RCXn1E2^nF7UwVlA*SD4- zLgl3mMn>laf9#`cDZiG+?6^XUp2{?AEv@RMLa0!-f?H>Ce<%3Xvtm^&4!=mR6E=4? zqNVs8jyIi=+UC6ekUn6Xy;e)e4!)s{EqYkk+BMoL!LxTAS!sGN4J@5+168I0 V(J{=%`7kTm&Mqu};OS+~Mt}A6Z*Kqq literal 0 HcmV?d00001