Initial commit. UI localisation works.

This commit is contained in:
2023-08-18 12:22:23 +02:00
commit 9f17393310
100 changed files with 39402 additions and 0 deletions
+52
View File
@@ -0,0 +1,52 @@
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
}
+15
View File
@@ -0,0 +1,15 @@
use leptos::*;
use crate::locales::catalogues::get_dictionary;
mod catalogues;
pub fn trl(cx: Scope, phrase: &'static str) -> impl Fn() -> &'static str {
let mut translated = phrase;
if let Some(dict) = get_dictionary(cx) {
if let Some(p) = dict.get(phrase) {
translated = *p;
}
}
|| { translated }
}