Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i18n fixes + sync #60

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/i18n/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# use_i18n
# i18n

Learn how to use `use_i18n`.
i18n for web or single window on desktop.

Run:

```dioxus serve```
```dx serve --bin i18n```
Binary file removed examples/i18n/public/favicon.ico
Binary file not shown.
1 change: 1 addition & 0 deletions examples/i18n/src/en-US.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"id": "en-US",
"name": "English",
"texts": {
"messages": {
"hello_world": "Hello World!"
Expand Down
4 changes: 2 additions & 2 deletions examples/i18n/src/es-ES.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"id": "es-ES",
"name": "Español",
"texts": {
"messages": {
"hello_world": "Hola Mundo!"
},
"messages.hello": "Hola {name}"
}
}
}
9 changes: 9 additions & 0 deletions examples/i18n/src/it-IT.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"id": "it-IT",
"texts": {
"messages": {
"hello_world": "Ciao Mondo!"
},
"messages.hello": "Ciao {name}"
}
}
76 changes: 49 additions & 27 deletions examples/i18n/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use dioxus::prelude::*;
use dioxus_sdk::i18n::*;
use dioxus_sdk::translate;
use std::collections::HashMap;
use std::str::FromStr;

fn main() {
Expand All @@ -13,38 +14,59 @@ fn main() {

static EN_US: &str = include_str!("./en-US.json");
static ES_ES: &str = include_str!("./es-ES.json");
static IT_IT: &str = include_str!("./it-IT.json");

#[allow(non_snake_case)]
fn Body() -> Element {
let mut i18 = use_i18();
fn app() -> Element {

let change_to_english = move |_| i18.set_language("en-US".parse().unwrap());
let change_to_spanish = move |_| i18.set_language("es-ES".parse().unwrap());
use_init_i18n("en-US".parse().unwrap(),
("en-US", HashMap::from([("es", vec!["it-IT"])])).into()
, || {
let en_us = Language::from_str(EN_US).unwrap();
let es_es = Language::from_str(ES_ES).unwrap();
let it_it = Language::from_str(IT_IT).unwrap();
vec![en_us, es_es, it_it]
});

let i18 = use_i18();

rsx!(
button {
onclick: change_to_english,
label {
"English"
}
}
button {
onclick: change_to_spanish,
label {
"Spanish"
}
}
p { {translate!(i18, "messages.hello_world")} }
p { {translate!(i18, "messages.hello", name: "Dioxus")} }
change_language_dropdown{}
change_language_btn{}
p { "Translated in selected language: " {translate!(i18, "messages.hello_world")} }
p { "Fallback due to missing translation: " {translate!(i18, "messages.hello", name: "Dioxus")} }
p { "Missing translation and missing in fallback: " {translate!(i18, "unkown_id")} }

)
}

fn app() -> Element {
use_init_i18n("en-US".parse().unwrap(), "en-US".parse().unwrap(), || {
let en_us = Language::from_str(EN_US).unwrap();
let es_es = Language::from_str(ES_ES).unwrap();
vec![en_us, es_es]
});

rsx!(Body {})
fn change_language_btn() -> Element {
let mut i18 = use_i18();
rsx! {{
i18.language_list().iter().map(|(id, name, _img)| {
let id = id.clone();
rsx! { button {
onclick: move |_| { i18.set_language(id.clone()); },
"{name}"
}}
})
}}
}

fn change_language_dropdown() -> Element {
let mut i18 = use_i18();

rsx! {
select {
oninput: move |ev| {
i18.set_language(ev.value().parse().unwrap())
},
{i18.language_list().iter().map(|(id, name, _img)| {
rsx! { option {
value: id.to_string(),
selected: i18.is_selected(id),
"{name}"
}}
})}
}
}
}
16 changes: 16 additions & 0 deletions examples/i18sync/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "i18sync"
version = "0.1.0"
edition = "2021"

[dependencies]
dioxus = { workspace = true, features = ["desktop"] }
dioxus-sdk = { workspace = true, features = ["i18n"] }

log = "0.4.6"






42 changes: 42 additions & 0 deletions examples/i18sync/Dioxus.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[application]

# App (Project) Name
name = "i18sync"

# Dioxus App Default Platform
# desktop, web, mobile, ssr
default_platform = "desktop"

# `build` & `serve` dist path
out_dir = "dist"

# resource (public) file folder
asset_dir = "public"

[web.app]

# HTML title tag content
title = "dioxus | ⛺"

[web.watcher]

# when watcher trigger, regenerate the `index.html`
reload_html = true

# which files or dirs will be watcher monitoring
watch_path = ["src", "public"]

# include `assets` in web platform
[web.resource]

# CSS style file
style = []

# Javascript code file
script = []

[web.resource.dev]

# Javascript code file
# serve: [dev-server] only
script = []
7 changes: 7 additions & 0 deletions examples/i18sync/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# i18n

i18n in multiple windows - desktop only.

Run:

```dx serve --bin i18sync```
10 changes: 10 additions & 0 deletions examples/i18sync/src/en-US.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"id": "en-US",
"name": "English",
"texts": {
"messages": {
"hello_world": "Hello World!"
},
"messages.hello": "Hello {name}"
}
}
9 changes: 9 additions & 0 deletions examples/i18sync/src/es-ES.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"id": "es-ES",
"name": "Español",
"texts": {
"messages": {
"hello_world": "Hola Mundo!"
}
}
}
9 changes: 9 additions & 0 deletions examples/i18sync/src/it-IT.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"id": "it-IT",
"texts": {
"messages": {
"hello_world": "Ciao Mondo!"
},
"messages.hello": "Ciao {name}"
}
}
Loading