-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from sebadob/browser-language-detector
browser native language detection
- Loading branch information
Showing
6 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use actix_web::http::header::{HeaderValue, ACCEPT_LANGUAGE}; | ||
use actix_web::HttpRequest; | ||
use rauthy_common::constants::COOKIE_LOCALE; | ||
use rauthy_common::error_response::{ErrorResponse, ErrorResponseType}; | ||
use serde::{Deserialize, Serialize}; | ||
use tracing::debug; | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] | ||
pub enum Language { | ||
En, | ||
De, | ||
} | ||
|
||
impl Language { | ||
fn all_available<'a>() -> [&'a str; 4] { | ||
["en", "en-US", "de", "de-DE"] | ||
} | ||
|
||
pub fn as_str(&self) -> &str { | ||
match self { | ||
Language::En => "en", | ||
Language::De => "de", | ||
} | ||
} | ||
|
||
// pub fn set_cookie_str(&self) -> (&'static str, HeaderValue) { | ||
// ( | ||
// "Set-Cookie", | ||
// HeaderValue::from_str(&format!( | ||
// "{}={};Path=/;Max-Age={};SameSite=Lax", | ||
// // "{}={};Path=/;Max-Age={};SameSite=Lax;Secure=true", | ||
// LOCALE_COOKIE, | ||
// self.as_str(), | ||
// 15552000 | ||
// )) | ||
// .unwrap(), | ||
// ) | ||
// } | ||
} | ||
|
||
impl Default for Language { | ||
fn default() -> Self { | ||
Self::En | ||
} | ||
} | ||
|
||
impl From<&HeaderValue> for Language { | ||
fn from(value: &HeaderValue) -> Self { | ||
Language::from(value.to_str().unwrap_or_default()) | ||
} | ||
} | ||
|
||
impl From<&str> for Language { | ||
fn from(value: &str) -> Self { | ||
match value { | ||
"de" | "de-DE" => Self::De, | ||
"en" | "en-US" => Self::En, | ||
_ => Self::default(), | ||
} | ||
} | ||
} | ||
|
||
impl TryFrom<&HttpRequest> for Language { | ||
type Error = ErrorResponse; | ||
|
||
fn try_from(value: &HttpRequest) -> Result<Self, Self::Error> { | ||
if let Some(cookie) = value.headers().get(COOKIE_LOCALE) { | ||
return Ok(Language::from(cookie)); | ||
} | ||
|
||
if let Some(accept_lang) = value.headers().get(ACCEPT_LANGUAGE) { | ||
let accept_as_str = accept_lang.to_str().unwrap_or_default(); | ||
let common_languages = | ||
accept_language::intersection(accept_as_str, &Language::all_available()); | ||
debug!("common_languages: {:?}", common_languages); | ||
if !common_languages.is_empty() { | ||
return Ok(Language::from(common_languages.get(0).unwrap().as_str())); | ||
} | ||
} | ||
|
||
Err(ErrorResponse::new( | ||
ErrorResponseType::NotFound, | ||
"Could not find and extract a locale cookie or the accept-language header".to_string(), | ||
)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters