extract language

This commit is contained in:
2025-08-03 09:32:31 +02:00
parent 9919ae93c2
commit ba20e80873
3 changed files with 74 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
use crate::model::client::Client;
use axum::{routing::get, Router};
use axum::{http::HeaderMap, routing::get, Router};
use axum_extra::extract::{cookie::Cookie, CookieJar};
use sqlx::{pool::PoolOptions, sqlite::SqliteConnectOptions, SqlitePool};
use std::{str::FromStr, sync::Arc};
@@ -8,6 +8,7 @@ use uuid::Uuid;
mod game;
mod index;
pub(crate) mod language;
pub(crate) mod model;
mod page;
pub(crate) mod random_names;
@@ -16,6 +17,27 @@ pub(crate) enum Backend {
Sqlite(SqlitePool),
}
#[derive(Debug)]
enum Language {
German,
English,
}
impl From<String> for Language {
fn from(value: String) -> Self {
if value.starts_with("de") {
Language::German
} else {
Language::English
}
}
}
struct Req {
client: Client,
lang: Language,
}
impl Backend {
async fn client(&self, cookies: CookieJar) -> (CookieJar, Client) {
let existing_uuid = cookies
@@ -31,6 +53,19 @@ impl Backend {
}
}
}
// Combined method for getting both client and language
async fn client_full(&self, cookies: CookieJar, headers: &HeaderMap) -> (CookieJar, Req) {
let (cookies, client) = self.client(cookies).await;
let lang = language::language(&cookies, headers);
(
cookies,
Req {
client,
lang: lang.into(),
},
)
}
}
#[tokio::main]