1 unstable release
new 0.1.0 | Jan 6, 2025 |
---|
#97 in Internationalization (i18n)
115 downloads per month
8KB
76 lines
Dioxus Translate
Usage
Add dependancy
Cargo.toml
[dependencies]
dioxus-translate = { version = "0.1.0" }
[features]
web-only = ["dioxus-translate/web"]
Define translate
- Define
i18n
translates.
use dioxus_translate::translate;
translate! {
HeaderTranslate;
signup: {
en: "Sign up",
},
login: {
en: "Login",
},
}
- The above macro will be expanded to the below code
#[derive(Debug, Clone, PartialEq)]
pub struct HeaderTranslate {
pub signup: &'static str,
pub login: &'static str,
}
impl dioxus_translate::Translator for HeaderTranslate {
fn en() -> Self {
Self {
signup: "Sign up",
login: "Login",
}
}
}
- At this,
Translator
define the belowTranslator
.
pub trait Translator {
fn en() -> Self;
}
Getting translate
#![allow(non_snake_case)]
use dioxus::prelude::*;
use dioxus_translate::*;
#[component]
pub fn Component(lang: Language) -> Element {
let i18n_header: HeaderTranslate = translate(&lang);
rsx! { "{i18n_header.signup}" }
}
Define additional language spec
- Currently, only
ko
language is supported withko
feature
Add ko
feature
- Add
ko
feature inCargo.toml
[dependencies]
dioxus-translate = { version = "0.1.0", features = ["ko"] }
Define translate
use dioxus_translate::translate;
translate! {
HeaderTranslate;
signup: {
ko: "회원가입",
en: "Sign up",
},
login: {
ko: "로그인",
en: "Login",
},
}
Dependencies
~0.3–3MB
~61K SLoC