diff --git a/Cargo.toml b/Cargo.toml index 72823bb..fadb17c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ log = "0.4.17" matrix-sdk = { version = "0.6.2", default-features = false, features = ["js", "native-tls", "e2e-encryption", "indexeddb", "experimental-timeline"] } tokio = "1.27.0" url = "2.3.1" -web-sys = {version = "0.3.61", features = ["Document", "Element", "HtmlElement", "HtmlBodyElement", "Node", "NodeList", "Window", "console", "CssStyleDeclaration", "Location"]} +web-sys = {version = "0.3.61", features = ["Document", "Element", "HtmlElement", "HtmlBodyElement", "Node", "NodeList", "Window", "console", "CssStyleDeclaration", "Location", "Navigator"]} time = "0.3.22" anyhow = "1" serde = { version = "1.0.96", features = ["derive"] } diff --git a/src/main.rs b/src/main.rs index 0e34097..ab8044f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,8 +39,28 @@ static EN_US: &str = include_str!("./locales/en-US.json"); static ES_ES: &str = include_str!("./locales/es-ES.json"); fn App(cx: Scope) -> Element { - let selected_language: LanguageIdentifier = - "es-ES".parse().expect("can't parse es-ES language"); + if let Some(static_login_form) = window()?.document()?.get_element_by_id("static-login-form") { + if let Some(parent) = static_login_form.parent_node() { + parent.remove_child(&static_login_form); + }; + }; + + let navigator_language = window() + .expect("window") + .navigator() + .language() + .unwrap_or("en-US".to_string()); + + let default_language = if navigator_language.starts_with("es") { + "es-ES" + } else { + "en-US" + }; + + let selected_language: LanguageIdentifier = default_language + .parse() + .expect("can't parse es-ES language"); + let fallback_language: LanguageIdentifier = selected_language.clone(); use_init_i18n(cx, selected_language, fallback_language, || { @@ -49,12 +69,6 @@ fn App(cx: Scope) -> Element { vec![en_us, es_es] }); - if let Some(static_login_form) = window()?.document()?.get_element_by_id("static-login-form") { - if let Some(parent) = static_login_form.parent_node() { - parent.remove_child(&static_login_form); - }; - }; - use_init_app(cx); let client = use_client(cx);