|
|
|
@ -1,6 +1,11 @@
|
|
|
|
|
use std::{collections::HashMap, fs::{self, File}, io::BufReader, process::Command};
|
|
|
|
|
use derive_more::From;
|
|
|
|
|
use rocket_dyn_templates::tera::{self, Value};
|
|
|
|
|
use std::{
|
|
|
|
|
collections::HashMap,
|
|
|
|
|
fs::{self, File},
|
|
|
|
|
io::BufReader,
|
|
|
|
|
process::Command,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use gettext::Catalog;
|
|
|
|
|
|
|
|
|
@ -18,7 +23,12 @@ pub fn load_catalogs() -> Result<HashMap<String, Catalog>, LoadCatalogsError> {
|
|
|
|
|
for file in fs::read_dir("i18n")? {
|
|
|
|
|
let file = file?;
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
let language_code = path
|
|
|
|
@ -33,7 +43,10 @@ pub fn load_catalogs() -> Result<HashMap<String, Catalog>, LoadCatalogsError> {
|
|
|
|
|
.arg("-o")
|
|
|
|
|
.arg(&mo_file_path)
|
|
|
|
|
.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) {
|
|
|
|
|
return Err(LoadCatalogsError::MissingDefaultLanguage);
|
|
|
|
@ -41,15 +54,16 @@ pub fn load_catalogs() -> Result<HashMap<String, Catalog>, LoadCatalogsError> {
|
|
|
|
|
Ok(catalogs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn i18n_filter(value: &Value, _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")
|
|
|
|
|
})?;
|
|
|
|
|
pub fn i18n_filter(
|
|
|
|
|
value: &Value,
|
|
|
|
|
_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
|
|
|
|
|
.get(DEFAULT)
|
|
|
|
|
.expect("Missing catalog")
|
|
|
|
|
.gettext(key);
|
|
|
|
|
let translation = catalogs.get(DEFAULT).expect("Missing catalog").gettext(key);
|
|
|
|
|
|
|
|
|
|
Ok(Value::String(translation.to_owned()))
|
|
|
|
|
}
|
|
|
|
|