52 lines
1.4 KiB
Rust
52 lines
1.4 KiB
Rust
use std::collections::HashMap;
|
|||
|
|
use lazy_static::lazy_static;
|
||
|
|
use leptos::*;
|
||
|
|
|
||
|
|
lazy_static! {
|
||
|
|
static ref LANGUAGES: HashMap<&'static str, HashMap<&'static str, &'static str>> = {
|
||
|
|
let m = HashMap::from([
|
||
|
|
("cs", HashMap::from( [
|
||
|
|
("Dashboard", "Přehled"),
|
||
|
|
("Settings", "Nastavení"),
|
||
|
|
("Search...", "Najít...")
|
||
|
|
])),
|
||
|
|
("sk", HashMap::from( [
|
||
|
|
("Dashboard", "Prehlad"),
|
||
|
|
("Settings", "Nastavenie"),
|
||
|
|
("Search...", "Najsť...")
|
||
|
|
]))
|
||
|
|
]);
|
||
|
|
m
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
fn get_locales(cx: Scope) -> Vec<Option<String>> {
|
||
|
|
let (loc, set_loc) = create_signal(cx, Vec::new());
|
||
|
|
|
||
|
|
create_effect(cx, move |_| {
|
||
|
|
let js_locales = window().navigator().languages();
|
||
|
|
set_loc.update(|l| *l = js_locales.into_iter().map(|val| val.as_string()).collect::<Vec<_>>());
|
||
|
|
});
|
||
|
|
|
||
|
|
loc.get_untracked()
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn get_dictionary(cx: Scope) -> Option<&'static HashMap<&'static str, &'static str>> {
|
||
|
|
let locs = get_locales(cx);
|
||
|
|
|
||
|
|
for loc in locs {
|
||
|
|
if let Some(key) = loc {
|
||
|
|
if let Some(k) = key.split("-").collect::<Vec<_>>().get(0) {
|
||
|
|
if *k == "en" {
|
||
|
|
return None;
|
||
|
|
}
|
||
|
|
if !LANGUAGES.contains_key(*k) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
return LANGUAGES.get(*k);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
None
|
||
|
|
}
|