-
Notifications
You must be signed in to change notification settings - Fork 10
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 #108 from Baptistemontan/fix-csr-fetch-locale
Fix csr locale fetching
- Loading branch information
Showing
9 changed files
with
130 additions
and
91 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,58 @@ | ||
use crate::Locale; | ||
|
||
cfg_if::cfg_if! { | ||
if #[cfg(all(feature = "ssr", not(any(feature = "hydrate", all(feature="csr", feature="cookie")))))] { | ||
#[inline] | ||
pub fn fetch_locale<T: Locale>() -> T { | ||
crate::server::fetch_locale_server_side::<T>() | ||
} | ||
} else if #[cfg(all(feature = "hydrate", not(any(all(feature="csr", feature="cookie"), feature = "ssr"))))] { | ||
pub fn fetch_locale<T: Locale>() -> T { | ||
leptos::document() | ||
.document_element() | ||
.and_then(|el| el.get_attribute("lang")) | ||
.and_then(|lang| T::from_str(&lang)) | ||
.unwrap_or_default() | ||
} | ||
} else if #[cfg(all(all(feature="csr", feature="cookie"), not(any(feature = "ssr", feature = "hydrate"))))] { | ||
pub fn fetch_locale<T: Locale>() -> T { | ||
fn inner<T: Locale>() -> Option<T> { | ||
let document = super::get_html_document()?; | ||
let cookies = document.cookie().ok()?; | ||
cookies.split(';').find_map(|cookie| { | ||
let (key, value) = cookie.split_once('=')?; | ||
if key.trim() == super::COOKIE_PREFERED_LANG { | ||
T::from_str(value) | ||
} else { | ||
None | ||
} | ||
}) | ||
} | ||
inner().unwrap_or_default() | ||
} | ||
#[inline] | ||
pub fn fetch_locale<T: Locale>() -> T { | ||
if cfg!(feature = "ssr") { | ||
fetch_locale_ssr() | ||
} else if cfg!(feature = "hydrate") { | ||
fetch_locale_hydrate() | ||
} else if cfg!(feature = "csr") { | ||
fetch_locale_csr() | ||
} else { | ||
#[inline] | ||
pub fn fetch_locale<T: Locale>() -> T { | ||
Default::default() | ||
Default::default() | ||
} | ||
} | ||
|
||
// ssr fetch | ||
fn fetch_locale_ssr<T: Locale>() -> T { | ||
crate::server::fetch_locale_server_side::<T>() | ||
} | ||
|
||
// hydrate fetch | ||
fn fetch_locale_hydrate<T: Locale>() -> T { | ||
leptos::document() | ||
.document_element() | ||
.and_then(|el| el.get_attribute("lang")) | ||
.and_then(|lang| T::from_str(&lang)) | ||
.unwrap_or_default() | ||
} | ||
|
||
// csr fetch | ||
fn fetch_locale_csr<T: Locale>() -> T { | ||
fn get_lang_cookie<T: Locale>() -> Option<T> { | ||
let document = super::get_html_document()?; | ||
let cookies = document.cookie().ok()?; | ||
cookies.split(';').find_map(|cookie| { | ||
let (key, value) = cookie.split_once('=')?; | ||
if key.trim() == super::COOKIE_PREFERED_LANG { | ||
T::from_str(value) | ||
} else { | ||
None | ||
} | ||
}) | ||
} | ||
|
||
if cfg!(feature = "cookie") { | ||
if let Some(lang) = get_lang_cookie() { | ||
return lang; | ||
} | ||
} | ||
|
||
leptos::window() | ||
.navigator() | ||
.languages() | ||
.into_iter() | ||
.filter_map(|js_val| js_val.as_string()) | ||
.find_map(|pref_lang| T::from_str(&pref_lang)) | ||
.unwrap_or_default() | ||
} |
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