Improved locale recognition.
This commit is contained in:
@@ -21,6 +21,32 @@ pub fn init_locales() {
|
||||
);
|
||||
provide_context(loc);
|
||||
}
|
||||
|
||||
#[cfg(feature = "ssr")]
|
||||
{
|
||||
use actix_web::HttpRequest;
|
||||
use leptos::provide_context;
|
||||
|
||||
let req = use_context::<HttpRequest>();
|
||||
if let Some(r) = req {
|
||||
if r.query_string().contains("lang") {
|
||||
let languages = r.query_string()
|
||||
.split("&")
|
||||
.map(|qc| {
|
||||
let mut it = qc.split("=").take(2); (it.next().unwrap_or(""), it.next().unwrap_or(""))
|
||||
})
|
||||
.filter(|l| l.0 == "lang")
|
||||
.map(|l| l.1)
|
||||
.collect::<Vec<_>>();
|
||||
if !languages.is_empty() {
|
||||
provide_context(Locales(vec![Some(languages.get(0).unwrap().to_string())]));
|
||||
}
|
||||
} else if let Some(al) = r.head().headers.get("accept-language") {
|
||||
let loc = Locales(al.to_str().unwrap_or("").split(",").map(|l| Some(l.to_string())).collect());
|
||||
provide_context(loc);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn trl(phrase: &str) -> impl Fn() -> String {
|
||||
|
||||
Reference in New Issue
Block a user