diff --git a/build.rs b/build.rs index 25997695..f85f7cec 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 6a655593..96e73dfa 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 00000000..27705383 Binary files /dev/null and b/translations/en/LC_MESSAGES/plume.mo differ diff --git a/translations/fr/LC_MESSAGES/plume.mo b/translations/fr/LC_MESSAGES/plume.mo new file mode 100644 index 00000000..ca4fa888 Binary files /dev/null and b/translations/fr/LC_MESSAGES/plume.mo differ