This commit is contained in:
2026-01-24 12:15:23 +01:00
parent b5e7ab5c48
commit af095684a1
37 changed files with 2595 additions and 85 deletions

View File

@@ -0,0 +1,47 @@
//! This example demonstrates how to use an auto_locales derived I18nConfig.
//! This is useful when you have a lot of locales and you don't want to manually add them.
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 performs a deep search for all locales in the given path.
// It IS NOT supported in WASM targets.
I18nConfig::new(langid!("en-US")).with_auto_locales(PathBuf::from("./examples/data/i18n/"))
});
rsx!(Body {})
}

View File

@@ -0,0 +1,62 @@
//! 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 {})
}

View File

@@ -0,0 +1,57 @@
//! This example demonstrates how to use pathbuf derived I18nConfig.
//! This is useful for WASM targets; the paths to the translation files must be known at compile time.
use dioxus::prelude::*;
use dioxus_i18n::{prelude::*, t};
use unic_langid::langid;
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-dynamic-pathbuf.rs for a comparison)
// or using an explicit Locale::new_static call.
//
// The two examples are functionally equivalent.
//
// It IS supported in WASM targets.
I18nConfig::new(langid!("en-US"))
// Implicit...
.with_locale((langid!("es-ES"), include_str!("./data/i18n/es-ES.ftl")))
// Explicit...
.with_locale(Locale::new_static(
langid!("en-US"),
include_str!("./data/i18n/en-US.ftl"),
))
});
rsx!(Body {})
}

View File

@@ -0,0 +1,124 @@
### Fluent grammar examples for dioxus-i18n.
## These examples demonstrate Fluent file grammar and how dioxus-i18n can be
## used to access these translations.
## Examples derived from: https://projectfluent.org/fluent/guide/index.html
# Simple message
simple-message = This is a simple message.
# $name (String) - The name you want to display.
message-with-variable = This is a message with a variable: { $name }.
# Reference to a term.
-a-term = This is a common term used by many messages.
message-referencing-a-term = This is a message with a reference: { -a-term }.
# Use of special characters.
message-with-special-character = This message contain opening curly brace {"{"} and a closing curly brace {"}"}.
# Message with blanks.
blank-is-removed = This message starts with no blanks.
blank-is-preserved = {" "}This message starts with 4 spaces (note HTML contracts them).
# Message with attributes.
message-with-attributes = Predefined value
.placeholder = email@example.com
.aria-label = Login input value
.title = Type your login email
# Message with quotes.
literal-quote-cryptic = Text in {"\""}double quotes{"\""}.
literal-quote-preferred = Text in "double quotes".
# Message with Unicode characters.
unicode-cryptic = {"\u2605"} {"\u2606"} {"\u2728"} {"\u262F"} {"\u263A"}
unicode-preferred = ★ ☆ ✨ ☯ ☺
# Message with a placeable.
single-line = Text can be written in a single line.
multi-line = Text can also span multiple lines
as long as each new line is indented
by at least one space.
block-line =
Sometimes it's more readable to format
multiline text as a "block", which means
starting it on a new line. All lines must
be indented by at least one space.
# Message using functions.
#
# Note: Builtin functions are currently unsupported: See Fluent issue https://github.com/projectfluent/fluent-rs/issues/181
# The Bundle::add_builtins() function is not published at the time of writing this example.
#
# Using a builtin currently results in an error.
#
# $duration (Number) - The duration in seconds.
time-elapsed-no-function = Time elapsed: { $duration }s.
time-elapsed-function = Currently unsupported: error raised: { NUMBER($duration) }.
# Message reference.
referenced-message = Referenced message
message-referencing-another-message = Message referencing another message: { referenced-message }.
# Message selection plurals.
message-selection-plurals =
{ $value ->
*[one] Value is one: { $value }.
[other] Value is more than one: { $value }.
}
# Message selection numeric.
# Argument must be numeric.
message-selection-numeric =
{ NUMERIC($value) ->
[0.0] Zero: { $value }.
*[0.5] A half: { $value }.
[other] Other ($value)
}
# Message selection number.
#
# Note: Builtin functions are currently unsupported: See Fluent issue https://github.com/projectfluent/fluent-rs/issues/181
# The Bundle::add_builtins() function is not published at the time of writing this example.
#
# Using the NUMBER builtin always results in a default behaviour.
#
message-selection-number = { NUMBER($pos, type: "ordinal") ->
[1] First!
[one] {$pos}st
[two] {$pos}nd
[few] {$pos}rd
*[other] {$pos}th
}
# Variables in references.
-term-using-variable = https://{ $host }
message-using-term-with-variable = For example: { -term-using-variable(host: "example.com") }.
-term-using-variable-2 =
{ $case ->
*[nominative] Firefox
[locative] Firefoksie
}
message-using-term-with-variable-2-1 = Informacje o { -term-using-variable-2(case: "locative") }.
message-using-term-with-variable-2-2 = About { -term-using-variable-2(case: "nominative") }.
message-using-term-with-variable-2-default = About { -term-using-variable-2(case: "") }.
message-using-term-with-variable-2-not-provided = About { -term-using-variable-2 }.
-brand-name =
{ $case ->
*[nominative] Firefox
[locative] Firefoksie
}
string-literal = { "string literal" }
number-literal-1 = { 1 }
number-literal-2 = { -123 }
number-literal-3 = { 3.14 }
inline-expression-placeable-1 = { { "string literal" } }
inline-expression-placeable-2 = { { 123 } }

View File

@@ -0,0 +1,3 @@
hello_world = Hello, World!
hello = Hello, {$name}!

View File

@@ -0,0 +1,3 @@
hello_world = Hola, Mundo!
hello = Hola, {$name}!

View File

@@ -0,0 +1,47 @@
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(|| {
I18nConfig::new(langid!("en-US"))
.with_locale((langid!("en-US"), include_str!("./data/i18n/en-US.ftl")))
.with_locale((
langid!("es-ES"),
PathBuf::from("./examples/data/i18n/es-ES.ftl"),
))
});
rsx!(Body {})
}

View File

@@ -0,0 +1,233 @@
//! This example demonstrates many of the Fluent grammar constructs, and how they are
//! used in dioxus-i18n.
//! This performs a lookup only, no additional translation files are provided
use dioxus::prelude::*;
use dioxus_i18n::{prelude::*, tid};
use unic_langid::langid;
use std::path::PathBuf;
fn main() {
launch(app);
}
#[allow(non_snake_case)]
#[component]
fn Body() -> Element {
rsx! {
table {
tbody {
tr {
td { "Simple message" }
td { {tid!("simple-message")} }
}
tr {
td { "Non-existing message: id provided by default when using tid! macro" }
td { {tid!("non-existing-message")} }
}
tr {
td { "Message with a variable" }
td { {tid!("message-with-variable", name: "Value 1")} }
}
tr {
td { }
td { {tid!("message-with-variable", name: "Value 2")} }
}
tr {
td { "Reference to a term" }
td { {tid!("message-referencing-a-term")} }
}
tr {
td { "Use of special characters." }
td { {tid!("message-with-special-character")} }
}
tr {
td { "Message with blanks." }
td { "'" {tid!("blank-is-removed")} "'" }
}
tr {
td { }
td { "'" {tid!("blank-is-preserved")} "'" }
}
tr {
td { "Message with attributes: root" }
td { {tid!("message-with-attributes")} }
}
tr {
td { "Message with attributes: attribute" }
td { {tid!("message-with-attributes.placeholder")} }
}
tr {
td { }
td { {tid!("message-with-attributes.aria-label")} }
}
tr {
td { }
td { {tid!("message-with-attributes.title")} }
}
tr {
td { "Message with attributes: not existing" }
td { {tid!("message-with-attributes.not-existing")} }
}
tr {
td { "Message with attributes: invalid" }
td { {tid!("message-with-attributes.placeholder.invalid")} }
}
tr {
td { "Message with quotes: cryptic" }
td { {tid!("literal-quote-cryptic")} }
}
tr {
td { "Message with quotes: preferred" }
td { {tid!("literal-quote-preferred")} }
}
tr {
td { "Message with Unicode characters: cryptic" }
td { {tid!("unicode-cryptic")} }
}
tr {
td { "Message with Unicode characters: preferred" }
td { {tid!("unicode-preferred")} }
}
tr {
td { "Message with a placeable: single-line" }
td { {tid!("line-single")} }
}
tr {
td { "Message with a placeable: single-line" }
td { {tid!("single-line")} }
}
tr {
td { "Message with a placeable: multi-line (1)" }
td { {tid!("multi-line")} }
}
tr {
td { "Message with a placeable: multi-line (2)" }
td { pre { {tid!("multi-line")} } }
}
tr {
td { "Message with a placeable: block-line (1)" }
td { {tid!("block-line")} }
}
tr {
td { "Message with a placeable: block-line (2)" }
td { pre { {tid!("block-line")} } }
}
tr {
td { "Message using functions: no function" }
td { pre { {tid!("time-elapsed-no-function", duration: 23.7114812589)} } }
}
tr {
td { "Message using functions: function" }
td { pre { {tid!("time-elapsed-function", duration: 23.7114812589)} } }
}
tr {
td { "Reference to a message" }
td { {tid!("message-referencing-another-message")} }
}
tr {
td { "Message selection: plurals" }
td { {tid!("message-selection-plurals", value: 1)} }
}
tr {
td { }
td { {tid!("message-selection-plurals", value: 2)} }
}
tr {
td { "Message selection: plurals (default: an 'empty' value must be provided...)" }
td { {tid!("message-selection-plurals", value: "")} }
}
tr {
td { "Message selection: plurals (default: ... otherwise an error is raised)" }
td { {tid!("message-selection-plurals")} }
}
tr {
td { "Message selection: numeric" }
td { {tid!("message-selection-numeric", value: 0.0)} }
}
tr {
td { }
td { {tid!("message-selection-numeric", value: 0.5)} }
}
tr {
td { }
td { {tid!("message-selection-numeric", value: 42.0)} }
}
tr {
td { "Message selection: numeric (default)" }
td { {tid!("message-selection-numeric", value: "")} }
}
tr {
td { "Message selection: number" }
td { {tid!("message-selection-number", pos: 1)} }
}
tr {
td { "" }
td { {tid!("message-selection-number", pos: 2)} }
}
tr {
td { "" }
td { {tid!("message-selection-number", pos: 3)} }
}
tr {
td { "" }
td { {tid!("message-selection-number", pos: 4)} }
}
tr {
td { "Variables in references (1)" }
td { {tid!("message-using-term-with-variable")} }
}
tr {
td { "Variables in references (2)" }
td { {tid!("message-using-term-with-variable-2-1")} }
}
tr {
td { }
td { {tid!("message-using-term-with-variable-2-2")} }
}
tr {
td { }
td { {tid!("message-using-term-with-variable-2-default")} }
}
tr {
td { }
td { {tid!("message-using-term-with-variable-2-not-provided")} }
}
tr {
td { "Literals: string" }
td { {tid!("string-literal")} }
}
tr {
td { }
td { {tid!("number-literal-1")} }
}
tr {
td { }
td { {tid!("number-literal-2")} }
}
tr {
td { }
td { {tid!("number-literal-3")} }
}
tr {
td { }
td { {tid!("inline-expression-placeable-1")} }
}
tr {
td { }
td { {tid!("inline-expression-placeable-2")} }
}
}
}
}
}
fn app() -> Element {
use_init_i18n(|| {
// Only one example in this path, which contains the complete Fluent grammar.
I18nConfig::new(langid!("en")).with_auto_locales(PathBuf::from("./examples/data/fluent/"))
});
rsx!(Body {})
}

View File

@@ -0,0 +1,57 @@
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
use dioxus_i18n::{prelude::*, t};
use freya::prelude::*;
use unic_langid::langid;
use std::path::PathBuf;
fn main() {
launch_with_props(app, "freya + i18n", (300.0, 200.0));
}
#[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!(
rect {
rect {
direction: "horizontal",
Button {
onclick: change_to_english,
label {
"English"
}
}
Button {
onclick: change_to_spanish,
label {
"Spanish"
}
}
}
label { { t!("hello_world") } }
label { { t!("hello", name: "Dioxus") } }
}
)
}
fn app() -> Element {
use_init_i18n(|| {
I18nConfig::new(langid!("en-US"))
.with_locale((langid!("en-US"), include_str!("./data/i18n/en-US.ftl")))
.with_locale((
langid!("es-ES"),
PathBuf::from("./examples/data/i18n/es-ES.ftl"),
))
});
rsx!(Body {})
}