Skip to content
This repository has been archived by the owner on Aug 24, 2024. It is now read-only.

Commit

Permalink
chore: use navigator language (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
b-avb authored Feb 28, 2024
1 parent 2ddd74a commit a89acd3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
30 changes: 22 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, || {
Expand All @@ -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);
Expand Down

0 comments on commit a89acd3

Please sign in to comment.