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.
This commit is contained in:
Bat 2018-06-15 18:43:24 +01:00
parent 978104b518
commit 7313f60d14
4 changed files with 26 additions and 9 deletions

View File

@ -1,4 +1,4 @@
use std::fs::File; use std::fs::{create_dir_all, File};
use std::io::{BufReader, prelude::*}; use std::io::{BufReader, prelude::*};
use std::path::Path; use std::path::Path;
use std::process::Command; use std::process::Command;
@ -6,16 +6,12 @@ use std::process::Command;
fn main() { fn main() {
update_po(); update_po();
compile_po(); compile_po();
install_po();
} }
fn update_po() { fn update_po() {
let pot_path = Path::new("po").join("plume.pot"); 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"); for lang in get_locales() {
let linguas = BufReader::new(&linguas_file);
for lang in linguas.lines() {
let lang = lang.unwrap();
let po_path = Path::new("po").join(format!("{}.po", lang.clone())); let po_path = Path::new("po").join(format!("{}.po", lang.clone()));
if po_path.exists() && po_path.is_file() { if po_path.exists() && po_path.is_file() {
println!("Updating {}", lang.clone()); 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<String> {
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()
}

View File

@ -1,5 +1,7 @@
use gettextrs::*; use gettextrs::*;
use rocket::{Data, Request, Rocket, fairing::{Fairing, Info, Kind}}; use rocket::{Data, Request, Rocket, fairing::{Fairing, Info, Kind}};
use std::fs;
use std::path::PathBuf;
const ACCEPT_LANG: &'static str = "Accept-Language"; const ACCEPT_LANG: &'static str = "Accept-Language";
@ -24,7 +26,7 @@ impl Fairing for I18n {
} }
fn on_attach(&self, rocket: Rocket) -> Result<Rocket, Rocket> { fn on_attach(&self, rocket: Rocket) -> Result<Rocket, Rocket> {
bindtextdomain(self.domain, "/usr/local/share/locale"); bindtextdomain(self.domain, fs::canonicalize(&PathBuf::from("./translations")).unwrap().to_str().unwrap());
textdomain(self.domain); textdomain(self.domain);
Ok(rocket) Ok(rocket)
} }

Binary file not shown.

Binary file not shown.