|
|
@ -1,6 +1,11 @@
|
|
|
|
use std::{collections::HashMap, fs::{self, File}, io::BufReader, process::Command};
|
|
|
|
|
|
|
|
use derive_more::From;
|
|
|
|
use derive_more::From;
|
|
|
|
use rocket_dyn_templates::tera::{self, Value};
|
|
|
|
use rocket_dyn_templates::tera::{self, Value};
|
|
|
|
|
|
|
|
use std::{
|
|
|
|
|
|
|
|
collections::HashMap,
|
|
|
|
|
|
|
|
fs::{self, File},
|
|
|
|
|
|
|
|
io::BufReader,
|
|
|
|
|
|
|
|
process::Command,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
use gettext::Catalog;
|
|
|
|
use gettext::Catalog;
|
|
|
|
|
|
|
|
|
|
|
@ -18,7 +23,12 @@ pub fn load_catalogs() -> Result<HashMap<String, Catalog>, LoadCatalogsError> {
|
|
|
|
for file in fs::read_dir("i18n")? {
|
|
|
|
for file in fs::read_dir("i18n")? {
|
|
|
|
let file = file?;
|
|
|
|
let file = file?;
|
|
|
|
let path = file.path();
|
|
|
|
let path = file.path();
|
|
|
|
if !file.file_type()?.is_file() || !path.extension().map(|extension| extension.eq("po")).unwrap_or(false) {
|
|
|
|
if !file.file_type()?.is_file()
|
|
|
|
|
|
|
|
|| !path
|
|
|
|
|
|
|
|
.extension()
|
|
|
|
|
|
|
|
.map(|extension| extension.eq("po"))
|
|
|
|
|
|
|
|
.unwrap_or(false)
|
|
|
|
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let language_code = path
|
|
|
|
let language_code = path
|
|
|
@ -33,7 +43,10 @@ pub fn load_catalogs() -> Result<HashMap<String, Catalog>, LoadCatalogsError> {
|
|
|
|
.arg("-o")
|
|
|
|
.arg("-o")
|
|
|
|
.arg(&mo_file_path)
|
|
|
|
.arg(&mo_file_path)
|
|
|
|
.output()?;
|
|
|
|
.output()?;
|
|
|
|
catalogs.insert(language_code, Catalog::parse(BufReader::new(File::open(mo_file_path)?))?);
|
|
|
|
catalogs.insert(
|
|
|
|
|
|
|
|
language_code,
|
|
|
|
|
|
|
|
Catalog::parse(BufReader::new(File::open(mo_file_path)?))?,
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !catalogs.contains_key(DEFAULT) {
|
|
|
|
if !catalogs.contains_key(DEFAULT) {
|
|
|
|
return Err(LoadCatalogsError::MissingDefaultLanguage);
|
|
|
|
return Err(LoadCatalogsError::MissingDefaultLanguage);
|
|
|
@ -41,15 +54,16 @@ pub fn load_catalogs() -> Result<HashMap<String, Catalog>, LoadCatalogsError> {
|
|
|
|
Ok(catalogs)
|
|
|
|
Ok(catalogs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn i18n_filter(value: &Value, _args: &HashMap<String, Value>, catalogs: &HashMap<String, Catalog>) -> tera::Result<Value> {
|
|
|
|
pub fn i18n_filter(
|
|
|
|
let key = value.as_str().ok_or_else(|| {
|
|
|
|
value: &Value,
|
|
|
|
tera::Error::msg("The translation key must be a string")
|
|
|
|
_args: &HashMap<String, Value>,
|
|
|
|
})?;
|
|
|
|
catalogs: &HashMap<String, Catalog>,
|
|
|
|
|
|
|
|
) -> tera::Result<Value> {
|
|
|
|
|
|
|
|
let key = value
|
|
|
|
|
|
|
|
.as_str()
|
|
|
|
|
|
|
|
.ok_or_else(|| tera::Error::msg("The translation key must be a string"))?;
|
|
|
|
|
|
|
|
|
|
|
|
let translation = catalogs
|
|
|
|
let translation = catalogs.get(DEFAULT).expect("Missing catalog").gettext(key);
|
|
|
|
.get(DEFAULT)
|
|
|
|
|
|
|
|
.expect("Missing catalog")
|
|
|
|
|
|
|
|
.gettext(key);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(Value::String(translation.to_owned()))
|
|
|
|
Ok(Value::String(translation.to_owned()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|