From ac5e6d13dd9015a2512797446cb9b62011f7e3f1 Mon Sep 17 00:00:00 2001 From: JasperDeSutter Date: Tue, 20 Dec 2022 14:45:01 +0100 Subject: [PATCH] Add l10n example --- druid/examples/assets/en-US/banana.ftl | 7 +++ druid/examples/assets/fr-FR/banana.ftl | 7 +++ druid/examples/l10n.rs | 76 ++++++++++++++++++++++++++ druid/examples/readme.md | 7 +++ 4 files changed, 97 insertions(+) create mode 100644 druid/examples/assets/en-US/banana.ftl create mode 100644 druid/examples/assets/fr-FR/banana.ftl create mode 100644 druid/examples/l10n.rs diff --git a/druid/examples/assets/en-US/banana.ftl b/druid/examples/assets/en-US/banana.ftl new file mode 100644 index 0000000000..985028d024 --- /dev/null +++ b/druid/examples/assets/en-US/banana.ftl @@ -0,0 +1,7 @@ +banana-title = Banana count + +bananas = {$count -> + [0] No bananas + [1] One banana + *[other] {$count} bananas +} diff --git a/druid/examples/assets/fr-FR/banana.ftl b/druid/examples/assets/fr-FR/banana.ftl new file mode 100644 index 0000000000..ce0b516d33 --- /dev/null +++ b/druid/examples/assets/fr-FR/banana.ftl @@ -0,0 +1,7 @@ +banana-title = Nombre de bananes + +bananas = {$count -> + [0] Aucune banane + [1] Une banane + *[other] {$count} bananes +} diff --git a/druid/examples/l10n.rs b/druid/examples/l10n.rs new file mode 100644 index 0000000000..a530de9d5d --- /dev/null +++ b/druid/examples/l10n.rs @@ -0,0 +1,76 @@ +// Copyright 2019 The Druid Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! This is an example of how to translate and localize a druid application. +//! It uses the fluent (.ftl) files in the asset directory for defining defining messages. + +// On Windows platform, don't show a console when opening the app. +#![windows_subsystem = "windows"] + +use druid::widget::{prelude::*, Slider}; +use druid::widget::{Flex, Label}; +use druid::{AppLauncher, Data, Lens, LocalizedString, UnitPoint, WidgetExt, WindowDesc}; + +const VERTICAL_WIDGET_SPACING: f64 = 20.0; +const SLIDER_WIDTH: f64 = 200.0; + +#[derive(Clone, Data, Lens)] +struct BananaState { + count: f64, +} + +pub fn main() { + let main_window = WindowDesc::new(build_root_widget()) + .title(LocalizedString::new("banana-title")) + .window_size((400.0, 400.0)); + + let initial_state: BananaState = BananaState { count: 1f64 }; + + // start the application, referencing the translation files in /assets. + AppLauncher::with_window(main_window) + .log_to_console() + .localization_resources(vec!["banana.ftl".into()], "assets".into()) + .launch(initial_state) + .expect("Failed to launch application"); +} + +fn build_root_widget() -> impl Widget { + // create a label with a static translation + let title = Label::new(LocalizedString::new("banana-title")).with_text_size(28.0); + + // create a label that uses a translation with dynamic arguments + let banana_label = Label::new(|data: &BananaState, env: &Env| { + let mut s = LocalizedString::::new("bananas") + .with_arg("count", |d, _e| d.count.into()); + s.resolve(data, env); + + s.localized_str() + }) + .with_text_size(32.0); + + // control the banana count + let slider = Slider::new() + .with_range(0.0, 3.0) + .with_step(1.0) + .fix_width(SLIDER_WIDTH) + .lens(BananaState::count); + + Flex::column() + .with_child(title) + .with_spacer(VERTICAL_WIDGET_SPACING * 2.0) + .with_child(banana_label) + .with_spacer(VERTICAL_WIDGET_SPACING) + .with_child(slider) + .align_vertical(UnitPoint::CENTER) +} diff --git a/druid/examples/readme.md b/druid/examples/readme.md index b986532de3..0fa2ba4f97 100644 --- a/druid/examples/readme.md +++ b/druid/examples/readme.md @@ -86,6 +86,13 @@ cargo run --example invalidation --features="im" ``` A demonstration how to use debug invalidation regions in your own widgets, including some examples of builtin widgets. +## L10n +``` +cd druid/examples +LANG=fr-FR cargo run --example l10n +``` +Shows how to localize and translate text in druid. On Linux, set the LANG environment variable to either "fr-FR" or "en-US". + ## Layout ``` cargo run --example layout