rust
Elnu 2 years ago
parent 76e359ea89
commit 245fcbcf1e

@ -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()))
}

@ -3,9 +3,9 @@ extern crate rocket;
use poise::serenity_prelude::Http;
use rocket::fs::{relative, FileServer};
use rocket_dyn_templates::{Template, tera};
use rocket_dyn_templates::{tera, Template};
use sass_rocket_fairing::SassFairing;
use std::{env, collections::HashMap};
use std::{collections::HashMap, env};
mod models;
use models::Settings;
@ -18,7 +18,7 @@ mod routes;
use routes::*;
mod i18n;
use i18n::{load_catalogs, i18n_filter};
use i18n::{i18n_filter, load_catalogs};
mod prelude;
@ -46,9 +46,12 @@ async fn rocket() -> _ {
.attach(Template::custom(|engines| {
use tera::Value;
let catalogs = load_catalogs().unwrap();
engines.tera.register_filter("i18n", move |value: &Value, args: &HashMap<String, Value>| {
i18n_filter(value, args, &catalogs)
})
engines.tera.register_filter(
"i18n",
move |value: &Value, args: &HashMap<String, Value>| {
i18n_filter(value, args, &catalogs)
},
)
}))
.attach(SassFairing::default())
}
}

@ -54,4 +54,4 @@ impl Guild {
self.name = Some(server.name);
Ok(())
}
}
}

@ -6,7 +6,7 @@ mod tests;
use chrono::Utc;
use derive_more::From;
use reqwest::StatusCode;
use rocket::http::{CookieJar, Cookie};
use rocket::http::{Cookie, CookieJar};
use serial::*;
use serde::Deserialize;
@ -113,4 +113,4 @@ pub enum GetUserError {
fn parse_cookie_value<T: std::str::FromStr>(cookies: &CookieJar<'_>, name: &str) -> Option<T> {
cookies.get_private(name)?.value().parse().ok()
}
}

@ -3,10 +3,14 @@ use std::ops::Deref;
use rocket::{http::CookieJar, State};
use rocket_dyn_templates::{context, Template};
use crate::models::{Challenge, User, Settings};
use crate::models::{Challenge, Settings, User};
#[get("/<challenge>")]
pub async fn get_challenge(challenge: u32, cookies: &CookieJar<'_>, settings: &State<Settings>) -> Template {
pub async fn get_challenge(
challenge: u32,
cookies: &CookieJar<'_>,
settings: &State<Settings>,
) -> Template {
Template::render(
"index",
context! {

Loading…
Cancel
Save