Files
todo-baggins/dioxus-i18n/examples/config-dynamic-pathbuf.rs
Matouš Volf 2c2ad7ad21
Some checks failed
hadolint check / hadolint check (pull_request) Successful in 13s
actionlint check / actionlint check (pull_request) Successful in 7s
conventional pull request title check / conventional pull request title check (pull_request) Successful in 3s
conventional commit messages check / conventional commit messages check (pull_request) Successful in 6s
dotenv-linter check / dotenv-linter check (pull_request) Successful in 7s
GitLeaks check / GitLeaks check (pull_request) Successful in 13s
markdownlint check / markdownlint check (pull_request) Failing after 54s
Prettier check / Prettier check (pull_request) Failing after 51s
htmlhint check / htmlhint check (pull_request) Successful in 1m3s
checkov check / checkov check (pull_request) Failing after 2m26s
ShellCheck check / ShellCheck check (pull_request) Successful in 1m14s
Stylelint check / Stylelint check (pull_request) Successful in 1m27s
Rust check / Rust check (pull_request) Failing after 11m40s
yamllint check / yamllint check (pull_request) Successful in 13m36s
fix: automatically reconnect after losing a WebSocket connection
2026-01-23 11:04:10 +01:00

63 lines
1.7 KiB
Rust

//! This example demonstrates how to use pathbuf derived I18nConfig.
//! This is useful when the path to the translation files is not known at compile time.
use dioxus::prelude::*;
use dioxus_i18n::{prelude::*, t};
use unic_langid::langid;
use std::path::PathBuf;
fn main() {
launch(app);
}
#[allow(non_snake_case)]
fn Body() -> Element {
let mut i18n = i18n();
let change_to_english = move |_| i18n.set_language(langid!("en-US"));
let change_to_spanish = move |_| i18n.set_language(langid!("es-ES"));
rsx!(
button {
onclick: change_to_english,
label {
"English"
}
}
button {
onclick: change_to_spanish,
label {
"Spanish"
}
}
p { { t!("hello_world") } }
p { { t!("hello", name: "Dioxus") } }
)
}
fn app() -> Element {
use_init_i18n(|| {
// This initialisation allows individual translation files to be selected.
// The locales can be added with an implicitly derived locale (see config-static-includestr.rs for a comparison)
// or using an explicit Locale::new_dynamic call.
//
// The two examples are functionally equivalent.
//
// It IS NOT supported in WASM targets.
I18nConfig::new(langid!("en-US"))
// Implicit...
.with_locale((
langid!("es-ES"),
PathBuf::from("./examples/data/i18n/es-ES.ftl"),
))
// Explicit...
.with_locale(Locale::new_dynamic(
langid!("en-US"),
PathBuf::from("./examples/data/i18n/en-US.ftl"),
))
});
rsx!(Body {})
}