Translation (WIP)
This commit is contained in:
parent
9b0fcec5e2
commit
e9873c1943
29 changed files with 586 additions and 285 deletions
|
|
@ -1,7 +1,7 @@
|
|||
use std::{net::SocketAddr, path::Path};
|
||||
|
||||
use axum::{
|
||||
extract::{ConnectInfo, FromRef, Request, State},
|
||||
extract::{ConnectInfo, Extension, FromRef, Request, State},
|
||||
http::StatusCode,
|
||||
middleware::{self, Next},
|
||||
response::{Response, Result},
|
||||
|
|
@ -12,10 +12,12 @@ use axum_extra::extract::cookie::CookieJar;
|
|||
use chrono::prelude::*;
|
||||
use clap::Parser;
|
||||
use config::Config;
|
||||
use itertools::Itertools;
|
||||
use tower_http::{services::ServeDir, trace::TraceLayer};
|
||||
use tracing::{event, Level};
|
||||
|
||||
use data::{db, model};
|
||||
use translation::Tr;
|
||||
|
||||
mod config;
|
||||
mod consts;
|
||||
|
|
@ -26,6 +28,7 @@ mod html_templates;
|
|||
mod ron_extractor;
|
||||
mod ron_utils;
|
||||
mod services;
|
||||
mod translation;
|
||||
mod utils;
|
||||
|
||||
#[derive(Clone)]
|
||||
|
|
@ -191,6 +194,7 @@ async fn main() {
|
|||
.fallback(services::not_found)
|
||||
.layer(TraceLayer::new_for_http())
|
||||
// FIXME: Should be 'route_layer' but it doesn't work for 'fallback(..)'.
|
||||
.layer(middleware::from_fn(translation))
|
||||
.layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
user_authentication,
|
||||
|
|
@ -218,6 +222,39 @@ async fn user_authentication(
|
|||
Ok(next.run(req).await)
|
||||
}
|
||||
|
||||
async fn translation(
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
mut req: Request,
|
||||
next: Next,
|
||||
) -> Result<Response> {
|
||||
let language = if let Some(user) = user {
|
||||
user.lang
|
||||
} else {
|
||||
let available_codes = Tr::available_codes();
|
||||
|
||||
// TODO: Check cookies before http headers.
|
||||
|
||||
let accept_language = req
|
||||
.headers()
|
||||
.get(axum::http::header::ACCEPT_LANGUAGE)
|
||||
.map(|v| v.to_str().unwrap_or_default())
|
||||
.unwrap_or_default()
|
||||
.split(',')
|
||||
.map(|l| l.split('-').next().unwrap_or_default())
|
||||
.find_or_first(|l| available_codes.contains(l));
|
||||
|
||||
// TODO: Save to cookies.
|
||||
|
||||
accept_language.unwrap_or("en").to_string()
|
||||
};
|
||||
|
||||
let tr = Tr::new(&language);
|
||||
|
||||
// let jar = CookieJar::from_headers(req.headers());
|
||||
req.extensions_mut().insert(tr);
|
||||
Ok(next.run(req).await)
|
||||
}
|
||||
|
||||
async fn get_current_user(
|
||||
connection: db::Connection,
|
||||
jar: &CookieJar,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue