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:
parent
978104b518
commit
7313f60d14
31
build.rs
31
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<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()
|
||||
}
|
||||
|
@ -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<Rocket, Rocket> {
|
||||
bindtextdomain(self.domain, "/usr/local/share/locale");
|
||||
bindtextdomain(self.domain, fs::canonicalize(&PathBuf::from("./translations")).unwrap().to_str().unwrap());
|
||||
textdomain(self.domain);
|
||||
Ok(rocket)
|
||||
}
|
||||
|
BIN
translations/en/LC_MESSAGES/plume.mo
Normal file
BIN
translations/en/LC_MESSAGES/plume.mo
Normal file
Binary file not shown.
BIN
translations/fr/LC_MESSAGES/plume.mo
Normal file
BIN
translations/fr/LC_MESSAGES/plume.mo
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user