Created
October 26, 2025 21:13
-
-
Save jrmoulton/ed0450f992e3633515ab2b890d556a8e to your computer and use it in GitHub Desktop.
floem localization api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ```rust | |
| use floem::prelude::*; | |
| fn main() { | |
| floem::launch(app_view); | |
| } | |
| fn app_view() -> impl IntoView { | |
| // Create bundles for all supported languages | |
| let bundles = vec![ | |
| L10nBundle::new("en-US", &[ | |
| include_str!("locales/en-US.ftl"), | |
| include_str!("locales/common.ftl"), | |
| ]), | |
| L10nBundle::new("fr-FR", &[ | |
| include_str!("locales/fr-FR.ftl"), | |
| include_str!("locales/common.ftl"), | |
| ]), | |
| L10nBundle::new("ja-JP", &[ | |
| include_str!("locales/ja-JP.ftl"), | |
| include_str!("locales/common.ftl"), | |
| ]), | |
| ]; | |
| v_stack(( | |
| header_view(), | |
| content_view(), | |
| footer_view(), | |
| )) | |
| .style(|s| s | |
| .l10n_bundles(bundles) // Inherited property - flows down to all children | |
| .locale("en-US") // Inherited property - flows down to all children | |
| .width_full() | |
| .height_full() | |
| ) | |
| } | |
| fn content_view() -> impl IntoView { | |
| let item_count = create_rw_signal(5); | |
| let user_name = create_rw_signal("Alice"); | |
| let user_role = create_rw_signal("Admin"); | |
| v_stack(( | |
| // Simple localized text with fallback | |
| l10n("welcome.title") | |
| .style(|s| s.fallback("Welcome")), | |
| // With reactive arguments | |
| l10n("greeting") | |
| .arg("name", move || user_name.get()) | |
| .arg("role", move || user_role.get()) | |
| .style(|s| s.fallback("Hello, User!")), | |
| // With reactive plural count | |
| l10n("item-count") | |
| .arg("count", move || item_count.get()) | |
| .style(|s| s.fallback("Items")), | |
| // Increment button to show reactive plural updates | |
| button("Add Item").on_click_stop(move |_| { | |
| item_count.update(|n| *n += 1); | |
| }), | |
| // With multiple reactive args | |
| l10n("notification") | |
| .arg("user", move || "Bob") | |
| .arg("action", move || "commented") | |
| .style(|s| s.fallback("New notification")), | |
| )) | |
| .style(|s| s.gap(10.0)) | |
| } | |
| fn header_view() -> impl IntoView { | |
| l10n("app.title") | |
| .style(|s| s | |
| .font_size(24.0) | |
| .fallback("My Application") | |
| ) | |
| } | |
| fn footer_view() -> impl IntoView { | |
| // Can override locale for a specific subtree | |
| // Uses the same inherited bundles but different locale | |
| l10n("footer.copyright") | |
| .arg("year", move || "2025") | |
| .style(|s| s | |
| .locale("ja-JP") // Override inherited locale | |
| .font_size(12.0) | |
| .fallback("© 2025") | |
| ) | |
| } | |
| ``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment