66 lines
1.9 KiB
Rust
66 lines
1.9 KiB
Rust
|
use actix_web::HttpRequest;
|
||
|
use askama::Template;
|
||
|
use crate::config::Config;
|
||
|
#[derive(Template)]
|
||
|
#[template(path = "index.html")]
|
||
|
pub struct TplIndex<'a> {
|
||
|
pub lang: &'a str,
|
||
|
pub csrf_token: &'a str,
|
||
|
pub sncf_admin_token: Option<String>,
|
||
|
}
|
||
|
#[derive(Template)]
|
||
|
#[template(path = "error.html")]
|
||
|
pub struct TplError<'a> {
|
||
|
pub lang: &'a str,
|
||
|
pub error_msg: &'a str,
|
||
|
}
|
||
|
#[derive(Template)]
|
||
|
#[template(path = "link.html")]
|
||
|
pub struct TplLink<'a> {
|
||
|
pub lang: &'a str,
|
||
|
pub admin_token: &'a str,
|
||
|
pub csrf_token: &'a str,
|
||
|
pub config: &'a Config,
|
||
|
}
|
||
|
pub fn get_lang(req: &HttpRequest) -> String {
|
||
|
// getting language from client header
|
||
|
// taking the two first characters of the Accept-Language header,
|
||
|
// in lowercase, then parsing it.
|
||
|
// if it fails, returns "en"
|
||
|
if let Some(la) = req.uri().query() {
|
||
|
|
||
|
return la[5..].to_string();
|
||
|
|
||
|
} else {
|
||
|
if let Some(l) = req.headers().get("Accept-Language") {
|
||
|
if let Ok(s) = l.to_str() {
|
||
|
return s.to_lowercase()[..2].to_string();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
String::from("en")
|
||
|
}
|
||
|
mod filters {
|
||
|
use crate::config::LOC;
|
||
|
pub fn tr(key: &str, lang: &str) -> askama::Result<String> {
|
||
|
let translation = LOC.get(key).ok_or_else(|| {
|
||
|
eprintln!("tr filter: couldn't find the key {}", key);
|
||
|
askama::Error::from(std::fmt::Error)
|
||
|
})?;
|
||
|
Ok(String::from(
|
||
|
translation
|
||
|
.get(lang)
|
||
|
.unwrap_or(translation.get("en").ok_or_else(|| {
|
||
|
eprintln!("tr filter: couldn't find the lang {} in key {}", lang, key);
|
||
|
askama::Error::from(std::fmt::Error)
|
||
|
})?)
|
||
|
.as_str()
|
||
|
.ok_or_else(|| {
|
||
|
eprintln!("tr filter: lang {} in key {} is not str", lang, key);
|
||
|
askama::Error::from(std::fmt::Error)
|
||
|
})?,
|
||
|
))
|
||
|
}
|
||
|
}
|
||
|
|