find lang

This commit is contained in:
Mylloon 2024-12-22 18:30:17 +01:00 committed by Mylloon
parent e699d3cbac
commit ca12c5c467

View file

@ -1,8 +1,10 @@
use std::{fs, os::unix::fs::MetadataExt, path::Path}; use std::{fs, os::unix::fs::MetadataExt, path::Path};
use actix_web::{ use actix_web::{
http::header::{self, ContentType, TryIntoHeaderValue}, http::{
http::StatusCode, header::{self, ContentType, HeaderMap, TryIntoHeaderValue},
StatusCode,
},
HttpRequest, HttpResponse, Responder, HttpRequest, HttpResponse, Responder,
}; };
use base64::{engine::general_purpose, Engine}; use base64::{engine::general_purpose, Engine};
@ -116,3 +118,23 @@ fn read_img(data: Vec<u8>, mime: &mime::Mime) -> File {
pub fn remove_first_letter(s: &str) -> &str { pub fn remove_first_letter(s: &str) -> &str {
s.chars().next().map(|c| &s[c.len_utf8()..]).unwrap() s.chars().next().map(|c| &s[c.len_utf8()..]).unwrap()
} }
#[derive(Debug)]
pub enum Lang {
French,
English,
}
/// Get the browser language
pub fn lang(headers: &HeaderMap) -> Lang {
headers
.get("Accept-Language")
.and_then(|lang| lang.to_str().ok())
.and_then(|lang| {
["fr", "fr-FR"]
.into_iter()
.any(|i| lang.contains(i))
.then_some(Lang::French)
})
.unwrap_or(Lang::English)
}