cargo fmt
This commit is contained in:
parent
76e359ea89
commit
245fcbcf1e
5 changed files with 44 additions and 23 deletions
36
src/i18n.rs
36
src/i18n.rs
|
@ -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()))
|
||||||
}
|
}
|
||||||
|
|
13
src/main.rs
13
src/main.rs
|
@ -3,9 +3,9 @@ extern crate rocket;
|
||||||
|
|
||||||
use poise::serenity_prelude::Http;
|
use poise::serenity_prelude::Http;
|
||||||
use rocket::fs::{relative, FileServer};
|
use rocket::fs::{relative, FileServer};
|
||||||
use rocket_dyn_templates::{Template, tera};
|
use rocket_dyn_templates::{tera, Template};
|
||||||
use sass_rocket_fairing::SassFairing;
|
use sass_rocket_fairing::SassFairing;
|
||||||
use std::{env, collections::HashMap};
|
use std::{collections::HashMap, env};
|
||||||
|
|
||||||
mod models;
|
mod models;
|
||||||
use models::Settings;
|
use models::Settings;
|
||||||
|
@ -18,7 +18,7 @@ mod routes;
|
||||||
use routes::*;
|
use routes::*;
|
||||||
|
|
||||||
mod i18n;
|
mod i18n;
|
||||||
use i18n::{load_catalogs, i18n_filter};
|
use i18n::{i18n_filter, load_catalogs};
|
||||||
|
|
||||||
mod prelude;
|
mod prelude;
|
||||||
|
|
||||||
|
@ -46,9 +46,12 @@ async fn rocket() -> _ {
|
||||||
.attach(Template::custom(|engines| {
|
.attach(Template::custom(|engines| {
|
||||||
use tera::Value;
|
use tera::Value;
|
||||||
let catalogs = load_catalogs().unwrap();
|
let catalogs = load_catalogs().unwrap();
|
||||||
engines.tera.register_filter("i18n", move |value: &Value, args: &HashMap<String, Value>| {
|
engines.tera.register_filter(
|
||||||
|
"i18n",
|
||||||
|
move |value: &Value, args: &HashMap<String, Value>| {
|
||||||
i18n_filter(value, args, &catalogs)
|
i18n_filter(value, args, &catalogs)
|
||||||
})
|
},
|
||||||
|
)
|
||||||
}))
|
}))
|
||||||
.attach(SassFairing::default())
|
.attach(SassFairing::default())
|
||||||
}
|
}
|
|
@ -6,7 +6,7 @@ mod tests;
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
use derive_more::From;
|
use derive_more::From;
|
||||||
use reqwest::StatusCode;
|
use reqwest::StatusCode;
|
||||||
use rocket::http::{CookieJar, Cookie};
|
use rocket::http::{Cookie, CookieJar};
|
||||||
use serial::*;
|
use serial::*;
|
||||||
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
|
@ -3,10 +3,14 @@ use std::ops::Deref;
|
||||||
use rocket::{http::CookieJar, State};
|
use rocket::{http::CookieJar, State};
|
||||||
use rocket_dyn_templates::{context, Template};
|
use rocket_dyn_templates::{context, Template};
|
||||||
|
|
||||||
use crate::models::{Challenge, User, Settings};
|
use crate::models::{Challenge, Settings, User};
|
||||||
|
|
||||||
#[get("/<challenge>")]
|
#[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(
|
Template::render(
|
||||||
"index",
|
"index",
|
||||||
context! {
|
context! {
|
||||||
|
|
Loading…
Add table
Reference in a new issue