From ca12c5c467ad28019a15c9c374b6d1c8587a3a1d Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sun, 22 Dec 2024 18:30:17 +0100 Subject: [PATCH] find lang --- src/utils/misc.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/utils/misc.rs b/src/utils/misc.rs index dacfd8b..e70f9fc 100644 --- a/src/utils/misc.rs +++ b/src/utils/misc.rs @@ -1,8 +1,10 @@ use std::{fs, os::unix::fs::MetadataExt, path::Path}; use actix_web::{ - http::header::{self, ContentType, TryIntoHeaderValue}, - http::StatusCode, + http::{ + header::{self, ContentType, HeaderMap, TryIntoHeaderValue}, + StatusCode, + }, HttpRequest, HttpResponse, Responder, }; use base64::{engine::general_purpose, Engine}; @@ -116,3 +118,23 @@ fn read_img(data: Vec, mime: &mime::Mime) -> File { pub fn remove_first_letter(s: &str) -> &str { 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) +}