freya/_docs/i18n.rs
1//! # i18n
2//!
3//! You may add i18n (localization) support to your Freya app by using the [**freya-i18n**](https://crates.io/crates/freya-i18n) crate.
4//! You can also enable its reexport by turning on the `i18n` feature in freya.
5//!
6//! ```fluent
7//! # en-US.ftl
8//!
9//! hello_world = Hello, World!
10//! hello = Hello, {$name}!
11//! ```
12//!
13//!
14//! ```fluent
15//! # es-ES.ftl
16//!
17//! hello_world = Hola, Mundo!
18//! hello = Hola, {$name}!
19//! ```
20//!
21//! ```rust
22//! # use freya::prelude::*;
23//! # use freya_i18n::prelude::*;
24//!
25//! // main.rs
26//!
27//! fn main() {
28//! # return;
29//! launch(LaunchConfig::new().with_window(WindowConfig::new(app)))
30//! }
31//!
32//! #[derive(PartialEq)]
33//! struct Body;
34//!
35//! impl Render for Body {
36//! fn render(&self) -> impl IntoElement {
37//! // Access to the i18n state
38//! let mut i18n = I18n::get();
39//!
40//! // Update the current language
41//! let change_to_english = move |_| i18n.set_language(langid!("en-US"));
42//! let change_to_spanish = move |_| i18n.set_language(langid!("es-ES"));
43//!
44//! rect()
45//! .expanded()
46//! .center()
47//! .child(
48//! rect()
49//! .horizontal()
50//! .child(Button::new().on_press(change_to_english).child("English"))
51//! .child(Button::new().on_press(change_to_spanish).child("Spanish")),
52//! )
53//! .child(t!("hello_world"))
54//! .child(t!("hello", name: "Freya!"))
55//! }
56//! }
57//!
58//! fn app() -> impl IntoElement {
59//! // Initialize our i18n config
60//! use_init_i18n(|| {
61//! I18nConfig::new(langid!("en-US"))
62//! .with_locale(Locale::new_static(
63//! langid!("en-US"),
64//! include_str!("./en-US.ftl"),
65//! ))
66//! .with_locale(Locale::new_dynamic(
67//! langid!("es-ES"),
68//! "./es-ES.ftl",
69//! ))
70//! });
71//!
72//! Body
73//! }
74//! ```